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:
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
and the windows for dfield will pop up. And the command for pplane is
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.
Example. Use Euler's method to plot the solution of the initial value problem
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
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:
and you will get
Now we use Euler's method routine. Type
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
to disply all data points, simply type
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
>> f = polyfit(t,p,1)
then f would be the linear function which fit the data set best. Type
you would get
That means p=4.6*t+9.8. To plot the data points and the regression line, type
1. In dfield, draw the direction field for P'=P(1-P)-aP cos(t) with a=0.3