Math 251H, Fall 2006
Matlab Introductory Tutorial
John Zweck
The Matlab software is a product of The MathWorks, and the best way
to get familiar with it is to use The MathWorks excellent documentation.
In this tutorial you will read selected parts of Matlab's documentation,
experiment using Matlab for yourself, and use Matlab to do some
problems in Multivariable Calculus.
In what follows I will be referring to the
Getting Started web pages.
(1) First read the sections "What is MATLAB?", "MATLAB Documentation",
and "Starting and Quitting MATLAB".
(2) Now start up Matlab for yourself. To learn what the different windows
are for look at the documentation "MATLAB Desktop".
(3) In the Command Window type "intro" to learn how to enter vectors and
matrices and plot data. (Don't expect to understand all the math just yet!)
(4) Try entering a vector such as v = [1 7 4 6] for yourself and then
plot it.
Notice that Matlab joins up the individual data points
(1,1), (2,7), (3,4), (4,6) with straight lines.
(5) Next read the section "Manipulating Matrices" as far as the page
on "Examples of Expressions".
(6) A row vector is a 1 x n matrix and a column vector is an
n x 1 matrix.
Enter a row vector v whose components are (1,2,3),
and a column vector w whose components are (4,5).
Make sure that Matlab echoes back to you the row and column
vectors you hoped it would!
To check the size of a matrix or vector use the size command, as in
"size(v)".
To compute the length of a row or column vector u you enter
"norm( u)".
Compute the lengths of the vectors v and
w.
For your homework you should do the following.
Read about the Matlab diary command using help diary.
Turn on a diary file homework1.diary
and then echo your script homework1.m to the screen using
the command type homework1.
Then run the script generating your results.
Finally turn your diary off. To print your figures, choose the
option print from the pulldown menu file
at the top left of the figure. You can also use the export
option in file to generate a .eps file which you can later print.
Everytime you hand in Matlab homework, you need to hand in the
code used to generate the results, a print out of any text generated
when you run the code, as well as neat, labelled figures!
(9) HOMEWORK QUESTION TWO
In this question you will learn to plot a discretized version
of a continuous function. Given a function y=f(x) whose domain contains
an interval [a,b], we choose an integer N and a small increment
delta_x. We construct an array of x-coordinates in Matlab using
x=[a:delta_x:b]. Note that size(x) = 1 x (N+1).
We then make a new array y whose components
are y(i) = f(x(i)), i= 1,2,...,N+1 . So for example if
f(x) = cos(2 pi x), we would enter
y=cos(2*pi*x). Then you can plot the discrete approximation
to the continuous function f by making the call
plot(x,y).
(a) Use the method described above to plot y=cos(2 pi x) on the interval
[0,1] using N=100.
For additional background on graphing, read some of the "Graphics" section
of the documentation.
(b) Repeat (a) with N=10, N=4, and N=1. Explain what is going on.
You might find it helpful to use he command plot(x,y,'bo-') which will
place a circle at each of the N+1 data points on the discrete curve.
(c) Repeat (a) with N=20 and y=cos(2*k*pi*x), where k=1,2,4,8,10,20.
What is going on here and why? How could you fix this problem?