# Numerical Integration of 2nd Order DE

by strokebow
Tags: integration, numerical, order
 P: 101 I am intending to use Runge Kutta 4th order to numerically solve a system of coupled equations: $\frac{d^{2}x}{dt^{2}}$ = K1 * x * cos(t) + ( (K2 * $\frac{dy}{dt}$) - $\frac{dz}{dt}$ ) $\frac{d^{2}y}{dt^{2}}$= -K1 * y * cos(t) + ( (K2 * $\frac{dz}{dt}$) - $\frac{dx}{dt}$ ) $\frac{d^{2}z}{dt^{2}}$= ( (K2 * $\frac{dx}{dt}$) - $\frac{dy}{dt}$ ) I'm really a bit stuck to be honest. I've only ever used RK4 on 1st order linear ODEs. I've been reading around alot but not making much progress. Initial values for $\frac{dx}{dt}$, $\frac{dy}{dt}$, $\frac{dz}{dt}$ are all known. The constants K are known. Can anyone please help? Thanks
 P: 290 Introduce the derivatives as new variables, say u=dx/dt and solve the system of 6 first order equations, where the second derivatives are replaced by the first derivatives du/dt etc. so the last equation will be dw/dt = k2*u - v but you could also write it as: dw/dt = k2*dx/dt - dy/dt They will lead to the same solution, but their convergence behavior will be slightly different.
P: 101
 Quote by bigfooted Introduce the derivatives as new variables, say u=dx/dt and solve the system of 6 first order equations, where the second derivatives are replaced by the first derivatives du/dt etc. so the last equation will be dw/dt = k2*u - v but you could also write it as: dw/dt = k2*dx/dt - dy/dt They will lead to the same solution, but their convergence behavior will be slightly different.
Thanks very much for your input.

So I would define:
u = $\frac{dx}{dt}$
v = $\frac{dy}{dt}$
w = $\frac{dz}{dt}$
and
$\frac{du}{dt}$ = $\frac{d^{2}x}{dt^{2}}$
$\frac{dv}{dt}$ = $\frac{d^{2}y}{dt^{2}}$
$\frac{dw}{dt}$ = $\frac{d^{2}z}{dt^{2}}$

