Beruflich Dokumente
Kultur Dokumente
What is Matlab?
Assembly
Command Window
Command Window
Workspace
Double click on m
variable (m is a 3x4 matrix)
Command History
Functions in MATLAB
You do not need always writing new functions.
There are also embedded functions in MATLAB.
Write help command in the command window
and see the list of help topics of MATLAB.
If you would like to get some knowledge for any
function, write help function_name in
command window. For example, write help
factorial .
Now, use factorial function to compute factorial
of 5 by using the given knowledge.
Functions in MATLAB
Another example: Write help linspace in
command window.
Can you say what linspace command does ?
Write in command window:
t=linspace(0,1,100)
What will the content of t be?
There are lots of other functions in MATLAB.
.m files
(Scripts, Functions)
.mat files
(Parameters,
Variables)
Array, Matrix
a vector
x = [1 2 5 1]
x =
1
a matrix
x = [1 2 3; 5 1 4; 3 2 -1]
x =
1
5
3
transpose
2
1
2
3
4
-1
y = x
y =
1
2
5
1
t =1:10
t =
k =2:-0.5:-1
k =
1.5
0.5
= [1:4; 5:8]
x =
1
5
2
6
3
7
4
8
-0.5
-1
10
Matrix Index
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Operators (arithmetic)
+
*
/
^
addition
subtraction
multiplication
division
power
complex conjugate transpose
Matrices Operations
Given A and B:
Addition
Subtraction
Product
Transpose
.*element-by-element multiplication
./ element-by-element division
.^element-by-element power
x = A(1,:)
x=
c=x./y
d = x .^2
b=
c=
0.33 0.5 -3
d=
y = A(3 ,:)
y=
1 2 3
b = x .* y
3 8 -3
3 4 -1
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
1 4 9
Determinant of a matrix
Power of a matrix
Power of a matrix
Ploting in MATLAB
Create an x-array of 10 samples between 0
and 1.
>>x=linspace(0,1,10);
Plot the y
>>plot(x,y)
>>grid
Ploting in MATLAB
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Ploting in MATLAB
xlabel('x')
ylabel('y')
title(y=x')
and open the Figure 1 again.
Now write
Ploting in MATLAB
Ploting in MATLAB
Write in command window hold now.
This command will overwrite the new plot on
to current figure without delete it.
At this step, we will use stem command
instead of plot.
Now, write stem(x,y, 'r');
Open Figure 1 again.
Ploting in MATLAB
y=x
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0.1
0.2
0.3
0.4
0.5
x
0.6
0.7
0.8
0.9
Open a new figure by using figure comand and do the same plot for 100 points.
Ploting in MATLAB
There are several properties of plot command.
To learn these properties, remember that
there is a help command in MATLAB.
Learn how to use the commands related to
plot command such as axis, subplot, ...
== Equal to
~= Not equal to
< Strictly smaller
> Strictly greater
<= Smaller than or equal to
>= Greater than equal to
& And operator
| Or operator
Flow Control
if
for
while
break
Control Structures
If Statement Syntax
if (Condition_1)
Matlab Commands
elseif (Condition_2)
Matlab Commands
elseif (Condition_3)
Matlab Commands
else
Matlab Commands
end
Control Structures
Some Dummy Examples
for i=1:100
Some Matlab Commands;
end
for j=1:3:200
Some Matlab Commands;
end
for m=13:-0.2:-21
Some Matlab Commands;
end
for k=[0.1 0.3 -13 12 7 -9.3]
Some Matlab Commands;
end
Control Structures
While Loop Syntax
while (condition)
Matlab Commands
end
Dummy Example
while ((a>3) & (b==5))
Some Matlab Commands;
end
If Example
function compare(x,y)
if x>y
display('x is greater than y');
else if x<y
display('y is greater than x');
else
display('x is equal to y');
end % Ending for else if
end % Ending for if
If example
function compare(x,y)
if x>y
display('x is greater than y');
else if x<y
display('y is greater than x');
else
display('x is equal to y');
end
end
% Ending for if
For example
for i=1:100
x(i)=i^2 ;
end
For example
Another Example
x=10*randn(1,100);
for i=1:100
if x(i) > 0
y(i)=1;
elseif x(i)<0
y(i)=-1;
else
y(i)=0;
end
end
Another Example
Another Example
Another Example
Now, get the same result of y by using the
MATLAB function sign(x)
Write in command window help sign.
Learn how to use sign command and use it for
determining the sign of elements of x.
MATLAB HW#1
1. Solve the following linear system
equation using Cramer Rule in MATLAB.
MATLAB HW#1
2. By applying
MATLAB HW#1
3. Find a matrix X such that AX=B for the given A
and B matrices below