Sie sind auf Seite 1von 22

Table of Contents

Objectives.............................................................................................................. 1
Introduction........................................................................................................... 1
Calculation............................................................................................................. 1
Results................................................................................................................... 8
Discussions.......................................................................................................... 14
Exercise............................................................................................................... 15
Conclusion........................................................................................................... 16
References........................................................................................................... 16

Objectives
1. To solve numerical methods for initial value problem in ordinary differential equation
by using excel spreadsheet.
2. To solve the ODE by Euler method, Midpoint method, Runge-Kutta Order Four (RK4),
Predictor-Corrector method and Extrapolation method.

Introduction
In this lab, the numerical methods for initial value problem in ordinary differential
equation will be implemented using Excel spreadsheet. Specifically, Euler method, midpoint
method, Runge-Kutta order four (RK4), predictor-corrector method and extrapolation
methods will be applied in solving the ODE. These methods can be applied in solving a
system of ODE.

Calculation
y te3t 2 y y 0 0 y 1 ?
Question:
,
,
Euler method
y i+1= yi +hf ( x i , yi )
Using h = 0.05,
For y1
y 1= y 0 +hf ( t 0 , y 0 ) =0+0.05 f ( 0,0 )=0+ 0.05[0 e 02 ( 0 ) ]=0
For y2
y 2= y 1 +hf ( t 1 , y 1 )=0+ 0.05 f ( 0.05,0 )=0+0.05 [0 .05 e 3 0.052 ( 0 ) ]=0.002905

Midpoint method

h
K 1=f ( xi , y i ) , y m = y i + K 1
2
h
K 2=f x i + , y m , y i+1= y i+ h K 2
2

Using h = 0.05,
For y1
K 1=f ( t 0 , y 0 )=f ( 0,0 )=[0 e0 2 ( 0 ) ]=0
h
0.05
y m= y 0 + K 1=0+
( 0 )=0
2
2
0.025 e3 0.025 2 ( 0 )
=0.026947
h
K 2=f t 0 + , y m =f ( 0,0 )=
2

y 1= y 0 +h K 2=0+0.05 ( 0.0269 )=0.001347


For y2
K 1=f ( t 1 , y 1) =f ( 0.05,0 )=[0 .05 e 3 0.052 ( 0.00135 ) ]=0.055397
h
0.05
y m= y 1 + K 1 =0.00135+
( 0.0554 )=0.002732
2
2
h
K 2=f t 1+ , y m =f ( 0.075,0 .00274 ) =[ 0.075 e3 0.0752 ( 0.00274 ) ]=0.08846
2

y 2= y 1 +h K 2=0.00135+ 0.05 ( 0.0884 )=0.00577

Runge-Kutta order four (RK4)

h
K 1=f ( xi , y i ) , y m 1 = y i+ K 1
2
h
h
K 2=f x i + , y m 1 , y m 2= y i + K 2
2
2

h
K 3=f x i + , y m 2 , y m 3= y i +h K 3
2

h
K 4 =f ( x i+1 , y m 3 ) , y i +1= y i + (K 1+2 K 2 +2 K 3+ K 4 )
6
Using h = 0.05,
For y1
K 1=f ( t 0 , y 0 )=f ( 0,0 )=[0 e0 2 ( 0 ) ]=0
h
0.05
y m 1= y 0 + K 1 =0+
( 0 ) =0
2
2
h
K 2=f t 0 + , y m 1 =f ( 0.025,0 )=[0 .025 e3 0.0252 ( 0 ) ]=0.026947
2

h
0.05
y m 2= y 0 + K 2 =0+
( 0.0269 ) =0.00 0674
2
2
h
K 3=f t 0 + , y m 2 =f ( 0.025,0 .000674 ) =[0 .025 e3 0.0252 ( 0.000674 ) ]=0.0256
2

y m 3= y 0 +h K 3 =0+0.05 ( 0.0256 ) =0.00128


K 4 =f ( t 1 , y m 3 ) =f ( 0.05,0 .00128 )=[0.05 e3 0.05 2 ( 0.00128 ) ]=0.055532
h
0.05
y 1= y 0 + ( K 1+2 K 2 +2 K 3+ K 4 ) =0+
( 0+ ( 2 0.026947 ) + ( 2 0.0256 ) +0.055532 ) =0.001339
6
6
For y2
4

K 1=f ( t 1 , y 1) =f ( 0.05,0 .001339 )=[0.05 e3 0.052 ( 0.001339 ) ]=0.055415


h
0.05
y m 1= y 1 + K 1=0.001339+
( 0.055415 )=0.002724
2
2
h
K 2=f t 1+ , y m 1 =f ( 0.075,0 .002724 )=[0 .075 e3 0.0752 ( 0.002724 ) ]=0.088476
2

