Solving Differential Equations

There are two main ways of solving differential equations on a computer which are very different from both the technical and the philosophical view points: symbolic solutions and numerical approximations. A symbolic solution is possible whenever there exists an expression of the solution by means of elementary functions and their composition. On the other hand, we have numerical approximations of the solution, this method is more general but does not provide a formula for the solution it is just able to compute the solution at given points, outputing hence a vector as a solution.

As an an example let us start with the following equation

y'(t) = t + y(t)
with initial condition
y(t0) = y0, for some given t0.

Symbolic solution with dsolve

To compute the symbolic solution, with all the embelishments, we will use and M-file. (Remember that you should write an M-file as soon as you find it useful, sometimes it can be useful even if you don't find it so. Typing in commands from the prompt to do projects in MATLAB is a very poor and highly inefficient practice.)
The solution file is already written and can be found in symbolic_ode.m. In this case dsolve is the main command. Since we are dealing with symbolics, the output of dsolve is an expression (the output is actually produced by Maple, not by MATLAB).
To transform the expression into something more manageable by MATLAB we must first vectorize the expression (to put "dots" where Maple doesn't do it) and then use the inline command to produce a MATLAB function. Notice that the inline command constructs a function of three arguments: one is the independent variable t and the other two are the parameters given by the initial condition, namely t0 and y0.
Once the solution family is given as a MATLAB function we can exploit it for plots, tabulates, etc.

Numerical integration

MATLAB version 5.3 has a very efficient family of numerical solvers for differential equations. The most simple example is given by int, which is just the definite integral of a function. (Calculating the integral of a function f can be viewed as the solution of the differential equation y' = f(x).) For example, let us compute the (definite) integral of the Gaussian distribution f(x) = e-x^2, between the points -4 and 4. To do this we first must define the integrand function with an M-file or with and inline command
f = inline('exp(-x.^2)','x')
and then approximate the definite integral using the quadrature rule quad:
quad('f',-4,4)

Numerical solution of ODE (Ordinary Differential Equations)

Before we illustrate the MATLAB technique for approximating numerically the solution of differential equations, let us make a small
Exercise Modify the M-file symbolic_ode.m to solve the differential equation
y'(t) = t + |y(t)|2
with initial condition
y(0) = 1.
What is the outcome of such a modification? MATLAB is unable to compute the solution in terms of elementary functions. This is a classical example (of striking simpicity) of a differential equation whose solution is transcendental. This example gives not only legitimacy, but shows that it is necessary, to look at numerical algorithms in order to get understand the solution of certain (most of them) equations.
Please download the M-file numeric_ode.m which contains the script to solve the above equation.
Observations:
  1. the maxt parameter in the M-file is important. Try to see what happens if maxt is pushed all the way to a value of 1. (Modify the M-file by doing an appropriate for loop.)
  2. controlling the axes is important to visualize the "interesting" features of the solution, a bad choice of axes scaling might hide interesting stuff.
  3. the syntax of ode45 is delicate, you should always consult the helpdesk while using it. Because:
... YOU NEVER END UP LEARNING MATLAB.

Back to top of section.
Back to contents.

PT home | About PT | Schedule | Registration | Courses | Alternatives | Contact us

Direct questions and comments to Carol Warrington.
© 2002 University of Maryland

 

How are we doing?site map
Office of Information Technology
Office of Information Technology University of Maryland