Numsol Ode
Numsol Ode
Numsol Ode
1
y(0) = 1 (4)
y(5) = 0.4 (5)
The solution should satisfy these boundary values.
We will not study boundary value problems. We will study
initial value problems which are easier to handle.
3 Euler’s method
Let us solve the following differential equation:
dy
= f (x, y) (11)
dx
with the initial value y(x0 ) = y0 .
x0 is the initial point and we need the value of the unknown function at
xN . The points are
x0 , x1 , x2 , x3 , ..., xk−1 , xk , xk+1 , ...xN (12)
2
Thus we have
xk = x0 + kh (13)
xN −x0
where h = N
is the spacing between the points and k = 0, 1, 2, ..., N .
The formula for the Euler’s method is
DERIVATION:
Taylor expansion of y around xk :
dy
y(x) = y(xk ) + (x − xk ) |x + ... (15)
dx k
dy
From the equation, we have dx
= f (x, y). So
3
4.2 Fourth order RK (RK4)
h
yk+1 = yk + (F1 + 2F2 + 2F3 + F4 ) (20)
6
where
F1 = f (xk , yk ) (21)
h h
F2 = f xk + , yk + F1 (22)
2 2
h h
F3 = f xk + , yk + F2 (23)
2 2
F4 = f xk + h, yk + hF3 (24)
The formula (20) is a weighted average of four slopes. The midpoint slopes
F2 and F3 have greater weight.
**********************************************************************
EXERCISE 1:
dy
=x+y (25)
dx
with the initial value y(0) = 1. Find y(5) using 100 steps. Use a) Euler’s
method, b) RK2, c) RK4.
EXACT RESULT: y(5)=290.8263182
1 % S o l v e dy/dx=x+y
2 % u s i n g Euler ' s method
3
4 clear all
5 clc
6
10 %Values :
11 x i n i t i a l =0;
12 y i n i t i a l =1;
13 x f i n a l =5;
14
4
15 %Number o f s t e p s :
16 N=100;
17
18 h=( x f i n a l = x i n i t i a l ) /N;
19
20 %Prepare th e v e c t o r s :
21 x v a l u e s=l i n s p a c e ( x i n i t i a l , x f i n a l ,N) ;
22 y v a l u e s=z e r o s ( 1 , 1 0 0 ) ;
23
24 %S e t t h e i n i t i a l v a l u e :
25 y v a l u e s ( 1 )=y i n i t i a l ;
26
32 %P r i n t y v a l u e a t x=x f i n a l :
33 d i s p ( y v a l u e s (N) )
34 %P l o t t he s o l u t i o n :
35 plot ( xvalues , yvalues )
**********************************************************************
d2 y(t) dy(t)
2
+b + ω 2 y(t) = 0 (26)
dt dt
with b = 0.3 and ω = 0.7. The initial values are: y(0) = 1 and y ′ (0) = 1
where the prime denotes the first derivative. For b = 0, we get the harmonic
motion.
Our numerical methods can work with first order equations. Thus, we
will write this second order equation as two first order equations and solve
them simultaneously.
5
Define
dy
≡z (27)
dt
and use this in the original equation
dz
+ bz + ω 2 y = 0 (28)
dt
Now we have two equations, namely
dz
= −bz − ω 2 y (29)
dt
dy
≡z (30)
dt
which will be solved simultaneously.
EXERCISE 2:
As a coding example, let us solve this system with RK4 from t = 0 to
t = 30 with 1000 points and plot the behavior.
1. Define two functions:
f(t,y,z)=z
g(t,y,z)=-b*z-(w*w)*y
2. Set
w=0.7
b=0.3
tinitial=0
yinitial=1
zinitial=1
tfinal=30
numpoints=1000
h=(tfinal-tinitial)/numpoints
3. Define three vectors, all with 1000 elements:
tvalues:
tvalues(first element)=tinitial
6
tvalues(last element)=tfinal
yvalues:
yvalues(first element)=yinitial
zvalues:
zvalues(first element)=zinitial
4. Enter a loop for the method:
1 f o r i=s t a r t : s t o p
2 t=t v a l u e s ( i ) ;
3 y=y v a l u e s ( i ) ;
4 z=z v a l u e s ( i ) ;
5
6 F1y=f ( t , y , z ) ;
7 F1z=g ( t , y , z ) ;
8