Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help >  programs >  matlab

Matlab and ODEs

With Matlab it's easy to solve some ODEs, though for harder ODEs you may need to do some preliminary maths first. This short document will illustrate the easier options, beginning with numerical solutions, then showing how to get general (symbolic) results.

A simple example

Suppose you want to solve
y' + 2ty =0
between t=0 and t=5
where y(0)=3
To be able to use the matlab routines you first need to write a function that calculates y' given t and y. That's easy to do here, which is why this example is easy.
function dy = yprime1(t,y)
dy= -2*t*y;
This function can go in a separate file called yprime1.m, though it's often tidier to make it a subfunction in the same file as the rest of the code (which is what we'll do here). Then all you need to do is run one of matlab's ODE-solver routines. If you put the following in a file called odedemo1.m and run it you'll see the result displayed in a graphics window.
function odedemo1
disp('Solving y''+2ty=0; y(0)=3; between t=0 and t=5');
[T,Y] = ode23(@yprime1, [0, 5], 3);
plot(T,Y);

function dy = yprime1(t,y)
dy= -2*t*y;
It's worth drawing the graph as a quick check even if you're only interested in the numbers. Note that ode23 in this example is given 3 arguments. The first defines the function (the @ before the name creates a "function handle" which is the recommended way of passing a function name). The 2nd and 3rd arguments describe the range and initial conditions respectively.

More arguments

Before trying harder ODEs, let's consider some variations on this simple example

Second Order ODEs

In the following example we have a 2nd derivative
y'' + 2ty' +3y=0
between t=0 and t=5
where y(0)=3 and y'(0)=1
The trick is to reduce the order of this to produce 2 coupled 1st order ODEs by introducing some new variables
y1 = y
y2 = y'
Then the original system can be rewritten as
y2' = -2ty2 -3y1
y1'=y2
ode23 can solve coupled systems like this, though vectors need to be used where in the earlier example we used single values. The subfunction (let's call it yprime2) now becomes
function dy = yprime2(t,y)
dy= [y(2); -2*t*y(2)-3*y(1)];
Note that matlab's y(1) and y(2) are what we called y1 and y2. The returned value is now a vector containing [y1'; y2'].

The initial conditions also need to be provided in a vector form. At t=0, [y1; y2] = [3;1]. Putting all this together we can create odedemo2.m

function odedemo2
[T,Y] = ode23(@yprime2, [0, 5], [3;1]);
plot(T,Y);

function dy = yprime2(t,y)
dy= [y(2); -2*t*y(2)-3*y(1)];
Now that ode23 is being given vectorised inputs it produces a vectorised output, so on the graph 2 lines will be displayed, showing y1 and y2 (which in terms of the original system are y and y').

More generally

Other ODE routines

Only ode23 has been used in these examples. It's not the only routine available, and it's not always the most accurate. If you have the time you could explore alternatives -

General Solutions - the Symbolic toolbox

A matlab add-on called the Symbolic toolbox provides a routine called dsolve which gives general solutions. For example,
dsolve('D2y + 2*Dy - 3*y=0')
(where D is the differential operator) returns C1*exp(t)+C2*exp(-3*t) (where C1 and C2 are arbitrary constants). If boundary conditions are supplied, dsolve can give numerical solutions. E.g.
dsolve('D2y -5*Dy + 4*y=0', 'y(0) = 1', 'Dy(0)=0')
returns 4/3*exp(t)-1/3*exp(4*t)

See Also

Whole books exist on the topic of using matlab to solve ODEs. Here are just a few of the online resources.
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
Last updated: January 2006