|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
(2) Creating simple variables:
creates a variable called ‘a’ and allocates the value 2 to this variable. You can perform the basic operations on the variables:
The Matlab command
displays the name of the variables, while the Matlab command
displays the name and the characteristics of the variables.
(3) Creating vectors and matrices:
creates a row vector with four entries. Entries are separated by a coma.
To create a column vector, use semicolon instead:
creates a row vector that contains the numbers 5,6,…,10, whereas the command
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:
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:
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.
(i) In the MATLAB Command Window, type the following:
What is the effect of the semicolon?
(ii) In the MATLAB Command Window, type the following:
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:
What is the difference between ‘^’ and ‘.^’?
What is the problem?
The distinction between ‘(operation)’ and ‘.(operation)’ is likely to give you a headache sooner or later.
II Plot a graph
Basic 2-D plot
Let’s see first how it works and then we’ll have closer look.
plots the graph of the function for .
As seen previously, the command
creates a row vector containing the numbers -2, -1.999, -1.998, -1.997, …, 2.
The command ‘y=x.^2;’ creates a row vector containing the square of the entries of the vector. The command ‘plot’ in itself traces a line between the points , for .
The process becomes more apparent if you modify the vector . Try:
You can also plot several graphs at the same time:
» hold on
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
» xlabel('-2 \leq x \leq 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:
» xlabel('-2 \leq x \leq 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.
Plot the graph of the function for (hint: use ‘pi’).
Plot the graph of the function for .
Plot the graph of the function for and label the and 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…).
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, =1 and for )
The following commands are self-explanatory:
Let’s combine a few instructions:
How do you explain the resulting matrix?
Working with the entries
and then try the following commands:
Try to explain the effect of these commands.
Extract the second row of A, the first column of A and then the entry on the second row and first column of A.
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