h
0.05
y m 2= y 1 + K 2=0.001339+
( 0.088476 )=0.00355
2
2
h
K 3=f t 1+ , y m 2 =f ( 0.075,0 .00355 )=[ 0 .075 e 3 0.0752 ( 0.00355 ) ]=0.086823
2

y m 3= y 1 +h K 3=0.001339+ 0.05 ( 0.086823 )=0.00568


K 4 =f ( t 2 , y m 3 ) =f ( 0.1,0 .00568 )=[0 .1e 3 0.1 2 ( 0.00568 ) ]=0.123626
h
y 2= y 1 + ( K 1 +2 K 2 +2 K 3+ K 4 )
6

0.001339+

0.05
( 0.055415+ ( 2 0.088476 )+ ( 2 0.086823 ) +0.123626 )
6

0.005752
Predictor-Corrector method
K 1=f ( xi , y i ) , y pi +1= y i +h K 1

K 2=f ( x i+1 , y

p
i +1

) , y c i+ 1= y i+ 2 (K 1 + K 2 )

Using h = 0.05,
For y1
K 1=f ( t 0 , y 0 )=f ( 0,0 )=[ 0 e3 0 2 ( 0 ) ]=0
5

y 1= y 0 +h K 1=0+0.05 ( 0 )=0
K 2=f ( t 1 , y p1 )=f ( 0.05,0 )=[ 0.05 e 3 0.052 ( 0 ) ]=0.058092
h
0.05
c
y 1= y 0 + ( K 1 + K 2 ) =0+
( 0+ 0.058092) =0.001452
2
2
For y2
K 1=f ( t 1 , y 1) =f ( 0.05,0 .00145 )= [ 0.05 e3 0.052 ( 0.001452 ) ]=0.055187
p

y 2= y 1 +h K 1=0.00145+ 0.05 ( 0.055187 )=0.004212


K 2=f ( t 2 , y p2 )=f ( 0.1,0 .004212 )=[ 0 .1 e3 0.12 ( 0.004212 ) ]=0.126563
h
0.05
c
y 2= y 1+ ( K 1 + K 2 )=0.001452+
( 0.055187+0.126563 )=0.005996
2
2

Extrapolation method
h
h
K 1=f ( xi , y i ) , x m 1=x i + , y = y i + K 1
4 m1
4

h
h
K 2=f ( xm 1 , y m 1 ) , x m 2=x i + , y m 2= y i + K 2
2
2

K 3=f ( x m 2 , y m 2 ) , x m 3 =xi +

3h
h
, y m3= y m 1+ K 3
4
2

h
K 4 =f ( x m 3 , y m 3 ) , x m 4 =xi + h , y m 4= y m 2 + K 4
2

K 5=f ( x m 4 , y m4 ) , y i+1=

y m3 + y m 4 h
+ K5
2
8

Using h = 0.05,
6

For y1
3 0
K 1=f ( t 0 , y 0 )=f ( 0,0 )=[ 0 e 2 ( 0 ) ]=0

h
0.05
t m 1=t 0 + =0+
=0.0125
4
4
h
0.05
y m 1= y 0 + K 1=0+
( 0 )=0
4
4
K 2=f ( t m 1 , y m 1 )=f ( 0.0125,0 )=[ 0.0125 e 3 0.0125 2 ( 0 ) ]=0.012978
h
0.05
t m 2=t 0 + =0+
=0.025
2
2
h
0.05
y m 2= y 0 + K 2 =0+
( 0.013 ) =0.000324
2
2
K 3=f ( t m 2 , y m 2 )=f ( 0.025,0.000324 )=[ 0.025 e 3 0.025 2 ( 0.000325 ) ] =0.026298

t m 3=t 0+

3h
3 0.05
=0+
=0.0375
4
4

h
0.05
y m 3= y m 1 + K 3 =0+
( 0.0263 ) =0.000657
2
2
K 4 =f ( t m 3 , y m 3 ) =f ( 0.0375,0 .000657 ) =[ 0.0375 e3 0.03752 ( 0.000657 ) ]=0.04065
t m 4=t 0 +h=0+0.05=0.05
h
0.05
y m 4 = y m 2+ K 4 =0.000325+
( 0.04065 ) =0.001341
2
2
K 5=f ( t m 4 , y m 4 )=f ( 0.05,0 .00134 )=[ 0.05 e3 0.05 2 ( 0.00134 ) ]=0.05541

y 1=

y m 3+ y m 4 h
0.000657+0.001341 0.05
+ K 5=
+
( 0.05541 )=0.001345
2
8
2
8
7

For y2
3 0.05
K 1=f ( t 1 , y 1) =f ( 0.05,0 .001345 )= [ 0.05 e
2 ( 0.001345 ) ]=0.055401

