Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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.
II Plot a graph
(1) Basic 2-D plot
Lets see first how it works and then well 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=-2:1:2;y=x.^2;plot(x,y)
x=-2:0.5:2;y=x.^2;plot(x,y)
x . Try:
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 3 x 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.
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.