Go to Post One of the reasons, we hate being from Hawaii. - waialua359 [more]
Home
Go Back   Chief Delphi > Other > Math and Science
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Old 22-12-2016, 16:31
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
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.


Attached Files
File Type: xls Euler.XLS (48.0 KB, 24 views)
File Type: xls Midpoint.XLS (62.0 KB, 22 views)

Last edited by Ether : 23-12-2016 at 11:00. Reason: typo
Reply With Quote
  #2   Spotlight this post!  
Old 22-12-2016, 16:46
Richard Wallace's Avatar
Richard Wallace Richard Wallace is offline
I live for the details.
FRC #3620 (Average Joes)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1996
Location: Southwestern Michigan
Posts: 3,619
Richard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond reputeRichard Wallace has a reputation beyond repute
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?
__________________
Richard Wallace

Mentor since 2011 for FRC 3620 Average Joes (St. Joseph, Michigan)
Mentor 2002-10 for FRC 931 Perpetual Chaos (St. Louis, Missouri)
since 2003

I believe in intuition and inspiration. Imagination is more important than knowledge. For knowledge is limited, whereas imagination embraces the entire world, stimulating progress, giving birth to evolution. It is, strictly speaking, a real factor in scientific research.
(Cosmic Religion : With Other Opinions and Aphorisms (1931) by Albert Einstein, p. 97)
Reply With Quote
  #3   Spotlight this post!  
Old 22-12-2016, 16:52
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by Richard Wallace View Post
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


Reply With Quote
  #4   Spotlight this post!  
Old 22-12-2016, 18:23
Hitchhiker 42's Avatar
Hitchhiker 42 Hitchhiker 42 is offline
Roboter
AKA: Mark Lavrentyev
FRC #4557 (FullMetal Falcons)
Team Role: Programmer
 
Join Date: Oct 2015
Rookie Year: 2015
Location: Cromwell, CT
Posts: 456
Hitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to behold
Re: numerical solution of differential equations

Quote:
Originally Posted by Richard Wallace View Post
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.
__________________



2016 - NE District Championship Entrepreneurship Award
2016 - Hartford District Industrial Design Award
2016 - Waterbury District Engineering Inspiration Award
Reply With Quote
  #5   Spotlight this post!  
Old 22-12-2016, 18:56
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,494
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: numerical solution of differential equations

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
Attached Files
File Type: xls parabolic.xls (63.5 KB, 2 views)
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 23-12-2016 at 11:16. Reason: Fixd sign on second equation, formatted cleaner
Reply With Quote
  #6   Spotlight this post!  
Old 22-12-2016, 22:44
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by Hitchhiker 42 View Post
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


Reply With Quote
  #7   Spotlight this post!  
Old 23-12-2016, 00:45
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations


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


Attached Files
File Type: txt x''=f(t,x,x').m.txt (741 Bytes, 8 views)
Reply With Quote
  #8   Spotlight this post!  
Old 23-12-2016, 11:46
sspoldi's Avatar
sspoldi sspoldi is offline
Steven Spoldi
AKA: Steve Spoldi
FRC #0230 (Gaelhawks)
Team Role: Mentor
 
Join Date: Mar 2010
Rookie Year: 2008
Location: Shelton, CT
Posts: 15
sspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to behold
Re: numerical solution of differential equations

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.
Attached Files
File Type: xls Bacward_Forward_euler.XLS (59.0 KB, 5 views)

Last edited by sspoldi : 23-12-2016 at 12:01.
Reply With Quote
  #9   Spotlight this post!  
Old 23-12-2016, 12:03
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,494
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by sspoldi View Post
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 View Post
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).
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.

Last edited by GeeTwo : 23-12-2016 at 12:20.
Reply With Quote
  #10   Spotlight this post!  
Old 23-12-2016, 12:36
sspoldi's Avatar
sspoldi sspoldi is offline
Steven Spoldi
AKA: Steve Spoldi
FRC #0230 (Gaelhawks)
Team Role: Mentor
 
Join Date: Mar 2010
Rookie Year: 2008
Location: Shelton, CT
Posts: 15
sspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to beholdsspoldi is a splendid one to behold
Re: numerical solution of differential equations

Quote:
Originally Posted by GeeTwo View Post
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.
Reply With Quote
  #11   Spotlight this post!  
Old 23-12-2016, 12:41
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by sspoldi View Post
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


Reply With Quote
  #12   Spotlight this post!  
Old 23-12-2016, 13:31
GeeTwo's Avatar
GeeTwo GeeTwo is offline
Technical Director
AKA: Gus Michel II
FRC #3946 (Tiger Robotics)
Team Role: Mentor
 
Join Date: Jan 2014
Rookie Year: 2013
Location: Slidell, LA
Posts: 3,494
GeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond reputeGeeTwo has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by Ether View Post
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.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
Reply With Quote
  #13   Spotlight this post!  
Old 23-12-2016, 15:00
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations

Quote:
Originally Posted by Ether View Post
θ'' = k1 + k2∙cos(θ) + k3∙θ'
derivation of k1 k2 k3


Attached Thumbnails
Click image for larger version

Name:	k1 k2 k3.png
Views:	11
Size:	15.4 KB
ID:	21394  
Reply With Quote
  #14   Spotlight this post!  
Old 23-12-2016, 15:58
Hitchhiker 42's Avatar
Hitchhiker 42 Hitchhiker 42 is offline
Roboter
AKA: Mark Lavrentyev
FRC #4557 (FullMetal Falcons)
Team Role: Programmer
 
Join Date: Oct 2015
Rookie Year: 2015
Location: Cromwell, CT
Posts: 456
Hitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to beholdHitchhiker 42 is a splendid one to behold
Re: numerical solution of differential equations

Quote:
Originally Posted by Ether View Post
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.
Attached Files
File Type: xls Copy of Midpoint.XLS (374.0 KB, 6 views)
File Type: xls Copy of Euler-2.XLS (248.0 KB, 4 views)
__________________



2016 - NE District Championship Entrepreneurship Award
2016 - Hartford District Industrial Design Award
2016 - Waterbury District Engineering Inspiration Award
Reply With Quote
  #15   Spotlight this post!  
Old 23-12-2016, 21:19
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 7,986
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: numerical solution of differential equations



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


Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 06:50.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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