T X T X T X: Using Ode45 To Solve Ordinary Differential Equations
T X T X T X: Using Ode45 To Solve Ordinary Differential Equations
T X T X T X: Using Ode45 To Solve Ordinary Differential Equations
Normal Form
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
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.
x1' (t ) y ' (t ) x2 (t )
'
x2 (t ) y ' ' (t ) x3 (t )
x ' (t ) y ( n1) (t ) x (t )
n1 n
xn (t ) y (t ) f (t , x1 , x2 ,, xn )
' ( n )
Normal form:
x ' (t ) x2 (t )
'
1
x2 (t ) x1 (t ) x1 (t )3
x1 (0) 0
with initial values:
x2 (0) 2
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.
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
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)
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:
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
Homework: