Runge Kutta - units

  • #1
10
0
When conducting numerical methods using 4th Order Runge-Kutta do the physical units have to be maintained?

This never occurred to me until I was writing out all the steps in detail when showing someone I work with the method using a simple projectile motion with drag. It had 4th Order time terms this making the units wrong in the iteration process.

So when using numerical methods, do the units have to be conserved/maintained?
 

Answers and Replies

  • #2
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
I don't see how any problem can be successfully solved if the units are not preserved. Can you give your example, for clarification with what happened in your situation?
 
  • Like
Likes K Murty
  • #3
10
0
Exactly, that was my thoughts. The first sanity check of something is to check the units.

But does that have to be maintained when using numerical methods to solve something as opposed to algebraic.

I will get my document cleaned up and figure out how to post it here. It several pages.
 
  • #4
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
If it is a missile launch, then I guess you will have derivatives with respect to time. So your steps will be Δt, which should be in the correct unit.
 
  • #5
10
0
if we take a simple particle velocity decay in the air, mass*acceleration = K*Velocity2 where K is drag and geometry coefficents of the particle.
If we let u1=position, y, and du1/dt=velocity, and then we let u2=velocity and du2/dt=acceleration

With Runge Kutta,
dy/dt=f(u(t),t)
yn+1=yn+(h/6)*(k1+2*k2+2*k3+k4)
tn+1=tn+h
k1=f(un,t)
k2=f(un+(h/2)*k1,t+h/2)
k3=f(un+(h/2)*k2,t+h/2)
k4=f(un+h*k3,t+h)

for the first equation f(un,t) = u2

Then substituting u2 into the above set of steps we get the time iteration for position as follows:
yn+1=yn+(u2*h/6)*(6+3h+h2+h3/4)

The units of u1 is the same as yn which are meters. The units of u2 is meters/sec. The unit of h is time, seconds

Note the term (u2*h/6) is velocity (m/s) times time (sec) which is distance, meters. So from a units perspective the units are for yn+1, yn, and (u2*h/6) for meters just as one would expect.

But the kicker are the (6+3h+h2+h3/4) terms. The 6 has no units, the 3h has units of time, and h2 has units of time2, and the h3/4 has units of time3. The units in this last group are not preserved.

As you can see, the units are not preserved when applying 4th order Runge -Kutta to numerically solve the ODE

This was a surprise to me in that I thought that the units must be conserved. But the classical method to numerically solve ODE's clearly shows units are not preserved. Hence the question, when using numerical methods, do units have to be conserved?
 
  • #6
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
How did you get to the yn+1=yn+(u2*h/6)*(6+3h+h2+h3/4) ?
I am just not seeing that jump. I have done a Euler's Method with 2 variables, but not Runge Kutta. I did some search and found this, article, which sets it up as a linear algebra style problem.
https://www.myphysicslab.com/explain/runge-kutta-en.html
There is no combining to get squared or cubed h terms in that.
 
  • #7
10
0
if k1=u2
then k2=u2+h/2*k1=u2+h*u2/2
Then k3=u2+h/2*k2=u2+(h/2)*(u2+h*u2/2)=u2+h*u2/2+h2*u2/4
then k4=u2+h*k3=u2+h(u2+h*u2/2+h2*u2/4)=u2+h*u2+h2u2/2+h3u2/4

thus
yn+1=yn+(h/6)*(k1+2k2+2k3+k4) = yn+1=yn+(u2*h/6)*(6+3h+h2+h3/4)

See how this happens? Crazy right? Units are not preserved, and yet this is one of the most popular methods for numerically solving ODE's
 
  • #8
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
Except that k2 is a function of (un+(h/2)*k1,t+h/2), not equivalent to un+(h/2)*k1, correct?
 
  • #9
10
0
In this example, it happens to be that way. The function can have numerous parameters passed to it. In this example, time is not a variable that is being passed.

if we want to get really formal, dy/dt=f(x, dy/dt, dy2/d2t,t). since the original equation was mass*d2y/dt2=K*(dy/dy)2

For Runge Kutta, we convert the 2nd order differential equation into a set of 1st order equations
thus letting u1=y, du1/dt = dy/dt Then we let u2=dy/dt and then du2/dt = d2y/dt2

du1/dt=u2 which is the first function = f1(u2)
du2/dt=(K/mass)(u2)2 which is the second function = f2(u2)

u1,n+1=u1,n+(h/6)*(k11+2k12+2k13+k14)=yn+1

k11, k12, k13, k14 are for evaluating the first function
k21, k22, k23, k24 are for evaluating the second function

Thus this leads back to the previously posted details.
 
  • #10
165
102
It is usually smart to put the problem in dimensionless form before doing the Runge-Kutta integration. For example, if you integrate the equations of motion for a missile launch, scale the distances by the radius of the Earth, scale the velocity by the orbital velocity at the surface, scale the time by one unit of v divided by a unit of r. etc. On the other hand, if you are working with the Hydrogen atom, scale the length by the Bohr radius etc, maybe scale the velocity by the velocity of light. It is usually possible to scale the problem so you lose only a little precision with the calculation. You wouldn't want to scale the position vector of a missile relative to the center of the Earth as multiples of the Bohr radius.
 
  • #11
