School of Chemical and BioMedical
Engineering
Division of Bioengineering
BG2802 Bioengineering Year 2 Lab
Lab Report
Analysis of Cyclic Fatigue Failure Data
using Least-square Regression
Author:
Yang
WANG
a aMatriculation Number: U1220560J
1 Background and Introduction . . . 2
2 Objectives . . . 2
3 Theories . . . 2
3.1 Euler’s Method . . . 2
3.2 4th order Runge-Kutta method . . . . 2
4 Experiment Procedures . . . 3
5 Results and Analysis . . . 9
5.1 Euler’s Method . . . 9
5.2 4th order Runge-Kutta Method . . . . 10
5.3 Comparison and Comments . . . 11
5.4 Epidemic Model . . . 12
6 Conclusion . . . 12
1
Background and Introduction
Solving differential equations is frequently required in handling engineering problems, however solving those equations by hands is not usually that easy. In many cases, there is no analytical solutions to some differential equations, ordinary or partial. Numerical methods to approximate the analytical solutions therefore is widely applied to get better results and simulations. In this experiment on computational methods for numerical analysis, we will use Matlab to apply Euler’s method and 4th order Runge-Kutta method for some data
approximation. By comparing the outcomes of these methods we might able to tell the advantages and limitations in both cases. The third part of this experiment is to apply these methods to model a epidemic incident. By the aforementioned methods we will get a better understanding on the numerical analysis algorithms and apply these understandings into solving real cases in the future.
2
Objectives
There are three objectives for this experiment.
First
Learn to use Matlab to compute solution of differential equations using Euler’s method and 4thorder Runge-Kutta method.
Second
Study the effect of step size used in Euler’s method and 4thorder Runge-Kutta method
have on the solution of ODEs. Third
Apply the numerical methods on a simple epidemic spreading model.
3
Theories
3.1 Euler’s Method
For a general initial value problem:
y′= dy
dt =f(t, y), y(0) =y0
for a stepping size h, the Euler’s method for approximation gives
yk+1=yk+hf(tk, yk),
k∈ {N∩[0, M−1)},
h=b−a
M
by this, a series of points can be plotted, which is the approximation of the original function curve.
3.2 4th
order Runge-Kutta method
dy
dt =f(t, y) y(0) =y0
the 4thorder Runge-Kutta method approximation uses the formula
yk+1 =yk+
by expanding with 4thorder Taylor series
yi+1=yi+
and the general solution set is
1. Matlab Program for Euler’s Method (a) Euler.m
function [tout, yout] = Euler(FunFcn, tspan, y0, ssize)
% FunFcn is the function to be solved
5 % tspan is the interval
% y0 is the initial conditions
% ssize is the step size
% Initialization
15 c l f
t0 = tspan(1); %Start point
tfinal = tspan(2); %End point
20
i f (nargin < 4), ssize = (tfinal - t0)/100; end %calculate step size if
h = ssize;
25
t = t0;
y = y0(:);
30 tout = t;
yout = y.’;
35
% The main loop
while (t < tfinal)
40 i f t + h > tfinal, h = tfinal - t; end
% Compute the slope
s1 = f e v a l(FunFcn, t, y); s1 = s1(:); % s1=f(t(k),y(k))
45
t = t + h;
y = y + h*s1; % y(k+1) = y(k) + h*f(t(k),y(k))
50 tout = [tout; t];
yout = [yout; y.’];
end;
(b) f Euler.m
function z = f(t, y)
z = (t - y)/2; %DonŠt forget the ; at the end of the line
5 return
(c) Run Euler.m
%Define the parameters
tspan = [0 3];
5 y0 = 1;
%Call the Euler solver
10
[tout, yout] = Euler(’f’, tspan, y0, h);
fi g u re(2);
15 plot(tout,yout)
t i t l e([’Euler s Method Method using h=’, num2str(h), ’ by Stephen’]);
%To show the value of yout at t = 3 in the command window
20
[m n] = s i z e(yout);
yout(m)
2. Matlab program for 4thorder Tunge-Kutta Method
(a) rk4.m
function [tout, yout] = rk4(FunFcn, tspan, y0, ssize)
% FunFcn is the function to be solved % tspan is the interval
% y0 is the initial conditions
5 % ssize is the step size
15 % We need to compute the number of steps.
dt = abs(tfinal - t0); N = f l o o r(dt/ssize) + 1;
i f (N-1)*ssize < dt N = N + 1;
20 end
% Initialize the output.
tout = zeros(N,1);
% Compute the slopes
s1 = f e v a l(FunFcn, t, y); s1 = s1(:);
s2 = f e v a l(FunFcn, t + h/2, y + h*s1/2); s2=s2(:);
s4 = f e v a l(FunFcn, t + h, y + h*s3); s4=s4(:); y = y + h*(s1 + 2*s2 + 2*s3 +s4)/6;
t = tout(k); yout(k,:) = y.’;
45 end;
(b) f rk.m
function z = f(t, y)
z = (t - y)/2; %DonŠt forget the ; at the end of the line
5 return
(c) Run rk4.m
%Define the parameters and constants
tspan = [0 3];
5 y0 = 1;
h = 1/16;
%To call the rk4 solver
10
[tout, yout] = rk4(’f’, tspan, y0, h); %Results from rk4 is stored in
fi g u re(2);
15 plot(tout,yout)
t i t l e([’Runge Kutta Method using h=’, num2str(h), ’ by Stephen’]);
%To show the value of yout at t = 3 in the command window
20
[m n] = s i z e(yout);
yout(m)
3. Epidemic Model
(a) Parameter adjustment Euler P5.m and rk4 P5.m , define new function f2.m
function [tout, yout] = Euler(FunFcn, tspan, y0, ssize)
% FunFcn is the function to be solved
5 % tspan is the interval
% y0 is the initial conditions
% ssize is the step size
10
% Initialization
15 c l f
tfinal = tspan(2); %End point
20
i f (nargin < 4), ssize = (tfinal - t0)/100; end %calculate step size if
h = ssize;
while (t < tfinal)
40 i f t + h > tfinal, h = tfinal - t; end
% Compute the slope
s1 = f e v a l(FunFcn, t, y); s1 = s1(:); % s1=f(t(k),y(k))
function [tout, yout] = rk4(FunFcn, tspan, y0, ssize)
% FunFcn is the function to be solved % tspan is the interval
% y0 is the initial conditions
5 % ssize is the step size
15 % We need to compute the number of steps.
dt = abs(tfinal - t0); N = f l o o r(dt/ssize) + 1;
i f (N-1)*ssize < dt N = N + 1;
20 end
% Initialize the output.
tout(1) = t;
% Compute the slopes
s1 = f e v a l(FunFcn, t, y); s1 = s1(:);
(b) Run Euler approximation Run Euler P5.m
%Define the parameters
tspan = [0 20];
5 y0 = 250;
h = 0.2;
%Call the Euler solver
10
[tout, yout] = Euler(’f2’, tspan, y0, h);
fi g u re(2);
15 plot(tout,yout)
t i t l e([’Euler s Method Method using h=’, num2str(h), ’ by Stephen’]);
%To show the value of yout at t = 3 in the command window
20
[m n] = s i z e(yout);
yout(m)
(c) Run 4thorder Tunge-Kutta approximation Run rk4 P5.m
tspan = [0 20];
5 y0 = 250;
h = 0.2;
%To call the rk4 solver
10
[tout, yout] = rk4(’f2’, tspan, y0, h); %Results from rk4 is stored in
fi g u re(2);
15 plot(tout,yout)
t i t l e([’Runge Kutta Method using h=’, num2str(h), ’ by Stephen’]);
%To show the value of yout at t = 3 in the command window
20
[m n] = s i z e(yout);
yout(m)
5
Results and Analysis
5.1 Euler’s Method
h t= 3
5.2 4th
order Runge-Kutta Method
h t= 3
5.3 Comparison and Comments
Obviously 4thorder Runge-Kutta Method provided a better and faster simulation than
Euler’s Method 4th
order Runge-Kutta Method
t= 20 2.3625×104 2.3702×104
5.4 Epidemic Model
6
Conclusion
This experiment practice some fundamental approximation using Matlab on Euler’s method and 4th order Runge-Kutta method. In real cases there are more complicated