*consider the function x = cos(t)
the first derivative is x’ = -sin(t)
and the second derivative is x" = -cos(t)
so we have the differential equation x" = -x
and x = cos(t) is the analytical (true) solution to that differential equation
with initial conditions x=1 and x’=0 at t=0.
Now turn things around.
suppose we are given the differential equation x" = -x’,
with initial conditions x=1 and x’=0 at t=0,
and we want to plot x and x’ vs t,
but we don’t know how to find the analytical solution,
so we decide to numerically integrate it using the Euler method:
x’[n+1] = x’[n] + x"[n]*dt
x[n+1] = x[n] + x’[n]*dt
x"[n+1] = -x[n+1]
See attached spreadsheet Euler.XLS to see what happens. Yikes.
Columns Xa and Va are the analytical (true) solutions for position and velocity.
Columns Xe and Ve are the Euler Method numerical solutions for position and velocity.
Now instead of using the Euler method, use the Midpoint method:
Vmid = x’[n] + x"[n]*dt/2
Xmid = x[n] + Vmid*dt/2
Amid = -Xmid
x[n+1] = x[n] + Vmid*dt
x’[n+1] = x’[n] + Amid*dt
x"[n+1] = -x[n+1]
See spreadsheet Midpoint.XLS
Columns Xa and Va are the analytical (true) solutions for position and velocity.
Columns Xm and Vm are the Midpoint Method numerical solutions for position and velocity.
Columns Vmid, Xmid, and Amid are the extra columns needed for the Midpoint Method.
Notice that even though the Midpoint method requires 3 additional columns,
you can double the step size dt,
so the computation is just as fast as Euler,
but with far better accuracy.
*
Euler.XLS (48 KB)
Midpoint.XLS (62 KB)
Euler.XLS (48 KB)
Midpoint.XLS (62 KB)