h
0.05
t m 1=t 1 + =0.05+
=0.0625
4
4
h
0.05
y m 1= y 1 + K 1=0.00135+
( 0.055401) =0.002038
4
4
K 2=f ( t m 1 , y m 1 )=f ( 0.0625,0.002038 ) =[ 0.0625 e3 0.0625 2 ( 0.002038 ) ]=0.071314
h
0.05
t m 2=t 1 + =0.05+
=0.075
2
2
h
0.05
y m 2= y 1 + K 2=0.001345+
( 0.071314 ) =0.003128
2
2
K 3=f ( t m 2 , y m 2 )=f ( 0.075,0.003128 )=[ 0.075 e 3 0.0752 ( 0.003128 ) ]=0.087668

t m 3=t 1 +

3h
3 0.05
=0.05+
=0.0875
4
4

h
0.05
y m 3= y m 1 + K 3 =0.00204+
( 0.0875 )=0.00423
2
2
K 4 =f ( t m 3 , y m 3 ) =f ( 0.0875,0 .00423 )= [ 0.0875 e3 0.08752 ( 0.00423 ) ] =0.105306
t m 4=t 1 +h=0.05+0.05=0.1
h
0.05
y m 4 = y m 2+ K 4 =0.003128+
( 0.105 ) =0.005761
2
2
K 5=f ( t m 4 , y m 4 )=f ( 0.1,0 .005761 )=[ 0.1 e3 0.12 ( 0.005761 ) ]=0.123464

y 2=

y m 3+ y m 4 h
0.00423+0.005761 0.05
+ K 5=
+
( 0.123464 ) =0.005767
2
8
2
8
8

Exact solution
dy
=t e 3 t2 y , y ( 0 )=0, y ( 1 )=?
dt
3t

dy=t e dt2 ydt


dy = t e 3 t dt 2 ydt

u=t dv=e3 t dt

Let

1 3t
du=dt v= e
3

Therefore,

3t

t e dt= udv =uv vdu


1
1
t e 3 t e 3t dt
3
3
1 3t 1 3t
te e
3
9

dy = t e 3 t dt 2 ydt
1 3t 1 3t
y= t e e 2ty+ C
3
9

y(0)=0
1
1
0= (0) e3 ( 0) e3 ( 0)2( 0) y +C
3
9

10

1
0=0 (1)0+C
9

C=

1
9

11

1
1
1
y= t e3 t e 3 t 2ty+
3
9
9

Therefore,

1
1
1
y +2 ty= t e3 t e 3 t +
3
9
9
9 y +18 ty=3 t e3 t e 3 t +1
y (9+18 t)=3 t e 3 te3 t +1

y=

3 t e 3 te3 t +1
9+ 18t

3 (1) e 3(1)e3 (1 )+1


y (1)=
9+18(1)
y=1.5249

12

Graph of y versus t (Exact Solution)


1.8
1.6
1.4
1.2
1.0

0.8
0.6
0.4
0.2
0.0
0.0

0.2

0.4

0.6

0.8

1.0

1.2

Resu
lts
Graph of y versus t (Exact Solution)

Euler

13

Graph of y vs t (Euler)
4
3.5
3
h = 0.5

2.5

h = 0.2
2

h = 0.1
h = 0.05

1.5

Exact

1
0.5
0

0.2

0.4

0.6

0.8

Graph of y versus t (Euler)


h=0.5

Percentage Difference=

|10.0427681.524855|
1.524855

100=558.60

h = 0.2
Percentage Difference=

|5.4034951.524855|
1.524855

100=254.36

h = 0.1
Percentage Difference=

|4.2172751.524855|
1.524855

100=176.57

h = 0.05
Percentage Difference=

|3.6950291.524855|
1.524855

14

100=142. 32%

1.2

Graph of y versus t (Midpoint)


5
4
4
3

h=0.5
h=0.2

h=0.1

h=0.05
Exact

2
1
1
0
0.0

0.2

0.4

0.6

0.8

1.0

1.2

Mi
dpoint
Graph of y versus t (Midpoint)
h = 0.5
Percentage Difference=

|23.1192931.524855|
1.524855

100=1416.16

h = 0.2
Percentage Difference=

|7.3608601.524855|
1.524855

100=382.73

h = 0.1
Percentage Difference=

|4.8942731.524855|
1.524855

h = 0.05
15

100=220.97

Percentage Difference=

|3.9743141.524855|
1.524855

100=160.64

Runge-Kutta order four (RK4)

Graph of y versus t (RK4)


5
4
4
3

h=0.5
h=0.2

h=0.1

h=0.05
Exact

2
1
1
0
0.0

0.2

0.4

0.6

0.8

1.0

1.2

Gr
aph of y versus t (RK4)
h = 0.5
Percentage Difference=

