Getting started with Matlab

advertisement
Getting started with Matlab
Launch Matlab by either clicking twice on the Matlab icon on your desktop or by
selecting: Start  Programs  Matlab  Matlab. The main window is the
MATLAB Command Window, where you write your instructions.
The sign ‘» ’ below indicates that what follows comes from the MATLAB Command
Window. In the MATLAB Command Window, Matlab will execute the instructions
after ‘» (command)’ when you press ‘enter’.
To save your work, it is usually easier to highlight whatever you want to save on the
MATLAB Command Window and to paste it on the Notepad than to use the Matlab
function ‘diary’. If you absolutely want to use the Matlab ‘diary’, type
» diary name_of_file
at the very beginning of your Matlab session and then
» diary off
at the very end of your Matlab session.
I Numbers and variables
(1) Basic operations:
- Addition and subtraction
» 2+3
» 2-3
- Multiplication and division
» 2*3
» 2/3
- To the power of:
» 2^3
(2) Creating simple variables:
» a=2
creates a variable called ‘a’ and allocates the value 2 to this variable. You can perform
the basic operations on the variables:
» b=(a^3)/(5-2*a^0.5)
The Matlab command
» who
displays the name of the variables, while the Matlab command
» whos
displays the name and the characteristics of the variables.
(3) Creating vectors and matrices:
» U=[1,2,3,4]
creates a row vector with four entries. Entries are separated by a coma.
1
To create a column vector, use semicolon instead:
» V=[1;2;3;4]
The command
» D=5:10
creates a row vector that contains the numbers 5,6,…,10, whereas the command
» D=5:0.2:6
creates a row vector that contains the numbers 5, 5.2, 5.4, 5.6, 5.8, 6.
Try to keep these two Matlab commands in mind, as they will be useful later for
plotting graphs.
You can also create matrices:
» A=[1,2;3,4]
In this particular example, ‘A’ is a 2 by 2 matrix. On each row, entries are separated
by a coma and rows are separated by a semicolon.
You can also perform some basic operations with vectors and matrices, although you
have to be a lot more careful. One basic operation that is always safe with vectors and
matrices is the multiplication by a number:
» 2*A
Remark: one of the great advantage of Matlab over Derive is that Matlab is a lot
easier to use when it comes to dealing with matrices and vectors. When it comes to
formal calculus and algebra, however, Derive is definitely more convenient.
(4) Questions
(i) In the MATLAB Command Window, type the following:
» u=5
» u=5;
What is the effect of the semicolon?
(ii) In the MATLAB Command Window, type the following:
» a=3;
» b=(2+3a)^2
What is the problem? Can you fix it?
Remark: you would not have encountered this problem with Derive.
(iii) In the MATLAB Command Window, type the following:
» A^2
» A.^2
What is the difference between ‘^’ and ‘.^’?
Type
» U=[1,2,3,4];U.^2
Now try
» U=[1,2,3,4];U^2
What is the problem?
The distinction between ‘(operation)’ and ‘.(operation)’ is likely to give you a
headache sooner or later.
2
II Plot a graph
(1) Basic 2-D plot
Let’s see first how it works and then we’ll have closer look.
» x=-2:0.001:2;y=x.^2;plot(x,y)
plots the graph of the function y  x 2 for  2  x  2 .
As seen previously, the command
» x=-2:0.001:2;
creates a row vector x containing the numbers -2, -1.999, -1.998, -1.997, …, 2.
The command ‘y=x.^2;’ creates a row vector y containing the square of the entries of
the vector x . The command ‘plot’ in itself traces a line between the points ( x, y ) , for
x  2,1.999,1.998,...,2 .
The process becomes more apparent if you modify the vector x . Try:
» x=-2:1:2;y=x.^2;plot(x,y)
» x=-2:0.5:2;y=x.^2;plot(x,y)
You can also plot several graphs at the same time:
» x=-2:0.001:2;y=x.^2;plot(x,y);
» hold on
» x=-2:0.001:2;y=x;plot(x,y);
Remark: when plotting a graph, it is imperative to use ‘.(operation)’ in most cases.
When it comes to graph, you can always assume that you have to use ‘.(operation)’.
(2) Titles, labels and annotations
Type
» x=-2:0.001:2;y=2.*x.^2;plot(x,y);
» xlabel('-2 \leq x \leq 2');
» ylabel('y=x^2');
» title('Plot of y=x^2');
» text(1.5,2*1.5^2,'\leftarrow 2*1.5^2','HorizontalAlignment','left')
On the second line, those of you who are familiar with LaTex have certainly noticed
a LaTex command: ‘\leq’. It stands for ‘less than or equal to’. You can also use the
standard ‘<’ from the keyboard (not as nice, though).
The first four lines are essential. You can more or less forget about the fifth line
unless you want to impress someone with your knowledge of Matlab. Note that you
can save you graph the usual way.
On the fifth line, ‘text(1.5,2*1.5^2,…)’ indicates the coordinates of the text.
‘\leftarrow’ is again a LaTex command that displays a small arrow pointing left. If
you want to know what the rest means, try:
3
»x=-2:0.001:2;y=2.*x.^2;plot(x,y);
» xlabel('-2 \leq x \leq 2');
» ylabel('y=x^2');
» title('Plot of y=x^2');
» text(1.5,2*1.5^2,'\leftarrow 2*1.5^2','HorizontalAlignment','right')
If you want to insert a text in your plot, there is a more simple, if less fancy, way to do
it:
In your plot window, go to Tools  Add  Text, and then left click where you
want to put the text. Then write it down.
(3) Exercises
(i)
(ii)
(iii)
Plot the graph of the function y  sin( x) for    x   (hint: use ‘pi’).
Plot the graph of the function y  x 3  3x 2  2 x  1 for  5  x  5 .
Plot the graph of the function y  cos( 2 x  2 ) for    x   and label the
x and y axis. Give your graph a title.
III More about matrices and vectors
(1) Predefined matrices:
The following Matlab commands are essentially useful when you need to work with
large matrices (in “real life” situation, it could mean 3000000000 by 2458796514
matrices and you don’t necessarily want to write the entries one by one…).
Try
» eye(3)
» eye(3,4)
eye(3) creates a 3 by 3 matrix with ones on the diagonal and zeros elsewhere, while
eye(3,4) creates a 3 by 4 matrix with ones on the “diagonal” (that is, a i ,i =1 and
a i , j  0 for i  j )
The following commands are self-explanatory:
» ones(2)
» ones(2,4)
» zeros(4)
» zeros(4,2)
Let’s combine a few instructions:
» B=[ones(1,3);zeros(2,3)]
How do you explain the resulting matrix?
4
(2) Working with the entries
Type:
» A=[1,2,3;4,5,6;7,8,9]
and then try the following commands:
» A(1,:)
» A(2,3)
» A(:,2)
Try to explain the effect of these commands.
(3) Exercises
(i)
Extract the second row of A, the first column of A and then the entry on the
second row and first column of A.
(ii)
Create a 12 by 12 matrix with entries equal to zero everywhere except one the
first column where the entries are equal to 1.
Something doesn’t work the way it should? Try
» why
Feeling better?
5
Download