This is an old revision of the document!
<html> <p><div align=“center”> <h1> ARCHIVED MATERIAL</h1> <h1>A Beginning MATLAB Tutorial for </h1></p> <p><h1>the UC Davis Department of Mathematics</h1></p> <p>by Sarah Williams<br>
<br>
updated 9/15/2003<br>
</div></p>
<p><hr> <font size=“+1”>Introduction and Overview</font><br></p> <p><ul>
<li>What is MATLAB?</li>
<li>Why use MATLAB? Why not?</li>
<!-- <li>What to expect from this workshop and beyond </li> --></p>
<p></ul>
<font size="+1">Command-Line MATLAB</font><br></p>
<p><ul>
<li>Launch MATLAB</li>
<li>Generate matrices</li>
<li>Implement functions</li>
<li>Write a small program that generates a plot</li></p>
<p></ul>
<font size="+1">M-File MATLAB</font><br></p>
<p><ul>
<li>Directory Structure (where will you save your file?)</li>
<li>Put your small program into an m-file, and run it</li>
<li>Scripts vs. Functions: write a function m-file</li>
<li>Get your Scripts and Functions Talking</li>
<li>Add More Features and Get More Help<br><p> </li></p>
</p> <p></ul>
<font size="+1"></font>
<ul></p> <p></ul></p> <p><hr width=“100%” size=“2”><font size=“+1”>Introduction and Overview</font><br>
<br></p>
<p>Please note that MatLAB can be accessed both on the department computers and from home via SSH. From home, one can even combine SSH with X Windows to use the graphical version of MatLAB.</p> <h2><h3><a name=“IntroA”></a>What is MATLAB?</h3></h2> <p> MATLAB is both a programming language and a software package. “MATLAB”
stands for "Matrix Laboratory." Accordingly, many features exploit matrices, and assume you are working with matrices. MATLAB
generally solves
problems numerically, not symbolically (compared to Mathematica).<br>
<br></p>
<p><h3><a name=“IntroB”></a>Why use MATLAB? Why not?</h3>
Pros:<br></p>
<p><ul>
<li>It's easy to start using MATLAB, with or without prior programming experience.</li><p> <li>MATLAB has a lot of built-in mathematical knowledge; e.g.
MATLAB has a function to find the 1-, 2-, or max-norm of a vector – you don't have to program this yourself.<br>
</li>
<li>MATLAB has a lot of very powerful "toolboxes" to work in
specific areas of math (optimization, wavelets, etc.)</li>
<li>It's easy to produce nice graphs.</li></p>
</p> <p></ul> Cons:<br> <ul>
<li>MATLAB has a lot of "overhead," like graphics and notation that make
it easy to use. This can slow it down compared to more bare-bones programming languages (like C, FORTRAN). MATLAB may be too slow to solve some large problems.</li><p> <li>MATLAB is not free - although we have good access at school (and you can run the school's version remotely), if you happen to want your own private copy, it costs $100 for students, and more for each toolbox.</li> </ul></p> </p> <p><!– <h3>What to expect from this workshop and beyond</h3>
After this workshop, you will be able to:<br></p>
<p><ul>
<li>complete a practice MATLAB homework assignment (based
on mat228a)</li>
<li>write a program, save it, run it, edit it, and run it again</li><p> <li>save subroutines in different files and call them from the
main program<br>
</li>
<li>make simple graphs</li>
<li>look for more help in the MATLAB documentation</li></p>
</p> <p></ul>
After working with MATLAB this quarter, you can expect to:<br></p>
<p><ul>
<li>be familiar with a lot of useful functions built into MATLAB</li>
<li>recognize common error messages and how to fix them</li>
<li>make more complex graphs<br>
</li></p>
<p></ul> –></p> <p><hr width=“100%” size=“2”> <ul></p> <p></ul>
<font size="+1">Command-Line MATLAB</font><br></p>
<p><h3>Launch MATLAB</h3></p> <p><ul>
<li>Click (once) on the MATLAB icon. (Enter your password
when prompted.)</li> <li>OR from the terminal window type “ssh sine” (enter) and then “matlab”</li></p> <p></ul></p> <p><h3>Generate matrices</h3></p> <p><ul>
<li>Find the panel called the Command Window. This is where
we will be working.</li>
<li>Type the following commands:</li><p> <ul>
<li>In what follows, the <font
face=“Courier New, Courier”>>></font> symbol is meant to represent the MATLAB prompt (i.e., don't type this yourself).</li><p> <li>Hit the return key at the end of each line; observe the result and try to understand the cause-and-effect of your commands.
</li></p>
</p> <p> </ul>
<br>
<font face="Courier New, Courier">>> A = [1 2 3; 4 5 6]<br>
>> B = [7; 8; 9] <br>
>> C = A*B<br><p> <br>
</font> <li>Now try a slightly different version of the same
thing:<br>
<br>
<font face="Courier New, Courier">>> clear<br>
>> A = [1 2 3; 4 5 6];</font>
(note the semicolon at the end)<br><p> <font face="Courier New, Courier">>> B = [7 8 9]'; </font>
(note the apostrophe)<br>
<font face="Courier New, Courier">>> C = A*B; <br>
>> C<br>
>> C'<br></p>
</p> <p> <br>
</font></li>
<li>Accessing matrix entries (vector components):<br>
<br>
<font face="Courier New, Courier">
>></font> A<br>
<font face="Courier New, Courier">
>></font> A(1,1)<br><p> <font face="Courier New, Courier">
>></font> A(1,2)<br>
<font face="Courier New, Courier">
>></font> A(2,1)<br>
<font face="Courier New, Courier">
</font><font face="Courier New, Courier">
>></font> B<br>
<font face="Courier New, Courier">>></font> B(1,1)<br>
<font face="Courier New, Courier">
>></font> B(1)<br></p>
<p> <font face=“Courier New, Courier”>
>></font> B(3)<br>
<br>
</li>
<li>A very handy way of making uniformly spaced "grids" in MATLAB:<br>
<br>
<font face="Courier New, Courier">>> clear<br></p>
<p> >> X = 1:5 <br>
>> Y = 3:0.3:5 <br>
>> Z = [0:(pi/4):(2*pi)]'<br>
<br>
</font></li>
<li>To review:</li></p>
</p> </p> <p> <ul>
<li>Put square brackets [] around matrices.</li>
<li>You will get a single row if you just type numbers in the
brackets. To go to the next row, type a semicolon.</li>
<li>Use * for matrix multiplication (works for regular multiplication,
too).</li>
<li>MATLAB repeats what you just typed. To suppress this,
put a semicolon ; at the end of the line.</li>
<li>To see the value of a matrix (or other variable) you've
entered, just type its name and hit return.<br><p> </li>
<li>An apostrophe gives you the transpose (the complex conjugate
transpose, in fact) of the matrix.<br>
</li>
<li>To refer to the component of matrix A in the mth row down
and nth column over, type A(m,n). If A is a row or column vector, you can just type A(j) to see the jth component. <br>
</li>
<li>The colon : notation gives you uniformly spaced grids (row
or column vectors). The first number is the left endpoint of the
grid. If you include a middle number, it's the grid spacing (delta x); if you don't state a value here, MATLAB assumes you want a value of 1. The last number is the maximum right endpoint of the grid.</li></p> <p> <li>The command “clear” erases all the variable values you've assigned so you can start fresh. Use this often – MATLAB has a bad habit of leaving remnants of old variables lying around unless you explicitly clear them out. <br>
</li></p>
</p> <p> </ul></p> </p> <p></ul></p> <p><h3>Implement functions</h3></p> <p><ul>
<li>Type the following commands:<br><p> <br>
<font face="Courier New, Courier">>> clear <br>
>> c = -pi/2; <br>
>> X = [0:0.1:1]';
<br>
<br>
>> sin(c)<br></p>
<p> </font><br>
Now, type the "up" arrow.
MATLAB will display the last thing you typed.
(Type "up" again and you can move backwards through your command
history.) Use the arrow keys
to edit the last line to read:<br>
<br>
<font face="Courier New, Courier"> >> sin(X) <br>
<br>
>> sqrt(c) </font></p>
<p> (Note the imaginary value.)<br>
<font face="Courier New, Courier"> >> sqrt(abs(c))<br>
<br>
>> Y = exp(X)<br>
>> Z = log(Y)<br><p> </font></li></p>
</p> </p> <p></ul></p> <p><h3>Write a small program that generates a plot</h3></p> <p><ul>
<li>Type the following commands to plot a semi-circle:<br>
(Note: <font face="Courier New,Courier">.^</font> gives the component-wise
square, i.e., it raises each entry of X to the power of 2.) <br><p> <font face="Courier New, Courier"><br>
>> clear <br>
>> X = -2:0.1:2;
<br>
>> Y = sqrt(4-(X.^2));</font><br>
<font face="Courier New, Courier">>> plot(X,Y) <br><p> </font></li></p>
</p> </p> <p></ul></p> <p><hr width=“100%” size=“2”><br>
<font size="+1">M-File MATLAB</font><br>
Typing commands directly to MATLAB is well and good, and you will
want to experiment at the command line frequently. However, for homework assignments and other distinct problems, you will want to keep your program in a file, so that you can edit it and run it again. You will also want to make files that call each other – for example, you could have a plotting routine that uses the format you like, and each of your programs could call this routine when it comes time to make a graph.<br></p> <h2><h3>Directory Structure (where will you save your file?)</h3></h2> <p> Now is the time when we talk about Linux. If you already know which directory you will save your program files in, skip to the next section.<br></p> <p><ul>
<li>Put MATLAB aside for a moment. (Minimize the window
if you like.)</li>
<li>Click on the "Localhost" icon to get a new terminal window.</li>
<li>Initially, you are "sitting" at your "home" directory.</li>
<li>Type ls to list the subdirectories.</li><p> <li>Do you have a subdirectory where you put your coursework?
Would you like one?</li>
<li>To create a subdirectory called Courses, type mkdir Courses
(you can change Courses to any other name you prefer)</li>
<li>Now type cd Courses (cd means change directory)</li>
<li>Now you are sitting at your Courses directory.</li>
<li>Type ls to list what's here. Nothing, right? Let's
put something here.</li></p>
<p> <li>Type mkdir Math228a to make a directory for numerics.</li>
<li>Now type ls. Can you see your new folder? This
is a good place to store your MATLAB programs.</li>
<li>You can exit the terminal window now (type exit) and bring
your MATLAB window back.<br>
</li></p>
</p> <p></ul></p> <p><h3>Put your small program into an m-file, and run it</h3></p> <p><ul>
<li>At the top left of the MATLAB window, there is an icon that
looks like a “new document” button. Click (once) on this.</li>
<li>A blank editor window will open. This is where
you type your program. Type:<br>
(Note: in my experience, trying to copy and paste text into the MATLAB editor crashes the whole thing. You can try it, but you'll learn
more if you type the text yourself anyway.)<font face=“Courier New, Courier”> <br>
<br><p> % My Name<br>
</font><font face="Courier New, Courier">% The Date<br>
% The purpose of this program is to plot a semi-circle on [-2,2].<br>
</font><font face="Courier New, Courier"> X = -2:0.1:2;<br>
Y = sqrt(4-(X.^2));<br></p>
<p> plot(X,Y)</font><font face=“Courier New, Courier”><br>
<br>
</font></li>
<li>The percent signs % indicate comments. You can put
these anywhere in a program, to explain your code. Use comments as often as possible; otherwise, you will not remember why you did what you did.<br>
</li>
<li>Click on the File menu, and choose "Save As."</li></p>
<p> <ul>
<li>You want the top field to contain the right directory (the
one you created in the last step). </li>
<li>In the bottom field, type the name of the program. Call it circle1.m</li></p>
<p> </ul>
<li>If your semi-circle figure is still open from before, close
it.</li>
<li>Click on the command window.</li><p> <li>Take a look at the top of the window. There is a field
labeled “Current Directory.” Is it looking in the right place for your program?</li></p> <p> <ul>
<li>If not, you can type "cd" and "ls'
in MATLAB just like you can in the terminal window. You might need to type something like<br>
<font face="Courier New, Courier, monospace">>> cd Courses/Math228a</font></li></p>
<h2> </ul></h2> <p> <li> type ls to see if your program is there. To run your program, type its name, e.g.<br>
<font face="Courier New, Courier, monospace">>>circle1</font><br>
</li></p>
</p> </p> <p></ul></p> <p><h3>Scripts vs. Functions: write a function m-file</h3></p> <p><ul>
<li>The program you just wrote, circle.m, is called a "script"
- it runs straight from beginning to end, like a script to a play.</li><p> <li>In MATLAB, you can also write a program that is a function.</li></p> <p> <ul>
<li>A function usually takes input and returns output.</li></p>
<p> </ul></p> <p> <ul>
<li>A function file must start with a "function declaration."
We will see an example below.</li></p> <h2> </ul></h2> <p> <li>Open a blank program file. Type:<br>
<br>
<font face="Courier New, Courier">% My Name<br>
% The Date<br>
% The purpose of this program is to plot a semi-circle of a<br>
% given radius.<br><p> <br>
function Y = circle2(radius)<br>
X = -radius:0.1:radius;<br>
Y = sqrt(radius^2-(X.^2));<br>
plot(X,Y)<br>
<br>
</font></li></p>
</p> <p> <li>The line <font face=“Courier New, Courier”>function Y = circle2(radius)</font> is the function declaration. Note:</li></p> <p> <ul>
<li>The word "function" must be the first (non-comment) word
of the program.</li>
<li>"Y" is the value that this function returns, or calculates,
a.k.a. the output.</li>
<li>"radius" is the name of the variable taken as input.</li><p> <li>"circle2" should be both the name of the function and the name
that you use to save the file.</li></p>
<p> </ul>
<li>To run your program for different radii, type:<br>
<font face="Courier New, Courier, monospace"><br>
>>circle2(2)<br>
</font><font face="Courier New, Courier, monospace">>>circle2(4)</font>;<br><p> <font face="Courier New, Courier, monospace">>>circle2(1);<br>
<br>
</font></li>
<li>Note that the figure is replaced each time you call the function,
and that MATLAB automatically chooses the size of the x- and y-axes.<br>
</li></p>
</p> </p> </p> <p></ul>
<br></p>
<p><h3>Get your scripts and functions talking</h3></p> <p><ul>
<li>Open a blank program file. Type:<br>
<br>
<font face="Courier New, Courier">% My Name<br>
% The Date<br>
% The purpose of this program is to plot semi-circles <br><p> % with radius = 1,2,3, and 4.<br>
<br>
for j = 1:4<br>
circle2(j);<br>
end<br>
<br><p> </font></li>
<li>Save the file (in the same directory as circle2.m) as makecircles.m</li>
<li>makecircles.m is a script. It is "calling" circle2.m, which
is a function. They're talking to each other. (Who is saying what?)<br>
</li> <li>Close any plots that are still open.</li> <li>To run the program, type:<br></p>
<p> <br>
<font face="Courier New, Courier, monospace">>> clear<br>
>> makecircles</font><br>
<br>
</li>
<li>Did you get what you expected? To make the program produce
4 different plots without overwriting, we need to make a small edit:</li></p> <p> <ul>
<li>open your circle2.m file (probably it is still open in the editor
window and you just need to click on its tab at the bottom of the window)</li>
<li>insert a line that says "<font
face=“Courier New, Courier, monospace”>figure</font>“ right above <font face=“Courier New, Courier, monospace”>plot(X,Y)</font>. This means “create a new figure (don't overwrite the last one).”</li>
<li>Now your program should read as follows:<br>
<br><p> <font face="Courier New, Courier">% My Name<br>
% The Date<br>
% The purpose of this program is to plot a semi-circle of a<br>
% given radius.<br>
<br>
function Y = circle2(radius)<br><p> X = -radius:0.1:radius;<br>
Y = sqrt(radius^2-(X.^2));<br>
<font face="Courier New, Courier, monospace">figure</font><br>
plot(X,Y)</font><br>
<br>
</li></p>
<h2> </ul></h2> <p> <li>Save circle2.m, close any figures that are open, and run makecircles again.<br>
<br> </li> <li>A different option: make the program produce one plot showing all
4 circles</li></p> <p> <ul>
<li>open your circle2.m file</li>
<li>"comment out" the "<font
face=“Courier New, Courier, monospace”>figure</font>” line. That is, put a <font face=“Courier New, Courier, monospace”>%</font> in front of it.</li><p> <ul>
<li>MATLAB will not read your comments</li>
<li>"commenting out" can be better than deleting, because you can
easily go back if you change your mind. (by erasing the <font face=“Courier New, Courier, monospace”>%</font> mark)</li></p> <p> </ul>
<li>above <font face="Courier New, Courier, monospace">plot(X,Y)</font>,
add a line that says “<font face=“Courier New, Courier, monospace”>hold on</font>.”</li><p> <li>Now your program should read as follows:<br>
<br>
<font face="Courier New, Courier">% My Name<br>
% The Date<br>
% The purpose of this program is to plot a semi-circle of a<br>
% given radius.<br><p> <br>
function Y = circle2(radius)<br>
X = -radius:0.1:radius;<br>
Y = sqrt(radius^2-(X.^2));<br>
<font face="Courier New, Courier, monospace">%figure<br>
</font>hold on<br></p>
</p> </p> <p> plot(X,Y)</font><br>
<br> </li> </ul> <li>Save cirlce2.m, close any figures that are open, and run makecircles
again.</li>
<li>To save your plot:</li> <ul></p>
<p> <li>At the top of the plot window, click on File</li>
<li>click on Export</li> <li>Verify that it's going in the right directory, give it a name in
place of where it now says *, (try cplot1), and choose a format (eps or color eps is good)<br>
<br> </li> </ul> <ul></p>
<p> </ul></p> </p> </p> </p> </p> </p> <p></ul></p> <p><h3>Add More Features and Get More Help<br> </h3> <ul>
<li>Plots should have titles. How do you add a title?</li> <ul> <li>In the MATLAB window, click on the Help menu item. From the
list that drops down, choose “MATLAB Help.”</li><p> <li>You'll get a new window full of help. </li>
<li>In the left-hand pane, there are 4 tabs: Contents, Index, Search,
and Favorites. Click on Search.</li>
<li>In the blank box, type "title" (and hit return)</li> <li>double-click on the first "hit" -- the one that simply says "title."</li> <li>Read the help in the right-hand pane, and see if you can add a line
to your program so that your plot will have a title.<br>
<br></p>
<p> </li>
</ul> <li>Maybe you'd rather change the x-axis of your plot, so that it shows
[-5,5].</li>
<ul> <li>Back in the help window, search for "axis"</li> <li>double-click on the "hit" that looks the best</li> <li>go to it!<br></p>
<p> </li>
</ul>
</ul> <br> </p> </p> </html>
