MAT 274                   Elementary Differential Equations                  Fall 1995 

1.3. MATLAB: Slope fields, numerical solutions


Objectives: This is a resource for sample commands w/ brief explanations:

MATLAB only recognizes one type of data: MATRICES.

Note that scalars are 1x1 matrices, row and column vectors 1xN and Nx1 matrices.

MATLAB is primarily designed for numerical calculations. Only in the latest version it has added access to the MAPLE machine thus providing for some symbolic calculations as well. (The access is cumbersome, though.)

Browse the on-screen help frequently; ask class-mates; write-down useful discoveries, and share them!

There are many ways to create MATRICES in MATLAB -compare (hyperlink to MATLAB 1-2) and the survey (hyperlink to homework typed up by student), the most important ones for us are typing in the entries component by component, using ranges (like A=[0:.1:10;0:.1:5];), as output of procedures (like ODE23(....)), and a few special elementary matrices (like zeros(5), ones(10), eye(10), rand(10), diag([vector])).

The basic way to create PLOTS in MATLAB is to first create the vectors containing the data points to be plotted (compare making tables of values à la hi-school). The following example overlays two curves.

xx=[-3:0.25:3]; yy=xx.*xx; zz=2*yy; plot(xx,yy,'r-',xx,zz,'bo'). Check HELP for PLOT, and HOLD.

(The example used the name xx to denote a vector of x-values, advanced users simply call this x.

The componentwise multiplication (or application of a function) ( .* in place of * takes a while to get used to.)

The built-in command QUIVER(XX,YY,FX,FY) generates a field plot corresponding to the system of differential equations (dx/dt)=fx(x,y), (dy/dt)=fy(x,y). In this simple form QUIVER takes as arguments four matrices of identical sizes. The components of the matrices XX and YY specify the x- and y-coordinates of the points where arrows are to be plotted. The matrices FX and FY specify the components of the arrows to be plotted at the grid points. A reasonable plot for first order equations is obtained by identifying x and t, i.e. fx=1.

A somewhat sloppy call is QUIVER(XX,YY,ones(XX),FY) with XX,YY,FY as above.

A uniform grid is easily generated with the command [XX,YY]=meshgrid(-5:0.5:5,-5:0.5:5).

For a quick view of a field plot for the differential equations dx/dt=-y, dy/dt=x (or dy/dx= -x/y) try the following: A=rand(10); QUIVER(A,A',-A',A,'c-').

An instructor supplied modification of QUIVER is SLOPEFLD(XX,YY,FX,FY). It is called in basically the same ways as QUIVER, and it generates a slope-field (of unoriented line-segments of equal lengths). The standard use for first order differential equations again uses FX=ones(XX).

The most basic numerical solution technique employs Euler's method. A typical example is as follows:

xx=0;yy=0.2; for i=1:50; xx(i+1)=xx(i)+.1; yy(i+1)=yy(i)+.1*yy(i)*(1-yy(i); next i; plot(xx,yy)

It is better to create the vector xx outside the for-loop. (Also, it is a good idea to reset yy at the beginning!) xx=[0:0.1:5];yy=zeros(size(xx)); yy(1)=0.2; for i=1:50; yy(i+1)= yy(i)+.1*yy(i)*(1-yy(i); next i; plot(xx,yy)

To simultaneously solve several initial value problems at a time, create a matrix (!) yy each of whose columns contains the data for one solution. Note, MATLAB understands the plotting command (listing xx only once):

xx=[0:0.1:5];yy=zeros(size(xx),); yy(1:)=[.1:.1:.9]; for i=1:50; yy(i+1:)= yy(i:)+.1*yy(i:)*(1-yy(i:); next i; plot(xx,yy)

MATLAB has two basic numerical solvers built in ODE23 and ODE45, check the corresponding help pages-- details behind their reasoning will be discussed later in the DE-class. The calling sequences are similar:

[xx,yy]=ode23('filename',x0,x1,y0). The output consists of two vectors that may be plotted just like the solutions obtained above. x0 and x1 are the initial and terminal time value, y0 is the initial value of the solution.

(In addition y0 may be a vector of initial conditions, yy will then be a matrix as above).

A formula for the right hand side of the differentail equation dy/dx=f(x,y) has to be saved as a function/procedure in an M-FILE. The following example (logistic equation: dy/dx=y(1-y)) exhibits the basic syntax: The filename is logistic.m, and it usually is saved in the root-directory of the C:-drive (in class), or in the subdirectory \matlab\bin (where not write-protected). The file contains only two lines of text:

function yprime=logistic(x,y)


(The name yprime may be replaced by any other name -- but it must be replaced in all locations.) The name logistic in the 1st line apparently may be replaced by any name (it apparently need not coincide with the filename). The following is a typical call of ode23 for the logistic equation:

[time,pop]=ode23('logistic',0,5,[.1:.1:.9]); plot(time,pop)

(The square brackets enclosing the range of initial conditions may be omitted.)

Note a convenient way to overlay a slope field with solution curves uses the command HOLD ON.