Introduction to Matlab
Introduction to Matlab
By: Awash T.
High Level
Languages such as
C, Pascal etc.
Assembly
Introduction to Matlab By:
04/17/25 Awash T. 3
Strengths of MATLAB
• Relatively easy to learn. Easy to interpret and fix
errors
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
Or
Matlab
Desktop
Press to create
new m-file in the
matlab editor
Introduction to Matlab By:
04/17/25 Awash T. 8
Functions
Programming in Matlab.
Users can write functions which can be called from the command line.
Functions can accept input variable(s)/matrice(s) and will output
variable(s)/matrice(s).
In Matlab functions closely resemble scripts and can be written in the
Matlab editor. Matlab functions have the function keyword.
Remember that the filename of a function will be its calling function name.
Don’t overload any built-in functions by using the same filename for your
functions or scripts!
function keyword
Matlab comment
prompt suppress operator
assign
command
operator
output Try the same line without the
semicolon and comments
Introduction to Matlab By:
04/17/25 Awash T. 11
Workspace
The workspace is Matlab’s memory
Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c=
22
>>
Introduction to Matlab By:
04/17/25 Awash T. 12
Creating vectors
Coulmn separator:
Row separator: Semicolon (;)
space/coma (,)
Creating sequences:
• From : jump: till
• linespec(X1, X2, N)
generates N points between
X1 and X2.
Introduction to Matlab By:
04/17/25 Awash T. 13
Array, Matrix
a vector x = [1 2 5 1]
x =
1 2 5 1
a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x =
1 2 3
5 1 4
3 2 -1
transpose y = x’ y =
1
2
5
1
t =
1 2 3 4 5 6 7 8 9 10
k =2:-0.5:-1
k =
2 1.5 1 0.5 0 -0.5 -1
B = [1:4; 5:8]
B =
1 2 3 4
5 6 7 8
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Introduction to Matlab By:
04/17/25 Awash T. 18
Concatenation of Matrices
x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1 2 4 5
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
ans=
indices of matrix element(s)
2
Remember Matrix(row,column)
A=
>>A(3,2:3) 3 2 1
ans = 5 1 0
2 1 7
1 7
>>A(:,2)
ans =
2
1
What’ll happen if you type A(:,:) ?
1
Introduction to Matlab By:
04/17/25 Awash T. 21
Manipulating Matrices A=
3 2 1
5 1 0
2 1 7
B=
Create matrices A and B and try out the the matrix operators in this slide
Introduction to Matlab By:
04/17/25 Awash T. 22
The use of “.” – “Element”
Element
Operation
A= [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 -1
b = x .* y c=x./y d = x .^2
x = A(1,:) y = A(3 ,:)
b= c= d=
x= y= 3 8 -3 0.33 0.5 -3 1 4 9
1 2 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.
Introduction to Matlab By:
04/17/25 Awash T. 23
MATLAB Special Variables
pi Value of
inf Infinity
Logic Control:
IF / ELSEIF / ELSE
SWITCH / CASE / OTHERWISE
Iterative Loops:
FOR
WHILE
Introduction to Matlab By:
04/17/25 Awash T. 27
The if, elseif and else
•statements
Works on Conditional statements
• Short-circuited in MATLAB - once a
condition is true, the sequence
if
if II ==
== JJ
terminates.
A(I,J)
A(I,J) == 2;
2;
elseif
elseif abs(I-J)
abs(I-J) ==
== 11
A(I,J)
A(I,J) == -1;
-1;
if (Condition_1) else
else
Matlab Commands A(I,J)
A(I,J) == 0;
0;
end
end
elseif (Condition_2)
Matlab Commands
elseif (Condition_3) »if_examp
Matlab Commands
else
Matlab Commands
Introduction to Matlab By:
end
04/17/25 Awash T. 28
Switch, Case, and Otherwise
switch
switch input_num
input_num
More efficient than elseif statements case
case -1
-1
input_str
input_str == 'minus
'minus one';
Only the first matching case is one';
case
case 00
executed input_str
input_str == 'zero';
'zero';
case
case 11
input_str
input_str == 'plus
'plus one';
one';
case
case {-10,10}
{-10,10}
input_str
input_str == '+/-
'+/- ten';
ten';
otherwise
otherwise
input_str
input_str == 'other
'other value';
value';
end
end
»switch_examp
N=10;
N=10;
• Similar to other programming
for
for II == 1:N
1:N
languages for
for JJ == 1:N
1:N
A(I,J)
A(I,J) == 1/(I+J-1);
1/(I+J-1);
• Repeats loop a set number of times
end
end
(based on index) end
end
»array_vs_loops
MATLAB will plot one vector vs. another. The first one
will be treated as the abscissa (or x) vector and the
second as the ordinate (or y) vector. The vectors have
to be the same length.
MATLAB will also plot a vector vs. its own index. The
index will be treated as the abscissa vector. Given a
vector “time” and a vector “dist” we could say:
>> plot (time, dist) % plotting versus time
>> plot (dist) % plotting versus index
>> plot(t,y,‘b.-')
Investigate the function
>> y=A*cos(w*t+phi);
for different values of phi (eg: 0, pi/4, pi/3,
pi/2), w (eg: 1, 2, 3, 4) and A (eg: 1, 0.5, 2). Use
the hold on Matlab command to display your
plots in the same figure. Remember to type A = amplitude
hold off to go back to normal plotting mode. phi = phase
Try using different plot styles (help plot) w = angular frequency = 2*pi*frequency
Introduction to Matlab By:
04/17/25 Awash T. 36
Plots
MATLAB is very good at visualizing mathematical
functions. Use “plot” command for the basic plot
1
0.8
>> xlabel('X')
sin(X)
0
>> ylabel('sin(X)') -0.2
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
X
1
sin(X)
0.8 cos(X)
0.6
>> x=-8:0.1:8;
0.4
>> y=sin(x);
>> z = cos(x); 0.2
>> plot(x,y,'+-',x,z,'o-') 0
-0.4
-0.6
-0.8
-1
-8 -6 -4 -2 0 2 4 6 8
>>x=linspace(0,4*pi,100);
0.8
0.6
>>y=sin(x); 0.4
0.2
-0.4
-0.6
>>plot(y) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
>>plot(y2) 0.6
0.5
0.4
0.3
0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
0.6
0.5
plot(.) 0.4
0.3
0.2
Example: 0.1
>>x=linspace(0,4*pi,100); 0
>>y=sin(x); -0.1
>>plot(y) -0.2
>>plot(x,y) -0.3
0 10 20 30 40 50 60 70 80 90 100
0.7
stem(.) 0.6
0.5
0.4
0.3
0.2
Example: 0.1
>>stem(y) 0
>>stem(x,y) -0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
title(.)
This is the sinus function
>>title(‘This is the sinus function’) 1
0.8
xlabel(.) 0.6
0.4
sin(x)
0
ylabel(.) -0.2
-0.4
-0.6
-0.8
>>ylabel(‘sin(x)’) -1
0 10 20 30 40 50 60 70 80 90 100
x (secs)