|23.9866551.524855|
1.524855

100=1473.04

h = 0.2
Percentage Difference=

|7.3292601.524855|
1.524855

h = 0.1

16

100=380.65

Percentage Difference=

|4.8849721.524855|
1.524855

100=220.36

h = 0.05
Percentage Difference=

|3.9720431.524855|
1.524855

100=160.49

Graph of y versus t (Predictor-Corrector)


5
4
4
3

h=0.5
h=0.2

h=0.1

h=0.05
Exact

2
1
1
0
0.0

0.2

0.4

0.6

0.8

1.0

1.2

Pre
dictor-Corrector
Graph of y versus t (Predictor-Corrector)
h = 0.5
Percentage Difference=

|36.4071691.524855|
1.524855

h = 0.2

17

100=2287.58

Percentage Difference=

|8.0085641.524855|
1.524855

100=425.20

h = 0.1
Percentage Difference=

|4.9988871.524855|
1.524855

100=227.83

h = 0.05
Percentage Difference=

|3.9951661.524855|
1.524855

100=162.00

Extrapolation

Graph of y versus t (Extrapolation)


5
4
4
3

h=0.5
h=0.2

h=0.1

h=0.05
Exact

2
1
1
0
0.0

0.2

0.4

0.6

0.8

Graph of y versus t (Extrapolation)


h = 0.5

18

1.0

1.2

Percentage Difference=

|24.1121311.524855|
1.524855

100=1481.27

h = 0.2
Percentage Difference=

|7.3621481.524855|
1.524855

100=382.81

h = 0.1
Percentage Difference=

|4.8914611.524855|
1.524855

100=220.78

h = 0.05
Percentage Difference=

|3.9734341.524855|
1.524855

100=160.58

Graph of y versus t (Exact and All Method with h=0.05)


4.5
4.0
3.5
3.0
2.5
y

2.0
1.5
1.0
0.5
0.0
0.0

0.2

0.4

0.6

0.8

1.0

t
Euler

Midpoint

RK4

Predictor-Corrector

Extrapolate

Exact

Graph of y versus t (Exact and All Method with h=0.05)

19

1.2

Discussions
1) From the percentage error calculated above we can observe that as the smaller the h
value, the more accurate the answer. This fact observed from every method we used.
2) Generally, Midpoint method is better than Euler method, this method will have better
result compare to Euler method. However, we have exponential term in the equation,
so as the calculation of Midpoint method become more complicated and hence make it
inaccurate.
3) Same situation happened when we use Runge-Kutta order four method, due to the
exponential term in the equation, the result show higher percentage error although RK4
method is more accurate in general.
4) Preditor-Corrector and Extrapolation method shows almost the same result from the
graph plotted.
5) Comparing the percentage error plotted in the graph, we can observe that Euler method
have the lowest error and hence we can say that it is the most accurate method to
calculate the equation.

Exercise
Predator-Prey Problem

Graph of Number of Species versus Time


h=0.5, L(0)=450, S(0)=1000
3000
2500
2000

Number of Species

Lion

1500

Sheep

1000
500
0

50

100

Time

20

150

200

Graph of Number of Species versus Time (h=0.5)

Graph of Number of Species versus Time


h=0.1, L(0)=450, S(0)=1000
1600
1400
1200
1000

Number of Species

Lion

800

Sheep

600
400
200
0

20 40 60 80 100 120 140 160

Time

Graph of Number of Species versus Time (h=0.1)

21

Discussions
1) From the graph, we can see that the number of sheep decreases when the number of
lion increases and also vice versa.
2) We can observe that the number of sheep is changing vigorously compare to the
number of lion and the number of sheep increase rapidly when h = 0.5, while when h
= 0.1, the changes become more smooth.
3) In order to get a more accurate result, we need to decrease the step size, h, so that we
can get more data point at the same time period.

Conclusion
We can make a conclusion that for the equation

dy
3t
=t e 2 y
dt

, Euler method gives the

best accuracy. Euler method requires the least calculation (programing) effort because only 2
equation needed to get the next y value. Extrapolation method requires the most input of
formula.

References
1. Atkinson, K., Han, W., & Stewart, D. (2014). NUMERICAL SOLUTION OF ORDINARY
DIFFERENTIAL EQUATIONS (1st ed.). Iowa City, Iowa: A JOHN WILEY & SONS,
INC., PUBLICATION.
2. Efunda.com,. (2014). Numerical Solutions of Ordinary Differential Equations. Retrieved
2 December 2014, from http://www.efunda.com/math/num_ode/num_ode.cfm
3. Suli, E. (2014). Numerical Solution of Ordinary Differential Equations (1st ed.).
Mathematical Institute, University of Oxford.

22

Das könnte Ihnen auch gefallen