Numerical Methods For Second Order Differential Equation
Numerical Methods For Second Order Differential Equation
Terence Etchells
(a)
y sin( xy ) y = y e x
(b)
xy (1 + x 2 ) y + y = e x
Page 1
3/2/99
Terence Etchells
#4:
#5:
#6:
Author
f ( x , y , z ):= 4 y + 2 z
g ( x , y , z ):= 3 y z
EULER _ SIM ( x , y , z ,0,11
, ,0.110
, )
approX #6
to give
Liverpool John Moores University
Page 2
3/2/99
(C)
Terence Etchells
#8:
#9:
#10:
y = z
z = e x 3z 2 y
Author
f ( x , y , z ):= z
g ( x , y , z ):= e x 3z 2 y
( Alt e for the exponential e)
EULER _ SIM ( x , y , z ,0,0,1,0110
., )
approX #10 to give
Page 3
3/2/99
Terence Etchells
We can plot y against x by extracting the first two columns of this matrix, the
following function will do this for us.
#12: EXTRACT (m): = [m`sub1, m`sub2]`
#13: EXTRACT(#11)
approX #13 , Plot (Beside at 40), Centre , Zoom in (once: press F9).
we get
Page 4
3/2/99
#15:
Terence Etchells
Author
DSOLVE 2 _ IV (3,2, e x , x ,0,0,1)
Simplify #15 and Plot #16
we get
Which shows that, as one would expect, the Euler method is peeling away from the
solution as x increases.
The Modified Euler Method
In the single first order case, the modified Euler method is defined as
h
y n +1 = y n + ( k 1 + k 2 ) where k 1 = f ( x , y ) and k 2 = f ( x + h, y + hk 1 ).
2
As in the Euler method, we have a third variable, z, which is also changing as x steps
along the x axis. So we adjust the modified Euler method to account for the third
variable thus:
Let f ( x, y , z ) = k 1 and g ( x , y, z ) = l1 (we use l as it is next to k in the alphabet)
and also let
k 2 = f ( x + h, y + hk 1 , z + hl1 ) and l2 = g ( x + h, y + hk1 , z + hl1 ).
We can now define the recurrence equations for y and z as,
h
h
y n +1 = y n + ( k 1 + k 2 ) and z n +1 = z n + ( l1 + l2 )
2
2
respectively.
Page 5
3/2/99
Terence Etchells
Example 8A
Find an approximate value for y(11
. ) and z(11
. ) , where y ( x ) and z ( x ) satisfy the
simultaneous differential equations,
dy
dz
= x + yz and
= xz
dx
dx
with initial values x 0 = 1, y 0 = 0 and z 0 = 0 and a step size of h=0.1.
So k 1 = x + yz and l1 = x z . Using the initial values we have that
k1 = 1 + 0 * 0 = 1 and l1 = 1 0 = 1
So, as k 2 = f ( x + h, y + hk 1 , z + hl1 ) and l2 = g ( x + h, y + hk1 , z + hl1 ),
then k 2 = 11
. + (0 + 01
. * 1)(0 + 0.1* 1) = 111
. and l2 = 11
. (0 + 01
. * 1) = 1
Which leads to,
h
01
.
y (11
. ) = y1 = y 0 + ( k 1 + k 2 ) = 0 =
(1 + 11
. ) = 01055
.
and
2
2
h
01
.
z (11
. ) = z1 = z 0 + ( l1 + l2 ) = 0 + (1 + 1) = 01
..
2
2
Whilst not overcomplicated in its computation, one wouldnt wish to be doing this
1000 times!
DERIVE ACTIVITY 8B
(A)
Write a function that will automate the modified Euler method for two
dy
= f ( x , y , z ) and
simultaneous first order differential equations
dx
dz
= g ( x , y , z ) ,with initial values x 0 , y 0 , z 0 and step size h.
dx
#1:
#2:
#3:
#4:
#5:
#6:
#7:
#8:
#9:
#10:
#12:
#13:
Author
f ( x , y , z ):=
g ( x , y , z ):=
[ x 0: =, y 0: =, z 0: = ]
k 1: = f ( x , y, z )
l1: = g ( x , y , z )
k 2: = f ( x + h, y + hk 1, z + hl1)
l 2: = g ( x + h, y + hk 1, z + hl1)
h
y + ( k 1 + k 2)
2
h
z + ( l1 + l 2)
2
[ x + h,#8,#9]
Simplify #10
Author
ITERATES (#11,[ x , y , z ],[ x 0, y 0, z 0], n)
RK 2 _ SIM ( x , y , z , x 0, y 0, z 0, h, n): =#12
Remove #4 to #12
Transfer Save Derive as RK2_SIM.MTH
Page 6
3/2/99
Terence Etchells
Use the RK2_SIM() function to find y(1) and z(1) for the differential equation
in example 8A with step size h=0.1.
#5:
#6:
#7:
f ( , y , z ):=
yz
x y
xz
RK SIM y z
Exercise 8A
(1)
approximate 2 first order simultaneous equations
using the Runge Kutta order 4 method.
Use the RK2_SIM() function for different step size h
y (0) = 1, y (0) = 0 to show that
equation + 3 + 2 =
the modified Euler method has global error order h 2 .
Page 7
3/2/99
Terence Etchells
the step size; n is the number of iterations. As ever, an example will describe best how
to use this function.
DERIVE ACTIVITY 8C
(A)
Use the RK4() function in ODE_APPR.MTH to solve the following system of
first order differential equations with the given initial values;
dw
dy
dz
= x w + y,
= wx + y + z ,
= wxyz
dx
dx
dx
where x 0 = 0, w0 = 1, y 0 = 2, z 0 = 3, h=0.1 and n=10.
#1:
#2:
#3:
#4:
(B)
Page 8
3/2/99
Terence Etchells
dy
=z
dx
dz
= e x sin x 5z 6 y
dx
Author
#6:
[ z , e x sin x 5z 6 y ]
[ x , y, z]
#7:
#8:
[0,0,1]
RK(#6, #7, #8, 01
. , 20)
#9:
approX #9
We now have a large matrix of points for x, y and y , if we wish to plot x
against y then we need to extract the first two columns.
When
ODE_APPR.MTH
is
loaded
a
function
called
EXTRACT_2_COLUMNS(m,C1,C2) is entered into DERIVEs function
memory, this extracts columns C1 and C2 from the matrix m. We can use this
to extract the first two columns of the matrix in expression 10 to produce a
plot of the solution.
Author
#11: Extract_2_Columns(#10,1,2)
approX #11
Plot, Beside, at 40, #12
This particular differential equation can, in fact, be solved analytically. We can
use the utility file ODE2.MTH which contains the function
DSOLVE 2 _ IV ( p, q , r , x , x 0, y 0, v 0) that solves second order differential
equations of the form y + p( x ) y + q ( x ) y = r ( x ) with initial values y ( x 0) = y 0
and y ( x 0) = v 0 (where possible). We shall use this function here!
Transfer Load Utility ODE2.MTH
#13: DSOLVE 2 _ IV (5, 6, e x sin x , x ,0,0,1)
Simplify #13
Plot #14 Scale x: 0.5 y: 0.05
The following screen dump demonstrates the DERIVE output
Page 9
3/2/99
Terence Etchells
Page 10
3/2/99
Terence Etchells
see how close we get to the target, we then adjust the initial gradient until we hit the
target. There you have it, the Shooting Method as employed by the Artillery for
centuries. Lets see this method in action!
DERIVE ACTIVITY 8D
We will use the example y + 3 y + 2 y = 5cos x where y(0) = 0 and y(1) = 0 .
Recasting the second order differential equation into two simultaneous first order
differential equations we have:
y = z
z = 3z 2 y 5cos x
Transfer Load the Utility File ODE_APPR.MTH
The initial value for the position is (0,0)and we will take the first guess at the initial
value of the gradient to be 1, this will mean that we have an initial starting vector of
[0,0,1]. If we use a step size of h=0.1, we will need 10 iteration to reach (1,0)
#1:
#2:
Author
[ z ,3z 2 y 5 cos x ]
RK (#1,[ x , y , z ],[0,0,1],0.110
, )
approX #2
we get
Page 11
3/2/99
Terence Etchells
From inspection, we can see that we have missed the mark but not by a great deal. A
picture would be helpful at this stage to see what happened. The function
EXTRACT_2_COLUMNS(m,C1,C2) was automatically loaded with the utility file
ODE_APPR.MTH. If we now employ this function on #3 we can the plot our attempt
at the solution.
Author
#4:
EXTRACT _ 2 _ COLUMNS (#3,1,2)
approX #4
Plot, Beside, at Column 40 #5 Scale x:0.25 y:0.25 Centre (0,0.5)
resulting in
Intuitively, the initial gradient needs to be much higher. Edit #3 by highlighting it,
Author F3 and adjust the starting vector to [0,0,3], using a starting gradient of 3
instead of 1. approX and extract the first two columns and plot (as before) to give
Page 12
3/2/99
Terence Etchells
As we can see the initial gradient was still not high enough, so we repeat the whole
process with a starting vector [0,0,4].
Page 13
3/2/99
Terence Etchells
As you can see we have now over shot the mark, so our starting gradient is in the
region [3,4]. Once we know an interval in which the initial gradient lies we can use
Linear Interpolation to find a more accurate starting gradient.
When we used 3 as a starting gradient we had -0.185722 as our value for y(1) and
when we used 4 as a starting gradient we had 0.0468179 as our value for y(1) . Using
the method of linear interpolation with approximate values
h
1
=
h 0.798710 . Which implies that a
0185772
.
0.185772 + 0.0468179
starting value for the gradient of 3.79871 should be very close to the mark!
we have
Page 14
3/2/99
Terence Etchells
Finally, we will solve this equation analytically (only because we can) a see what the
analytical solution looks like.
#n:
Of course, the luxury of such verification is not always available, but it demonstrates
the accuracy of the method.
Exercise
Solve the following boundary value second order differential equations.
1.
2.
Page 15
3/2/99