Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Math and Science (http://www.chiefdelphi.com/forums/forumdisplay.php?f=70)
-   -   numerical solution of differential equations (http://www.chiefdelphi.com/forums/showthread.php?t=152790)

Ether 12-22-2016 04:31 PM

numerical solution of differential equations
 
2 Attachment(s)

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.



Richard Wallace 12-22-2016 04:46 PM

Re: numerical solution of differential equations
 
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?

Ether 12-22-2016 04:52 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by Richard Wallace (Post 1622983)
maybe just antsy for the 2017 FIRST Robotics Competition kickoff.

Yes I am very much looking forward to the Kickoff.

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



Hitchhiker 42 12-22-2016 06:23 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by Richard Wallace (Post 1622983)
Hi Russ!
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?

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.

GeeTwo 12-22-2016 06:56 PM

Re: numerical solution of differential equations
 
1 Attachment(s)
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.

Code:

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


Ether 12-22-2016 10:44 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by Hitchhiker 42 (Post 1623004)
As I understand it,...the midpoint method takes the slope by connecting a point behind and a point ahead of the given point...Please, do correct me if I'm wrong.

Where did you come by this understanding?

Quote:

It seems Euler integration would be better suited [for monotonic function]...
You could easily modify the XLS I posted to test this hypothesis :)



Ether 12-23-2016 12:45 AM

Re: numerical solution of differential equations
 
1 Attachment(s)

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

θ'' = k1 + k2∙cos(θ) + k3∙θ'

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



sspoldi 12-23-2016 11:46 AM

Re: numerical solution of differential equations
 
1 Attachment(s)
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.

GeeTwo 12-23-2016 12:03 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by sspoldi (Post 1623149)
Hillbilly solution:
one forward Euler integration, one backward Euler integration, less typing and good enough for government work.

Surprising how often that works...

That is:

Code:

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.

Quote:

Originally Posted by sspoldi (Post 1623149)
P.S. The equation (for the catapult) should be something like θ" = K1∙(K2 - θ'), θ is just along for the ride.

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

sspoldi 12-23-2016 12:36 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by GeeTwo (Post 1623151)
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.

Ether 12-23-2016 12:41 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by sspoldi (Post 1623149)
one forward Euler integration, one backward Euler integration

Vn+1 = Vn + An∙dt;
Xn+1 = Xn + Vn+1∙dt;

Maybe provides some insight: the above is algebraically equivalent to

Vn+1 = Vn + An∙dt;
Xn+1 = Xn + dt∙(Vn+Vn+1)/2 + ½∙An∙dt2



GeeTwo 12-23-2016 01:31 PM

Re: numerical solution of differential equations
 
Quote:

Originally Posted by Ether (Post 1623160)
Maybe provides some insight: the above is algebraically equivalent to

Vn+1 = Vn + An∙dt;
Xn+1 = Xn + dt∙(Vn+Vn+1)/2 + ½∙An∙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.

Ether 12-23-2016 03:00 PM

Re: numerical solution of differential equations
 
1 Attachment(s)
Quote:

Originally Posted by Ether (Post 1623101)
θ'' = k1 + k2∙cos(θ) + k3∙θ'

derivation of k1 k2 k3



Hitchhiker 42 12-23-2016 03:58 PM

Re: numerical solution of differential equations
 
2 Attachment(s)
Quote:

Originally Posted by Ether (Post 1623089)
You could easily modify the XLS I posted to test this hypothesis :)

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

Ether 12-23-2016 09:19 PM

Re: numerical solution of differential equations
 


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




All times are GMT -5. The time now is 04:19 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi