Computer Lab (Math 410)

Log in Campus network

To use Matlab program, you first need to log in W&M Campus network. The location of Labs can be found at http://www.wm.edu/IT/labs/map.html. Here is the procedure to open Matlab:

  1. Log in the computer network using your W&M ID and password(usually p and last 6 digits of your social security number). For the domain, choose "Campus".
  2. Once Windows 2000 has started, click "Start"->"Programs"->"Courseware"->"Math"->"Matlab 5.3"->MatlabR12"
The Matlab will start. It will first display an error message, but you can ignore it by clicking "OK".
It will take a few seconds to 30 seconds for Matlab to initialize.

Basic Commands of Matlab

Matlab is a commecial mathematics software which can do a lot of computation and visualization of mathematics. Most of time we only need to use John Polking's programs: dfield and pplane. But in the lab we will also use some Matlab functions to do Euler's method, data fitting and others.
To start dfield, simply type
>> dfield6
and the windows for dfield will pop up. And the command for pplane is
>> pplane6

Next we show some other Matlab functions by examples.

Plotting function graphs
Example. Sketch the graphs of y=x^2-3x+5 and z=x^3+6x^2-6 over the interval [-2,3] on the same figure. Use a solid line type for the first graph and a dashed line type for the second graph.
>> x=-2:0.05:3;
>> plot(x,x.^2-3*x+5,x,x.^3+6*x.^2-6,'--')

Euler's method
Example. Use Euler's method to plot the solution of the initial value problem
                         y'=y+t, y(0)=1
on the interval [0,3].
Before using the routine for Euler's method, you have to first write a function M-file for f(t,y)=y+t.
In Matlab main window, click "File"->"New"->"M-file", then a new window to write a M-file in will show up. In that window, type
>>function yprime=yplust(t,y)
>>yprime=y+t;
then save the file in a directory of your choice. For example, you can save it to C:\my files\M-files\
and the filename is yplust.m. Then you have to add this directory to the paths which Matlab will search for excutables. To add the path, click "File"->"Set Path", and a new window pop up. In the new window, click "Add folder", and choose C:\my files\M-files\, then click "Save". Now try type this in the main Matlab window:
>>yplust(1,2)
and you will get

ans =

     3
Now we use Euler's method routine. Type
>> [t,y]=eul('yplust',[0,3],1,0.1);
>> plot(t,y)
A graph of the approximate solution will show up. Here [0,3] is the interval to perform Euler's method, 1 is the initial value y(0)=1, and 0.1 is the stepsize. To just plot the data points, you type
>>plot(t,y,'o')
to disply all data points, simply type
>>y

Data Fitting
Example. [PBA] page 135 problem 8.
First we need to enter data points. In Matlab, this can be done using vectors. Here let's try for the first five data points in the problem. Type
>> t=[0,1,2,3,4]
>> p=[10,14,19,24,28]
>> f = polyfit(t,p,1)
then f would be the linear function which fit the data set best. Type
>> f
you would get
f =

    4.6000    9.8000
That means p=4.6*t+9.8. To plot the data points and the regression line, type
>> t1=0:0.1:4;
>> p1=polyval(f,t1);
>> plot(t,p,'o',t1,p1)

Exercises
1. In dfield, draw the direction field for P'=P(1-P)-aP cos(t) with a=0.3