Sets of differential equations

The commands ode23 and ode45 can also be applied for sets of differential equations. For example, consider the set of differential equations

$\displaystyle \dot{x}_1$ $\displaystyle = 5x_1-2x_2$ (5.6)
$\displaystyle \dot{x}_2$ $\displaystyle = 7x_1-4x_2$    

The initial values $ x_1(0)=2$ and $ x_2(0)=8$ define exactly one solution.

With the commands ode23 and ode45 this type of differential equations can be tackled. To start, we write the function file `deq.m' in the `MATLAB Editor/Debugger', with the contents:
function xdot = deq(t,x)
    xdot = [5*x(1)-2*x(2);7*x(1)-4*x(2)];
Note, that $ xdot$ has to be a column vector. The command:

>> [t,x] = ode23('deq',[0,1],[2,8])

now calculates on the time interval [0,1] the values of $ x_1(t)$ and $ x_2(t)$ with initial conditions $ x_1(0)=2$ and $ x_2(0)=8$ . The variable $ x$ becomes an array with two columns, where the first column contains the values of $ x_1(t)$ and the second column contains the values of $ x_2(t)$ . You can plot the solutions with the command:

>> plot(t,x)

Since $ x$ consists of two columns, two graphs are drawn in the same figure. If you only enter the command:

>> ode23('deq',[0,1],[2,8])

the graphs of $ x_1(t)$ and $ x_2(t)$ are drawn, since by default the `OutputFcn' odeplot is used. Note that you could also have written the file `deq.m' in the following way:
function xdot = deq(t,x)
    A = [5,-2;7,-4];
    x = [x(1);x(2)];
    xdot = A*x;

Previous      Next      Up      Contents

Esteur 2010-03-22