Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Numerical Methods

to Solve Ordinary
Differential Equations
by
Agus Kartono

Ordinary Differential Equations


Most often, systems of differential equations
can not be solved analytically.
Therefore, it is important to be able to
approach the problem in other ways.
Today there are numerical methods that
produce
numerical
approximations
to
solutions of differential equations.
Here, we introduce the oldest and simplest
such method, originated by Euler about
1768.

Leonhard Euler (1707-1783).

EULER Methods
Euler methods are simple methods of
solving first-order ODE, particularly suitable
for quick programming because of their
great simplicity, although their accuracy is
not high.
Euler
methods include three versions,
namely:
1) forward Euler method
2) modified Euler method
3) backward Euler method

Taylor Series
Often a Taylor series is used to approximate
a solution which is then truncated.
Taylor Series:

2
3

xi 3
f
x
f
1
2
i
f xi 1 f xi f xi h
h
h ...
2!
3!

df
f xi
dx
1

2
d
f
2

f xi 2
dx

x xi

x xi

Forward Euler Method

The forward Euler method for y ' isf x, y


derived by rewriting the forward difference
approximation:

y xi 1 y xi y ' xi h y xi f xi h
or

y xi 1 y xi f xi , yi h

where y ' xi f xi , y i is used.

Steps
for
implementation

MATLAB

The typical steps of forward Euler's method are given


below:
Step 1. define f(x, y)
Step 2. input initial values x0 and y0
Step 3. input step sizes h and number of steps n
Step 4. calculate x and y:
for i=0:n
x(i+1)=x(i)+h
y(i+1)=y(i)+h*f(x(i),y(i))
end
Step 5. output x and y
Step 6. end

Example:

As an application, consider the following


initial value problem:
dy
x
y ' f , x, y
dx
y

y 0 1

which was chosen because we know the


analytical solution and we can use it for
check. Its exact or analytical solution is
found to be:

y x x 2 1

m file in MATLAB
function [x,y]=euler_forward(f,xinit,yinit,xfinal,n)
%
%
%
%

Euler approximation for ODE initial value problem


Euler forward method
File prepared by Agus Kartono
Calculation of h from xinit, xfinal, and n

h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x = zeros(n+1,1); y = zeros(n+1,1);
x(1) = xinit; y(1) = yinit;
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
y(i+1)=y(i)+h*f(x(i),y(i));
end
plot(x,y);

Running m file in MATLAB


function f = f(x,y)
f=x/y;

>>[x,y]=euler_forward(@f,0,1,1,10)

Modified Euler Method


First, the modified Euler method is more
accurate than the forward Euler method.
Second, it is more stable. It is derived by
applying the trapezoidal rule to the solution
of y ' f x, y .

y xi 1

h
y xi f xi 1 , yi 1 f xi , yi
2

Steps for MATLAB


implementation
The

typical steps of modified Euler's method are given


below:
Step 1. define f(x, y)
Step 2. input initial values x0 and y0
Step 3. input step sizes h and number of steps n
Step 4. calculate x and y:
for i=1:n
x(i+1)=x(i)+h
ynew= y(i)+h*f(x(i),y(i))
y(i+1)=y(i)+ h/2*(f(x(i),y(i))+f(x(i+1),ynew));
end
Step 5. output x and y
Step 6. plot (x,y)

m file in MATLAB
function [x,y]=euler_modified(f,xinit,yinit,xfinal,n)
%
%
%
%

Euler approximation for ODE initial value problem


Euler modified method
File prepared by Agus Kartono
Calculation of h from xinit, xfinal, and n

h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x = zeros(n+1,1); y = zeros(n+1,1);
x(1) = xinit; y(1) = yinit;
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
ynew=y(i)+h*f(x(i),y(i));
y(i+1)=y(i)+(h/2)*(f(x(i),y(i))+f(x(i+1),ynew));
end
plot(x,y);

Backward Euler Method

The backward Euler method is based on the


backward difference approximation and
written as:

y xi 1 y xi hf xi 1 , yi 1

The accuracy of this method is quite the


same as that of the forward Euler method.

Steps for MATLAB


implementation

The typical steps of backward Euler's method are given


below:
Step 1. define f(x, y)
Step 2. input initial values x0 and y0
Step 3. input step sizes h and number of steps n
Step 4. calculate x and y:
for i=1:n
x(i+1)=x(i)+h
ynew= y(i)+h*f(x(i),y(i))
y(i+1)=y(i)+h*f(x(i+1),ynew)
end
Step 5. output x and y
Step 6. plot (x,y)