165
102
P.S I wrote many Runge Kutta routines without putting the problem in dimensionless terms from the beginning. Sometimes they worked, and sometimes I ran into trouble. After several years (my earlier advise is not obvious) I always put it in dimensionless form first.
 
  • #12
kimbyd
Science Advisor
Gold Member
1,156
605
BTW, if you use Latex, your equations will be clearer (and easier to write).
yn+1=yn+(h/6)*(k1+2*k2+2*k3+k4)
The units of u1 is the same as yn which are meters. The units of u2 is meters/sec. The unit of h is time, seconds
The equation I quoted requires the units of ##k_n## to be the same as the units of ##y_n / h##, or meters per second. That should make your units work out.
 
  • #13
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
The k1 through k4 are estimates of y' at point a point then slightly away from that point (see the +h/2 twice then the +h). These different estimates are then weight averaged, to provide a better estimate for y' than the Euler's Method (1st order Runge Kutta). This may explain it for you better than I am - http://lpsa.swarthmore.edu/NumInt/NumIntFourth.html
 
  • #14
atyy
Science Advisor
14,045
2,339
Fourth order Runge-Kutta basically uses the Taylor series expansion. The Taylor series expansion should have the right units.

 
Last edited:
  • Like
Likes scottdave
  • #15
BvU
Science Advisor
Homework Helper
2019 Award
13,594
3,285
yn+1=yn+(h/6)*(k1+2*k2+2*k3+k4)
is dimensionally correct, so if you substitute correctly, the result should also be correct.
Then substituting u2 into the above set of steps we get the time iteration for position as follows:
yn+1=yn+(u2*h/6)*(6+3h+h2+h3/4)
conversely, this result can not be right.
As you can see, the units are not preserved when applying 4th order Runge -Kutta to numerically solve the ODE
nonsense
This was a surprise to me in that I thought that the units must be conserved. But the classical method to numerically solve ODE's clearly shows units are not preserved. Hence the question, when using numerical methods, do units have to be conserved?
kudos for your gut feelings! units are conserved. anything else means there is an error.


but there is something else being overlooked here: your u has two components, one in meters and one in m/s. likewise your f has two components, one in m/s and one in m/s2. idem all k
 
  • Like
Likes scottdave
  • #16
BvU
Science Advisor
Homework Helper
2019 Award
13,594
3,285
if we want to get really formal, dy/dt=f(x, dy/dt, dy2/d2t,t). since the original equation was mass*d2y/dt2=K*(dy/dy)2
getting formal means you don’t mix notations and work accurately.
if the original equation is really $$m\ddot y = k\dot y^2$$ then there is no ##x## in the problem and ##\dot y## is a not a function of any of those variables you mention directly, but instead the item you want to find through integration (together with ##y## itself)

and the set of first order equations is $${dy\over dt}=\dot y\\\mathstrut \\ {d\dot y\over dt} ={k\over m}\dot y^2$$
For Runge Kutta, we convert the 2nd order differential equation into a set of 1st order equations
thus letting u1=y, du1/dt = dy/dt Then we let u2=dy/dt and then du2/dt = d2y/dt2

du1/dt=u2 which is the first function = f1(u2)
du2/dt=(K/mass)(u2)2 which is the second function = f2(u2)

u1,n+1=u1,n+(h/6)*(k11+2k12+2k13+k14)=yn+1

k11, k12, k13, k14 are for evaluating the first function
k21, k22, k23, k24 are for evaluating the second function

Thus this leads back to the previously posted details.
looks a lot better than post #1 where ##u## and ##k## only had one index:wink:
and of course you mean ‘follow from evaluating’ instead of ‘are for evaluating’

just to reassure us you now know what to do: please re-write post#1 in an absolutely immaculate form....
 
  • #17
scottdave
Science Advisor
Homework Helper
Insights Author
1,801
754
Just to recap, dimensions (or units if you like) should match. You cannot say 3 miles per hour + 2 miles, for example. This is a powerful tool to see if your answer makes sense. This means if you come up with something like you did (6+3h+h2+h3/4) where h carries dimension (time in this case), then they cannot be added as @BvU stated that you did not think it was correct.
You should suspect that something is not right, but don't jump to the conclusion that the Mathematicians, who created the formulas, are wrong. The more likely scenario is that there is a mistake in your workings, somewhere along the line.

Anybody is capable of a mistake. So yes, it is possible that a textbook or test question has an error on it.

Were you able to figure out where your error was, to get the dimensions to match?
 

Related Threads on Runge Kutta - units

  • Last Post
Replies
3
Views
3K
Replies
1
Views
3K
  • Last Post
Replies
5
Views
6K
  • Last Post
Replies
1
Views
6K
Replies
6
Views
3K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
7
Views
2K
Replies
6
Views
23K
  • Last Post
Replies
6
Views
19K
  • Last Post
Replies
2
Views
2K
Top