numerical solution of differential equations

*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)

Hi Russ!

I see you are bored, or maybe just antsy for the 2017 FIRST Robotics Competition kickoff. Either way, this is an interesting topic in numerical methods.

Question for an interested student (someone much younger than me):

Should we expect, in general, that Euler integration will be well suited to approximate monotonic systems, while Midpoint integration gives better results for periodic systems? If so, why?

Yes I am very much looking forward to the Kickoff.

And I have some long-overdue hardware to return to you :slight_smile:

As I understand it, the main difference between the Euler method and the Midpoint method is that the midpoint method takes the slope by connecting a point behind and a point ahead of the given point. The Euler method just takes the slope at that point at extrapolates for that step. Please, do correct me if I’m wrong.

So, a monotonic function only increases (or decreases). It seems Euler integration would be better suited (read: more accurate) because the midpoint method depends on points behind the given point that’s being calculated, which will tend to keep the slope smaller than it should be, whereas because the function doesn’t tend to change direction (up or down) as much (it can only go one direction - monotonic), approximating ahead will tend to be better than approximating while taking into account behind the current point as well.

What surprises me more than the gain in amplitude for Euler (which is pretty easy to guess if you consider what happens to energy at different points) is the excellent prediction of the period. I’ll have to give this a look.

I was able to do a version without the two extra columns that tracked pretty closely, using the parabolic formula for constant acceleration to calculate the next position, and the average acceleration assuming constant jerk (x’’’) to calculate the next velocity.

x[n+1]   =  x[n]  + dt*(x'[n]  + x''[n]*dt/2)
x''[n+1] = -x[n+1] 
x'[n+1]  =  x'[n] + dt*(x''[n] + x''[n+1])/2
```<br><br><a class='attachment' href='/uploads/default/original/3X/2/a/2a863b372feee98656c6c9ea854435d9f072b5b0.xls'>parabolic.xls</a> (63.5 KB)<br><br><br><a class='attachment' href='/uploads/default/original/3X/2/a/2a863b372feee98656c6c9ea854435d9f072b5b0.xls'>parabolic.xls</a> (63.5 KB)<br>

Where did you come by this understanding?

It seems Euler integration would be better suited [for monotonic function]…

You could easily modify the XLS I posted to test this hypothesis :slight_smile:

*Unless I was careless with the algebra (it happens when I’m tired),
Steve’s catapult can be modeled with an ODE of the form

θ’’ = k[sub]1[/sub] + k[sub]2[/sub]∙cos(θ) + k[sub]3[/sub]∙θ’

Attached is an Octave script that uses Octave’s built-in ODE solver “lsode”
to numerically integrate arbitrary ODEs of the form x’’ = f(t,x,x’)

*

x’’=f(t,x,x’).m.txt (741 Bytes)


x’’=f(t,x,x’).m.txt (741 Bytes)

Hillbilly solution:
one forward Euler integration, one backward Euler integration, less typing and good enough for government work.

Surprising how often that works…

Cheers,
Steve.

P.S. The equation (for the catapult) should be something like θ" = K1∙(K2 - θ’), θ is just along for the ride.

Bacward_Forward_euler.XLS (59 KB)


Bacward_Forward_euler.XLS (59 KB)

That is:

x'[n+1]  =  x'[n] + dt*x''[n]   //backward looking
x[n+1]   =  x[n]  + dt*x'[n+1]  //forward looking
x''[n+1] = -x[n+1] 

It looks OK on amplitude, but overpredicted the resonant frequency by … almost half a part per thousand. Certainly good enough for FRC.

The cosθ term is gravity acting on the boulder (and lever arm).

Yea, I’d like to say we just ignore stuff like that, but sometimes it’s a real factor. In 2014 we had a hammer with a 3 pound head on a 1 foot arm, gravity definitely made a difference.

Since we typically don’t have a lot of time (who does), I like to get the kids to do a simple model up front, and then we do some system id and fit the actual robot behavior to a model. This way we can tune control systems quickly, and it gives them a chance to do some data based optimization in addition to a little physics up front.

Cheers,
Steve.

V[sub]n+1[/sub] = V[sub]n[/sub] + A[sub]n[/sub]∙dt;
X[sub]n+1[/sub] = X[sub]n[/sub] + V[sub]n+1[/sub]∙dt;

Maybe provides some insight: the above is algebraically equivalent to

V[sub]n+1[/sub] = V[sub]n[/sub] + A[sub]n[/sub]∙dt;
X[sub]n+1[/sub] = X[sub]n[/sub] + dt∙(V[sub]n[/sub]+V[sub]n+1[/sub])/2 + ½∙A[sub]n[/sub]∙dt2

…so counting the constant acceleration term twice in the position calculation mostly offsets not counting the jerk in the velocity calculation…at least in this case.

derivation of k[sub]1[/sub] k[sub]2[/sub] k[sub]3[/sub]

*





I’ve attached your spreadsheets modified for 1/4*x^3 + 1.
It seems that the Euler method does approximate it better.

Copy of Midpoint.XLS (374 KB)
Copy of Euler-2.XLS (248 KB)


Copy of Midpoint.XLS (374 KB)
Copy of Euler-2.XLS (248 KB)

@Mark: Where did you get the accel formula for columns D and G.

Not sure. Must have been really tired :slight_smile: . I did that over, and also added an error column to show the difference between Xa and the predicted X (Xm or Xe).
I set dt = 0.01 s and compared the errors at t = 1, 2, 4 s

For 1s:
Midpt error: 0.598
Euler error: 0.585

For 2s:
Midpt error: 2.834
Euler error: 2.748

For 4s:
Midpt error: 50.070
Euler error: 48.113

Seems that, at least for this function, the Euler method holds up better. Tomorrow, I’ll try it with a function where the second derivative isn’t always positive on the interval I’m testing.

And the new sheets:

Copy of Euler-2.XLS (280 KB)
Copy of Midpoint.XLS (364 KB)


Copy of Euler-2.XLS (280 KB)
Copy of Midpoint.XLS (364 KB)

where did you get the revised accel formula?

I took the derivative of the actual velocity function. Is this not correct?

No, as I read it you’re setting x’’=3x/2. This would be solved by a function of the form x=Ae3x/2