Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

T X T X T X: Using Ode45 To Solve Ordinary Differential Equations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Using ode45 to solve Ordinary Differential Equations

Normal Form

A system of n differential equations in the n unknown functions x1 (t ), x2 (t ),, xn (t )


expressed as

 x1' (t )  f1 (t , x1 (t ), x2 (t ),, xn (t ))
 '
 x2 (t )  f 2 (t , x1 (t ), x2 (t ),, xn (t ))
(1) 
 
 x ' (t )  f (t , x (t ), x (t ),, x (t ))
 n n 1 2 n

then system of differential equations is in normal form.

Remark: It consists 1st order equations. Right side contains only independent variable and
unknown functions, but no derivatives of unknown functions.

A higher order equation can always be converted to an equivalent system of first order
equations.

How to convert an nth order differential equation

y ( n ) (t )  f (t , y, y ' ,, y ( n1) )

We introduce the sequence of derivatives of y as additional unknowns:

x1 (t )  y(t ), x2 (t )  y' (t ), x3 (t )  y' ' (t ),, xn (t )  y ( n1) (t )

Thus, we have n-1 first order equations in normal form:

 x1' (t )  y ' (t )  x2 (t )
 '
 x2 (t )  y ' ' (t )  x3 (t )

 
 x ' (t )  y ( n1) (t )  x (t )
 n1 n

 xn (t )  y (t )  f (t , x1 , x2 ,, xn )
' ( n )

Example 1 (Duffing equation): Convert the initial value problem :

y' ' (t )  y(t )  y(t )3  0, y(0)  0, y' (0)  2


into an initial value problem for a system in normal form.
Let x1 (t )  y(t ), x2 (t )  y '(t )

Normal form:

 x ' (t )  x2 (t )
 '
1

 x2 (t )   x1 (t )  x1 (t )3
 x1 (0)  0
with initial values: 
 x2 (0)  2

Using ode45 to solve Ordinary Differential Equations

Matlab’s standard solver for ordinary differential equations is the function ode45. This
function uses a Runge-Kutta method with a variable time step for efficient computation.

The ode45 is designed to solve the following problem:

 x1' (t )  f1 (t , x1 (t ), x2 (t ),, xn (t ))
 '
 x2 (t )  f 2 (t , x1 (t ), x2 (t ),, xn (t ))
(2) 
 
 x ' (t )  f (t , x (t ), x (t ),, x (t ))
 n n 1 2 n

with initial value conditions: x1 (t0 )  a1 , x2 (t0 )  a2 ,, xn (t0 )  an .

syntax for ode45

In the command line, we can invoke ode45 through

[t x]=ode45(‘filename’, tspan, x0 , options)

filename: Name of the function Mfile used to evaluate the right hand side function in (2)
at a given value of the independent variable and dependent variables. For function mfile,
the filename and function name must be same. The function definition line usually has
the form

function dxdt=filename(t,x)

tspan: [t0 , t final ] define the range of the independent variable

x0 : vector of initial conditions


options: matlab provides some options for you to control the details of the computation.
If you do not provide, it will use default values.

t: value of independent variable


x: value of dependent variable

Example 2: Use MATLAB ode45 function to plot the graph of function x2 (t ) from [0,35]
for the following system of differential equations:

 x1' (t )  35( x2 (t )  x1 (t ))
 '
 x2 (t )  7 x1 (t )  x1 (t ) x3 (t )  28 x2 (t )
 '
 x3 (t )  x1 (t ) x2 (t )  3x3 (t )
with initial conditions x1 (0)  0.5, x2 (0)  1, x3 (0)  1
First we make an m file named Chen_fun.m
function dxdt=Chen_fun(t,x)
% equations
dxdt(1)=35*(x(2)-x(1));
dxdt(2)=-7*x(1)-x(1)*x(3)+28*x(2);
dxdt(3)=x(1)*x(2)-3*x(3);
dxdt=dxdt';
Then in the command line,
EDU>> [t x]=ode45(@Chen_fun,[0 35],[0.5 1 1]);
EDU>> plot(t,x(:,2));
What’s the result if you invoke plot(t,x) in command line?
The above system has a chaotic attractor called Chen attractor.

60

50

40

z 30

20

10

0
- - - 0 10 20 30
30 20 10 x
Example 3: Use MATLAB ode45 to find the value of y(6) and plot the graph of y(t)
from [0,6] for the following Duffing equation:

y' ' (t )  y(t )  y(t )3  0, y(0)  0, y' (0)  2 .

First, we transfer the 2nd order ODE to systems in normal form as we did in example 1.
Let x1 (t )  y(t ), x2 (t )  y' (t ) , we have

 x1 (t )  x2 (t )
'

 '

 x2 (t )   x1 (t )  x1 (t )
3

with initial conditions x1 (0)  0, x2 (0)  2 .

Then define a function in an m-file:


function dxdt=Duffing_fun(t,x)
% equations
dxdt(1)=x(2);
dxdt(2)=-x(1)-x(1)^3;
dxdt=dxdt';

EDU>> [t x]=ode45(@Duffing_fun,[0 6],[0 2]);


EDU>> plot(t,x(:,1));
EDU>> x(size(x,1),1); (to get the value of y(6))

Homework:

1. A simplified mathematical model for conventional versus guerrilla combat is


given by the system
 x1  0.1x1 x2
'

 ' with initial conditions x1 (0)  10, x2 (0)  15 where x1 and


 x2   x1
x2 are the strengths of guerrilla and conventional troops, respectively, and 0.1 and 1
are the combat effectiveness coefficients. Who will win the conflict: the conventional
troops or the guerrillas? Using MATLAB to plot the graphs of x1 and x2 from time
0 to time 20.
2. For the initial value problem x' '0.1(1  x 2 ) x' x  0 , x(0)  x0 , x' (0)  0 , the
solution x exhibits damped oscillations when x0  1 , whereas x exhibits
expanding oscillations when x0  2.1. Using MATLAB to plot the graphs from
time 0 to time 20.
3. Using MATLAB ode45 to find y(5) and plot the graph of y(t) for the following
Airy equation: y' 'ty  0 with y(0)  1, y' (0)  1

You might also like