Matlab1 2022-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

First Matlab session for NMM1411

D.J. Jeffrey, B. Tudose, et.al


Department of Applied Mathematics,
The University of Western Ontario,
London, Canada, N6A 5B7

1 Starting Matlab
Steps to setting up Matlab.
1. Double click the Matlab icon.
2. Decide where you will store your exercises (memory stick, Western system, temporary space on local disk).
3. Go to the directory bar at the top of the Matlab window, and change to the place you have chosen.
4. Go to the menu bar and click on “open a new m-file”. The editor window will open.
5. On the first line in the editor window type a comment line describing today:
% First Matlab session.
Notice that the line starts with a comment symbol. Add further comment lines with your name and other
information that might be useful.
6. Save the file to your work space. Note that although Windows allows you to put spaces in filenames, this
can be inconvenient for files that you are using with Matlab. So choose a name without spaces or special
symbols.
7. Notice that Matlab saves the file with the extension “.m” and for that reason you will see books referring
to “m-files” or “dot m files”.
Of course, Windows often tries to hide from you things like extensions in filenames, so if you cannot see
the extension, but are curious to see it, you will have to modify your windows settings.
8. You may now begin trying Matlab commands.
Notice that most books recommend a different method. They say that the interactive window is the place
to start. I disagree. It is much better to work from the editor and save your work as you go along. At the
end of your lab session or your test, you are guaranteed to have a useful record of what you did. So use
the editor.

2 Elementary commands
Type into your editor window:
% Assign values to names
x = 3
y = 5
z = x + y
Notice again the comment line. Comments are important.
• They remind you of what you were doing. Believe me, you will forget, and figuring things out from the
code can be difficult.
• They explain to others, for example the person grading your test, what you were trying to do.

1
• They speed up searches, when you are looking for some specific piece of code.
Here the comment explains that we are demonstrating how names can be assigned values.
SAVE YOUR FILE AGAIN.
Transfer to the interactive window and type the name of your file. You should see your commands execute.
You should see z = 8.
Now return to the editor window. Type
% If you calculate without giving a name, Matlab stores the result in "ans"
x - y
ans + z
Now you should see ans= −2 and then ans=6. Notice, by the way, that I put spaces around operators like =
and +, because it improves readability. It is especially helpful later, when we get to the dot operator. Often, of
course, the syntax rules will not allow spaces.
The thing that makes Matlab powerful is its ability to create and handle lists of numbers. Everything it does
is handled by making lists of numbers. Naturally, this most important activity can be performed in a number
of different ways.
Method 1 for making lists – Explicit lists.
Type the following into your editor session.
x=[1,3,8,4,-2,6]
y=[1 3 8 4 -2 6]
Save your file and execute it as before. If you check the output, x and y appear to be the same. They are the
same. Both lines of input create the same list. So which one is better?
The first one is better; the one with the commas. The reason is that it is very easy to make a typing mistake
and leave out a space. For example, instead of y above, you type y1=[1 3 8 4-2 6]
Looks the same as above doesn’t it? But save your file and execute. y1 is different, if you typed correctly.
When there is no space between 4 and −2, Matlab interprets 4 − 2 as a piece of arithmetic and replaces 4 − 2
with 2.
Often we want to create very long lists, and then the explicit method does not work very well. Suppose I
want to create a list with 1000 elements. No one is going to type them all in. We use instead:
Method 2 for making lists – start:step:stop. x=[1:3:19] This creates the list [1, 4, 7, 10, 13, 16, 19]. It may
seem easier to use an even briefer notation, namely x=1:3:19
Again if you try the two formats, you will see that you get the same result, and again we can ask which is
better. The form with the brackets, the first form, is recommended. The second form without the brackets does
not work for explicit lists (method 1).
Try typing x = 1,3,8,4,-2,6
You might expect it to produce the same list as above — but Nooo!. Even worse is the attempt to use
x = 1 3 8 4 -2 6
that stops everything. So in conclusion, the recommended methods for creating lists are
x=[1,3,8,4,-2,6]
x=[1:3:19]
There is more to say about the step:start:stop method for lists. We are not stuck with integers. We can use
any numbers. [5.2:0.1:5.6]
and we can use negative numbers: [0.3:-0.1:-0.2]

3 Plotting in Matlab
Suppose we want to make a plot of y = x2 by hand, on paper. We first would make a table of values:
x 0 1 2 3 4 5
y 0 1 4 9 16 25
We do the same thing in Matlab. We make a list of the x values and then a list of the y values, and then we
use the function plot. We type

2
x=[ 0 , 1 , 2 , 3 , 4 , 5 ]
y=[ 0 , 1 , 4 , 9 ,16 ,25 ]
plot(x,y)
When people draw graphs, they join the points together using curved lines, artistically creating smooth curves.
Matlab joins the points using straight lines, and so the plot looks rough. To get a smooth curve, we have to
give Matlab more points, the closer together the points the more smooth the curve will look.
We do not want to type in 100s of points, so we need a way to perform operations on lists of numbers.

4 Arithmetic and functions


For arithmetic there are some important tricks to the syntax. To raise all the numbers in a list to a power, we
must write .∧ and not forget the dot. To take a list of numbers and calculate the reciprocal for each one we
must write ./ and be careful about the dot. Try
7+[1,2,3]
7*[1,2,3]
7 ./ [1,2,3]
[1,2,3] .^ 7
7 .^ [1,2,3]
Functions work on lists in the obvious way. Try sin([pi/4,pi/3,pi/2]).

5 Polynomials
Polynomials can be evaluated easily in Matlab by listing their coefficients. The polynomial

p(x) = an xn + an−1 xn−1 . . . a1 x + a0 is represented as [an , an−1 , . . . a1 , a0 ]

Given p(x) represented as a list p, we calculate p(3) by polyval(p,3). Try p(x) = 2x2 − x + 3 and calculate
p(1) = 4 using polyval.

You might also like