Therefore my equations become:
(1'): $\frac{du}{dt}$ = K$_{1}$ * x * cos(t) + ( (K$_{2}$ * v) - w )
(2'): $\frac{dv}{dt}$ = -K$_{1}$ * y * cos(t) + ( (K$_{2}$ * w) - u )
(3'): $\frac{dw}{dt}$ = ( (K$_{2}$ * u) - v )

Now for applying RK4 . . .
How would I do that simultaneously for both. I've found a couple of exams on the net but its quite difficult to follow since all the equations are inter-related.

Any help is much appreciated!

P: 101
Numerical Integration of 2nd Order DE

 Quote by bigfooted They will lead to the same solution, but their convergence behavior will be slightly different.
Not sure what you mean by this big foot?
PF Gold
P: 1,352
 Quote by strokebow Thanks very much for your input. So I would define: u = $\frac{dx}{dt}$ v = $\frac{dy}{dt}$ w = $\frac{dz}{dt}$ and $\frac{du}{dt}$ = $\frac{d^{2}x}{dt^{2}}$ $\frac{dv}{dt}$ = $\frac{d^{2}y}{dt^{2}}$ $\frac{dw}{dt}$ = $\frac{d^{2}z}{dt^{2}}$ Therefore my equations become: (1'): $\frac{du}{dt}$ = K$_{1}$ * x * cos(t) + ( (K$_{2}$ * v) - w ) (2'): $\frac{dv}{dt}$ = -K$_{1}$ * y * cos(t) + ( (K$_{2}$ * w) - u ) (3'): $\frac{dw}{dt}$ = ( (K$_{2}$ * u) - v ) Now for applying RK4 . . . How would I do that simultaneously for both. I've found a couple of exams on the net but its quite difficult to follow since all the equations are inter-related. How would I go about this for this case?
You now have a system of 6 1st-order equations to solve, (1')-(3') plus
(4'): $\frac{dx}{dt} = u$
(5'): $\frac{dy}{dt} = v$
(6'): $\frac{dz}{dt} = w$
P: 101
 Quote by DrClaude You now have a system of 6 1st-order equations to solve, (1')-(3') plus (4'): $\frac{dx}{dt} = u$ (5'): $\frac{dy}{dt} = v$ (6'): $\frac{dz}{dt} = w$

I appreciate that we now have a system of 1st order DE's. Its just that trying to apply RK4 to this system concurrently is quite confusing.
I'm trying to follow the example given on the net (e.g., http://www.nsc.liu.se/~boein/f77to90/rk.html)

But am finding it tough to implement.

Especially since these equations are coupled with more than 2 variables. Like with eqn (1') its depending on both v and w.
 Sci Advisor PF Gold P: 1,352 Maybe this formulation will be more useful. Set a vector $$\mathbf{a} = (u,v,w,x,y,z)$$ and a vector function $$\mathbf{f}(\mathbf{a},t) = (\frac{du}{dt},\frac{dv}{dt},\frac{dw}{dt},\frac{dx}{dt},\frac{dy}{dt}, \frac{dz}{dt})$$ The 4th-order RK algorithm then reads $$\mathbf{k}_1 = h \mathbf{f}(\mathbf{a}_n, t_n)$$ $$\mathbf{k}_2 = h \mathbf{f}(\mathbf{a}_n + \frac{\mathbf{k}_1}{2}, t_n + \frac{h}{2})$$ $$\mathbf{k}_3 = h \mathbf{f}(\mathbf{a}_n + \frac{\mathbf{k}_2}{2}, t_n + \frac{h}{2})$$ $$\mathbf{k}_4 = h \mathbf{f}(\mathbf{a}_n + \mathbf{k}_3, t_n + h)$$ $$\mathbf{a}_{n+1} = \mathbf{a}_n + \frac{\mathbf{k}_1}{6} + \frac{\mathbf{k}_2}{3} + \frac{\mathbf{k}_3}{3} + \frac{\mathbf{k}_4}{6}$$ Hope this helps.
P: 101
 Quote by DrClaude Maybe this formulation will be more useful. Set a vector $$\mathbf{a} = (u,v,w,x,y,z)$$ and a vector function $$\mathbf{f}(\mathbf{a},t) = (\frac{du}{dt},\frac{dv}{dt},\frac{dw}{dt},\frac{dx}{dt},\frac{dy}{dt}, \frac{dz}{dt})$$ The 4th-order RK algorithm then reads $$\mathbf{k}_1 = h \mathbf{f}(\mathbf{a}_n, t_n)$$ $$\mathbf{k}_2 = h \mathbf{f}(\mathbf{a}_n + \frac{\mathbf{k}_1}{2}, t_n + \frac{h}{2})$$ $$\mathbf{k}_3 = h \mathbf{f}(\mathbf{a}_n + \frac{\mathbf{k}_2}{2}, t_n + \frac{h}{2})$$ $$\mathbf{k}_4 = h \mathbf{f}(\mathbf{a}_n + \mathbf{k}_3, t_n + h)$$ $$\mathbf{a}_{n+1} = \mathbf{a}_n + \frac{\mathbf{k}_1}{6} + \frac{\mathbf{k}_2}{3} + \frac{\mathbf{k}_3}{3} + \frac{\mathbf{k}_4}{6}$$ Hope this helps.

So (replacing the 'k's' from the RK4 algorithm with 'm's' and 'n's')

For the first step, would this be correct:
m1u = h x (1')
m1v = h x (2')
m1w = h x (3')

n1u = h x (4')
n1v = h x (5')
n1w = h x (6')

?
 Sci Advisor PF Gold P: 1,352 I'm not sure I follow you. The "first step" is the calculation of $\mathbf{k}_1$. The first element of $\mathbf{f}(\mathbf{a}_n,t_n)$ is $$\left.\frac{du}{dt}\right|_{t_n} = K_1 x_n \cos(t_n) + ( K_2 v_n - w_n )$$ so the first element of $\mathbf{k}_1$ is $$h \times \left[ K_1 x_n \cos(t_n) + ( K_2 v_n - w_n ) \right]$$ and so on. For instance, the 4th element of $\mathbf{k}_1$ is $$u_n$$ Once you have calculated all six elements of $\mathbf{k}_1$, the "second step" is do the the same for $\mathbf{k}_2$, which will depend on the calculation of $\mathbf{k}_1$. Once you have calculated all $\mathbf{k}$'s, you can advance the solution one time step with the formula for $\mathbf{a}_{n+1}$. The easiest way to implement this numerically is not to program it explicitly, bu to write a function that given an array of six values $(u,v,w,x,y,z)$ and a value of $t$, returns an array of the six values $(du/dt,dv/dt,dw/dt,dx/dt,dy/dt,dz/dt)$. The RK integrator then just makes 4 calls to this function to calculate $\mathbf{k}_1$-$\mathbf{k}_4$, and then the new values of $(u,v,w,x,y,z)$.
P: 101
 Quote by DrClaude I'm not sure I follow you. The "first step" is the calculation of $\mathbf{k}_1$. The first element of $\mathbf{f}(\mathbf{a}_n,t_n)$ is $$\left.\frac{du}{dt}\right|_{t_n} = K_1 x_n \cos(t_n) + ( K_2 v_n - w_n )$$ so the first element of $\mathbf{k}_1$ is $$h \times \left[ K_1 x_n \cos(t_n) + ( K_2 v_n - w_n ) \right]$$ and so on. For instance, the 4th element of $\mathbf{k}_1$ is $$u_n$$ Once you have calculated all six elements of $\mathbf{k}_1$, the "second step" is do the the same for $\mathbf{k}_2$, which will depend on the calculation of $\mathbf{k}_1$. Once you have calculated all $\mathbf{k}$'s, you can advance the solution one time step with the formula for $\mathbf{a}_{n+1}$. The easiest way to implement this numerically is not to program it explicitly, bu to write a function that given an array of six values $(u,v,w,x,y,z)$ and a value of $t$, returns an array of the six values $(du/dt,dv/dt,dw/dt,dx/dt,dy/dt,dz/dt)$. The RK integrator then just makes 4 calls to this function to calculate $\mathbf{k}_1$-$\mathbf{k}_4$, and then the new values of $(u,v,w,x,y,z)$.
Thanks Dr C. Appreciate you taking time out to help me with this.

So, there are 6 equations for each step. i.e., k$_{1}$ has 6 equations. Writing them explicitly (I take your point that a generic function is best for programming but for now I am just trying to understand this solution):
$k_{1}u = h (K_1 x \cos(t) + ( K_2 v - w ))$

$k_{1}v = h (-K_1 y \cos(t) + ( K_2 v - w ))$

$k_{1}w = h (K_2 u - v)$

$k_{1}u' = ?$

$k_{1}v' = ?$

$k_{1}w' = ?$

When you said:
 Quote by DrClaude For instance, the 4th element of $\mathbf{k}_1$ is $$u_n$$
Do you mean that we just consider these (4th, 5th and 6th elements of the vector) to be constants?

Because we can derive expressions for u, v and w from (1'), (2') and (3')
 Sci Advisor PF Gold P: 1,352 Lets take it one step back. You converted your system of 3 2nd-order differential equations into a system of 6 first-order differential equations. You have increased the number of variables to 6, but that is a small price to pay compared with the gain 2nd to 1st order. The equations now read: $$\frac{du}{dt} = K_{1} x \cos(t) + ( K_{2} v - w )$$ $$\frac{dv}{dt} = -K_{1} y \cos(t) + ( K_{2} w - u )$$ $$\frac{dw}{dt} = (K_{2} u - v )$$ $$\frac{dx}{dt} = u$$ $$\frac{dy}{dt} = v$$ $$\frac{dz}{dt} = w$$ The first step is to calculate the elements of $\mathbf{k}_1$, which I will call $(u',v',w',x',y',z')$. $$u' = h \left[ K_{1} x_n \cos(t_n) + ( K_{2} v_n - w_n ) \right]$$ $$v' = h \left[ -K_{1} y_n \cos(t_n) + ( K_{2} w_n - u_n ) \right]$$ $$w' = h (K_{2} u_n - v_n )$$ $$x' = h u_n$$ $$y' = h v_n$$ $$z' = h w_n$$ Now, you need to calculate $\mathbf{k}_2 = (u'',v'',w'',x'',y'',z'')$. $$u'' = h \left[ K_{1} \left( x_n + \frac{x'}{2}\right) \cos(t_n + \frac{h}{2}) + ( K_{2} \left(v_n + \frac{v'}{2} \right) - \left(w_n + \frac{w'}{2} \right) ) \right]$$ and so on for the other variables. Finally, once you have all values $u',u'',u''',u''''$, you find $$u_{n+1} = u_n + \frac{u'}{6} + \frac{u''}{3} + \frac{u'''}{3} + \frac{u''''}{6}$$ and so on for the other variables.
 P: 101 Dr C. Thanks very much. That is very clear. I'm going to have a go at writing it all out explicitly and then I'll post it on here. Cheers!!

 Related Discussions Calculus 12 Differential Equations 1 Engineering, Comp Sci, & Technology Homework 9 Calculus & Beyond Homework 3 Differential Equations 10