Solving A Second Order Differential Equation With Runge Kutta
Solving A Second Order Differential Equation With Runge Kutta
d2x
m F
dt 2
Runge Kutta solves first order equations. We can solve the second order equation by turning it
into two first order equations. A new variable is defined as the first time derivative of x.
We will call this v for historical reasons. The set of two equations below completely describes
the problem:
dx dv F x 0 v 0
v, These are starting values:
F 1 m 1
dt dt m
x On the right side I used the Insert|Matrix command from the matrix menu
X
v and selected one column and two rows. X is the vector containing the
variables that are the answers.
Now solve the equations using Runge Kutta for t = 0 to t = 10. Let: tstart 0 tend 10
This interval will be divided into 10 parts: npoints 10
0 We must put the starting values of x and v into the vector Xstart.
Xstart
0 These are the constants of integration.
The equations of motion will be integrated by Runge-Kutta by using the rkfixed function.
The arguments are the starting value of x, the starting and ending values of t, the number of points
on the time interval
The answer matrix F will contain the times and the successive values of the vector X.
5/11/2012 Numercial methods Differential equations 2
The answer matrix returned b y rkfixed: Plots of the distance and velocity:
t X X
0 1 velocity v(t)
0 1 2 10
0 0 0 0 8
1 1 0.5 1 2 6
F
2 2 2 2 4
3 3 4.5 3 2
4 4 8 4 0
F 0 2 4 6 8 10
5 5 12.5 5
0
6 6 18 6 F
7 7 24.5 7
8 8 32 8 distance x(t)
9 9 40.5 9 50
10 10 50 10 40
1 30
F
These plots are what we expected. 20
With constant force, velocity increases linearly 10
with time and distance increases with the square 0
0 2 4 6 8 10
of time. Note that 0.5 a t2 = 50 when t = 10.
0
F
Now throw a ball in two dimensions:
The vector X will contain four components: x, y, dx/dt, dy/dt.
0 x is X0 My starting conditions are that I throw the ball from x,y = 0,0
y is X1 with v = 50 in the x direction and v = 50 in the y direction.
Xstart
0
50 dx/dt is X2
50 dydt is X3 g 9.8 is the acceleration of gravity, in the -y direction
X2 dx/dt = vx = X2
We will again integrate from t = 0 to t = 10
using 10 points.
X dy/dt = vy = X3
DX( t X) 3
dvx/dt = 0 F rkfixed ( Xstart tstart tend npoints DX)
0
dvy/dt = -g
g path of thrown ball
150