Beruflich Dokumente
Kultur Dokumente
Overview
Matlab Prompt
Help
Help
On search area you can enter the command that we need information
and get syntax, parameters, input and / or output etc.. etc..
For example, writing the command '"plot" you will get information on that
command. In particular, on the left column will be all the items of the
manual where there is information about "plot", clicking on one of these
items you will get the information about the selected item and the right
column .
Variables
Variables
On Matlab should not be defined the type and the size of the variable
before use it (opposite in C, C++, Fortran);
To view and edit a variable is available in the graphical user interface
the Workspace;
Each variable is added to the Workspace after the definition;
Each variable can be deleted from the workspace with the command
--> clear variable_name;
The command "clear all" allows to delete all the variables created by
a single user, however, it strongly discouraged the use of this
command inside a script (with the exception of line 1) because it can
destroy all the defined variables .
Variables examples
Variables examples
Format
Format
short (default) Scaled fixed-point format, with 4 digits after the
decimal point. For example, 3.1416.
long Scaled fixed-point format with 15 digits after the decimal point
for double; and 7 digits after the decimal point for single. For
example, 3.141592653589793.
shortE Floating-point format, with 4 digits after the decimal point. For
example, 3.1416e+000.
longE Floating-point format, with 15 digits after the decimal point for
double; and 7 digits after the decimal point for single. For example,
3.141592653589793e+000.
For more information check Matlab Help
Costants
In Matlab, there are some constants that are always available in the
workspace. To call these constants we must follow the following
syntax:
costant_name
Some constants available on Matlab are the following:
1) pi = 3.1415927...
2) i
3) eps
Costant
Imaginary unit
Floating-point relative accuracy
(releted to the machine used)
4) inf
5) nan
Infinity
NotANumber
4) Pay attention to the size of rows and columns pointing out that
the insertion is done by rows.
1 6 9
A= 15 22 66
17 4 1
a=[a,b,c,d,e,.........,n];
In Matlab there are some commands that allow the insertion of
special matrices such as:
1) eye(n,n) ;
2) zeros(n,m);
3) ones(n,m);
4) triu(X,k);
5) tril(X,k);
The sum of two matrices of the same size can be obtained through
the following statement:
A+B;
The product of two matrices of appropriate dimensions, is obtained by
the following statement:
A*B;
[ ]
1 0 0
B= 0 4 0
0 0 6
1) Determinant;
2) Rank;
3) Exponential matrix;
4) Esponential element-byelement;
5) Then extract the first row and
the second column.
d=[1,4,6]
B=diag(d)
det(B)
rank(B)
expm(B)
exp(B)
B(1,:)
B(:,2)
e =
A
k =0
A
k!
In the case of diagonal matrices the series is equal to the following matrix:
a11
e
A
expm A=e = 0
0
0
e
a22
0
0
a33
e
a11
e
exp A= e 0
0
e
][
a11
e
e
e
a22
0
e
e = 1
0
a33
e
e
1
1
1
a22
e
1
a33
1 e
[ ]
1 2 3
A= 4 5 6
7 8 9
Using the command det(A) we get:
>>det(A)= 6.6613e-16
we can observe as det(A) is close to zero then the command inv(A) could
provide bad results.
0,25 1 0,25
AB= 2
4
1
4,25 7 1,75
Polynomial
Mathematical Functions
round(x)
fix(x)
floor(x)
ceil(x)
sign(x)
Matematical Functions
fix(x)
>> ans = -1
floor(x)
>> ans = -2
-1
ceil(x)
>> ans = -1
Mathematical Functions
abs(x)
sqrt(x)
square root of x
exp(x)
exponential function
Matehmatical Functions
sin(x)
cos(x)
tan(x)
cot(x)
asin(x)
acos(x)
atan(x)
acot(x)
sinh(x)
cosh(x)
tanh(x)
Hyperbolic tangent
of argument in radians
In Matlab, we can define cycles For and While using the following
instructions:
for variable=values
statements 1;
....................;
statements n;
end
while expression
comando 1;
..................;
comando n;
end
If-Elseif-Else
Case
Plot 2D and 3D
In Matlab to plot graphs must first open a graphics window, this is
done using the following command:
figure(num);
where the string 'num' is the number of the graphics window to be
opened and must be a natural number.
To clean the graphics window in use we must use the following
command:
clf(num);
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
'none' No line
Marker Specifiers :
'+' Plus sign
'o' Circle
'x' Cross
(see matlab Help for more informations)
Plot 2D and 3D
plot(x,y,':*g'); grid
Plot 2D and 3D
hold off
Plot 2D and 3D
Plot 2D and 3D
ezplot(fun)
Example:
clear all
clc
figure(1)
ezplot('x.^2-y.^4-1',[-4 4 -4 4]); grid
figure(2)
ezplot('cos(t)','sin(t)'); grid
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
x=0.1:0.1:2;
subplot(2,3,1) , plot(x,x,'r');grid
title('y=x');xlabel('x'); ylabel('y');
subplot(2,3,2), plot(x, x.^2,'b');grid
title('y=x^2');xlabel('x'); ylabel('y');
subplot(2,3,3), plot(x,sin(x),'y');grid
title('y=sin(x)');xlabel('x'); ylabel('y');
subplot(2,3,4), plot(x,exp(x),'m');grid
title('y=exp(x)');xlabel('x'); ylabel('y');
subplot(2,3,5), plot(x,log(x),'g');grid
title('y=log(x)');xlabel('x'); ylabel('y');
subplot(2,3,6), plot(x,cos(x),'c');grid
title('y=cos(x)');xlabel('x'); ylabel('y');
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
Plot 2D and 3D
x=-1:0.1:1
FUNCTION:
To create a function in Matlab, we can use a procedure similar to that
used in the script. The function must also follow the following
structure:
function[out1,........,outn]=function_name(input 1,.....,input n)
statement 1;
............... ;
statement n;
endfunction
Exercize Script
Exercize Script
After opening the matlab editor will insert the following instructions:
clear all;
clc;
x=-2:0.1:2;
y=x;
[X Y]=meshgrid(x,y);
Z=X+exp(Y);
figure(1)
surf(X,Y,Z); xgrid
title(' Z=X+exp(Y)');
xlabel('x');
ylabel('y');
Exercize Script
Exercize Script
Exercize Script
Exercize Script
clear all;
clc;
coef=[1,3,-2];
roots(coef)
x=-5:0.1:5;
y=x^2+3*x-2;
figure(1);
plot(x,y,'r');grid
title('Function y=x^2+3*x-2');
xlabel('x');
ylabel('y');
Esempi script
Exercize Function
x=[1, 3, 4, 8, 2]