Using ODEs in MATLAB
Prepared By: Nahla Al Amoodi17th September 2007
ODEs
The initial value problem for an ordinary
differential equation involves finding a
function y(t) that satisfies:
With the initial condition y(t0)=y0
A numerical solution generates a sequence of values for the
independent variable, and a corresponding sequence of values for
the dependent variable, such that each yn approximates the solution
at tn.
Euler’s Method
Works mainly for simple first order differential
equations.
Consider the following equation:
and y(0) given
This differential can be replaced with its
Newton quotient:
),( yxfdx
dy
Euler’s Method
),()()(
yxfh
xyhxy
)1(),()()( yxhfxyhxy
),(1 iiii yxhfyy
Rearranging:
Developing an iterative scheme:
Where )( ii xyy
Euler’s Method
such that at the
beginning of step i,
Suppose that the ode is to be integrated over the
interval from x=a to x=b. This interval is divided
into m steps of length h,
h
abm
Where )( ii xyy
)0()1( 1 yyandhixi
Euler’s Method
The main disadvantage of this method
is that it is dependant on the step size
m. Errors are greatly reduced if the
step size is increased.
Example: Bacteria Growth
A colony of coliform bacteria is
multiplying at the rate of r=0.8. If the
initial count shows 1000 colonies,
determine how many colonies exists in
10 hours.
Solution
Defining:
N as the colony size.
r as the growth rate.
t as the time.
The rate equation can be given as:
1000)0( NrNdt
dN
Numerical solution by applying Euler’s
Algorithm:
Analytical solution; an approximate of the
exact solution:
iii rNhNN 1
rteNtN )0()(
Solving by Euler’s method in
MATLAB
The following code can be used to solve using Euler’s method and to compare with the exact solution for error observation:
h=0.5; %defining the interval length
r=0.8;
a=0;
b=10;
m=(b-a)/h; %estimating the number of steps
N(1)=1000; %N(0)
Euler’s code (cont)
t=a:h:b; %defining the interval with the step h
for i=1:m
N(i+1)=N(i)+r*h*N(i); %applying Euler's estimation
end
Nex=N(1)*exp(r*t); %calculating the exact answer from the analytical solution
disp([t' N' Nex'])
plot(t,N),xlabel('Hr'),ylabel('Bacteria'), title('Bacteria Growth')
Notice the huge error between the
numerical and the analytical solution.
The error can be reduced by reducing
h of the numerical solution.
ODE built-in solvers in
MATLAB
ODE built in solvers depend on the
type and complexity of the system
being solved and the accuracy
needed.
The frequently used solvers are the
ode23 and ode45 referring to
second/third order and fourth/fifth
order truncations of the tailor series
respectively.
To use these solvers a number of easy
steps must be clearly defined to MATLAB:
1. Create a function in an m file to define the
right hand side of the equation to be
solved.
2. Determine the interval length for the
independent variable or tspan vector
3. Enter the initial conditions n0.
4. Call the solver to obtain the solution by
typing the following command
[t,y]=ode23(@functionName, tspan, n0)
The left hand side of the above expression
is the output argument containing two
vectors; t the independent variable and y the
dependant variable. Other solvers use
similar syntax.
Example: Bacteria Growth-
Solving a first order equation
Define the function in an m file called
bacrate.m
function y=f(t,N)
y=0.8*N;
In another m-file or in command
window type the following:
a=0;
b=10;
n0=1000; %N(0)
tspan=[a b]; % defining the interval of integration
[t,N]=ode23(@bacrate, tspan, n0)
Nex=n0*exp(0.8*t); %calculating the exact answer from the analytical solution
disp([t' N' Nex'])
plot(t,N,'*'),xlabel('Hr'),ylabel('Bacteria')
, title('Bacteria Growth')
hold on
plot(t,Nex,'r'),hold off
Second order equations
The first step in solving a second (or
higher) order ordinary differential
equation in MATLAB is to write the
equation as a first order system.
Suppose y1 (x), y1’ (x), & y1
’’ (x) are
functions of x and y.
Setting these equations into a first
order system and defining initial
conditions:
y(x)= y1(x) y(0)=a
y’(x)=y2 (x), and y’(0)=b
y’’(x)=y2’ (x)
The second step is to enter the
derivative equations as column vectors
in MATLAB, then call the solver to
obtain the solution.
Example
Solve the following second order ODE
using ode45 solver:
At y(0) = 2 and t at the range
y'(0) = 4 0 to 5
Then plot t versus y.
teyyy ''' 3
Step 1-Set to a 1st order ODE
Set y=y1
y’=y2
y’’=y2’
This results in the following system:
Rearranging:
teyyy
yy
12
'
2
2
'
1
3
2
'
112
'
2 3 yyyyey t
Step 2: Create m-files
In an m-file enter the derivative
equations as a column vector and
save it:
function dy=secondode(t,y)
dy=[y(2);exp(t)-3*y(2)+y(1)];
In the command window call the
solver:
[t,y]=ode45('secondode',[0,5],[2,4]); %
having tspan=[0 5] and initial
conditions y(0)=2, y’(0)=4
plot(t,y)
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 50
10
20
30
40
50
60
70
EXERCISES
A fluid of constant density starts to flow
into an empty and infinitely large tank
at 8 L/s. A valve regulates the outlet
flow to a constant 4L/s. Derive and
solve the differential equation
describing this process over a 100
second interval.
Solving systems of ODEs
Systems of ODEs can be entered in an
m-file using the same procedure of 2nd
order ODE, however the initial values
are entered as a row vector.
System of 1st oder ODEs will be
discussed in this tutorial.
Solving systems of ODEs
Solve the following system:
At x(0)=-8, y(0)=8, z(0)=27, then plot z
versus x.
3
224
3
8
1010
xyzdt
dz
xzydt
dy
yxdt
dx
Solution
Set x as g(1)
y as g(2)
z as g(3)
Then enter the ODEs in an m-file:
function dg= lorenz(t,g)
dg=[-10*g(1) + 10*g(2); -g(2) - g(1)*g(3); -
8/3*g(3) + g(1)*g(2) – 224/3];
In the command window type:
>>tspan=[0,50];
>>g0=[-8;8;27];
>>[t,g]=ode45(@lorenz,tspan,g0);
>>plot(g(:,1),g(:,3))
-20 -15 -10 -5 0 5 10 15 20-30
-20
-10
0
10
20
30Z versus g
g
z
Stiffness
ODEs methods discussed use a step
value h over a given interval to
estimate a numerical solution for the
ode.
For some odes even such solvers
would require a very small
(infinitesimal) step value to converge.
Such equations are called “stiff”.
MATLAB has built-in solvers to deal
with stiff odes such as ode15s,
ode23s.
For a system of odes stiffness may
also occur if one or more of the
equations is converging too fast while
another equation (or more) is
converging too slowly.
Passing Additional Parameters
Letter parameters can be written to
generalize the model represented by
odes. Consider the system:
having
x(0)=105
y(0)=8
p=0.4, q=0.04
r=0.02, s=2
syrxydt
dy
qxypxdt
dx
Create an m-file by entering the ODEs:
function dx=ques(t,x,p,q,r,s)
dx=[p*x(1)-q*x(1)*x(2); r*x(1)*x(2)-
s*x(2)];
In another mfile or in the command
window type the following
p=0.4;q=0.04;r=0.02;s=2;
tspan=[0 10];
X0=[105;8];
[t,x]=ode23(@ques,tspan,X0,[],p,q,r,s);
plot(t,x)
xlabel('t')
ylabel('x,y')
0 1 2 3 4 5 6 7 8 9 100
20
40
60
80
100
120
t
x,y
Note that the term [] have been used.
The reason being the more general
format of calling a solver:
[t,x]=solver(handle, tspan, initial
conditions, options, additional
parameters)
When using additional parameters, the
above format must be used and
‘options’ can’t be omitted.
Using ‘[]’ will tell MATLAB to hold the
place for the options parameter, but
since nothing has been defined; the
default MATLAB options will be used
to obtain the solution.