m file in MATLAB
function [x,y]=euler_backward(f,xinit,yinit,xfinal,n)
%
%
%
%

Euler approximation for ODE initial value problem


Euler backward method
File prepared by Agus Kartono
Calculation of h from xinit, xfinal, and n

h=(xfinal-xinit)/n;
% Initialization of x and y as column vectors
x = zeros(n+1,1); y = zeros(n+1,1);
x(1) = xinit; y(1) = yinit;
% Calculation of x and y
for i=1:n
x(i+1)=x(i)+h;
ynew=y(i)+h*f(x(i),y(i));
y(i+1)=y(i)+h*f(x(i+1),ynew);
end
plot(x,y);

Runge-Kutta Algorithms

Second-Order Runge-Kutta:
Estimation of the slope is the derivative at
the mid-point between t and t+t. Figure in
below illustrates this idea. The equations
are then:

k1 f x n , t n t
k1
t
k 2 f xn , t n t
2
2

x t n 1 x t n k 2

Example:

As an application, consider the following


initial value problem:
dx
t
x'
f x,, t
dt
x

x 0 1

which was chosen because we know the


analytical solution and we can use it for
check. Its exact or analytical solution is
found to be:
x t t 2 1

Figure: Principle of the Second-order RungeKutta Method

Steps for MATLAB


implementation
The

typical steps of Second-Order Runge-Kutta 's method


are given below:
Step 1. define f(t, x)
Step 2. input initial values x0 and t0
Step 3. input step sizes t = h and number of steps m
Step 4. calculate x and t:
for n=1:m
t(n+1)=t(n)+h
k1= h*f(x(n),t(n))
k2= h*f(x(n)+k1/2,t(n)+h/2)
x(n+1)=x(n)+k2
end
Step 5. output t and x
Step 6. plot (t,x)

m file in MATLAB
function rk2_new
%
%
%
%

Runge-Kutta approximation for ODE initial value problem


Second-order Runge-Kutta method
File prepared by Agus Kartono
Calculation of h from tinit, tfinal, and m

tinit = 0;
xinit = 1;
tfinal = 1;
m = 10;
h=(tfinal-tinit)/m;
% Initialization of x and t as column vectors
x = zeros(m+1,1); t = zeros(m+1,1);
x(1) = xinit; t(1) = tinit;
% Calculation of x and t
for i=1:m
t(i+1)=t(i)+h;
k1=h*frk(x(i),t(i));
k2=h*frk(x(i)+h/2,t(i)+k1/2);
x(i+1)=x(i)+k2;

The formula for the fourth order


Runge-Kutta method (RK4)

function rungekutta
h = 0.5;
t = 0;
w = 0.5;
fprintf('Step 0: t = %12.8f, w =
%12.8f\n', t, w);
for i=1:4
k1 = h*frk4(t,w);
k2 = h*frk4(t+h/2, w+k1/2);
k3 = h*frk4(t+h/2, w+k2/2);
k4 = h*frk4(t+h, w+k3);
w = w + (k1+2*k2+2*k3+k4)/6;
t = t + h;
fprintf('Step %d: t = %6.4f, w =
%18.15f\n', i, t, w);
end
function v =
frk4(t,y)
v = y-t^2+1;

function rk45
epsilon = 0.00001;
h = 0.2;
t = 0;
w = 0.5;
i = 0;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', i, t,
w);
while t<2
h = min(h, 2-t);
k1 = h*f(t,w);
k2 = h*f(t+h/4, w+k1/4);
k3 = h*f(t+3*h/8, w+3*k1/32+9*k2/32);
k4 = h*f(t+12*h/13, w+1932*k1/21977200*k2/2197+7296*k3/2197);

k5 = h*f(t+h, w+439*k1/216-8*k2+3680*k3/513845*k4/4104);
k6 = h*f(t+h/2, w-8*k1/27+2*k23544*k3/2565+1859*k4/4104-11*k5/40);
w1 = w + 25*k1/216+1408*k3/2565+2197*k4/4104k5/5;
w2 = w + 16*k1/135+6656*k3/12825+28561*k4/564309*k5/50+2*k6/55;
R = abs(w1-w2)/h;
delta = 0.84*(epsilon/R)^(1/4);
if R<=epsilon
t = t+h;
w = w1;
i = i+1;
fprintf('Step %d: t = %6.4f, w = %18.15f\n', i,
t, w);
h = delta*h;
function v = frk45(t,y)
else
v = y-t^2+1;
h = delta*h;
end
end

You might also like