Introduction to MATLAB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Introduction to MATLAB

By
Swarna Laxmi Panda
What is MATLAB
The name MATLAB stands for MATrix LABoratory.

MATLAB is a software package for high-performance numerical computation and visualization.


It provides an interactive environment with hundreds of built-in functions for technical
computation, graphics, and animation.
MATLAB windows
(1) Command Window
(2) Editor window
(3) Figure window
(4) Workspace
(5) Command History
(6) Current Folder
clc, clear and close all
clc : clears all the text from the Command Window, resulting in a clear screen.
clear : clear all variables from the current workspace
close all :closes all figures
Workspace
Save: save workspace variables to .mat file
Load: load variables from .mat file
Clear: clear workspace
Variables Naming
Two rules:
(1) It may consist only of the letters a–z, the digits 0–9 and the underscore ( _ ).
(2) It must start with a letter.
(3) Variable names are case-sensitive
Scalar, Vector and Matrix representations :
Vector in form of Row matrix----- a=[1 2 3 4]; or a=[1,2,3,4];
 Vector in form of column matrix----- a=[1;2;3;4];

a=[1 2 3]; b=[2 3 4]; c=[5 6];


f=[a b]; g=[a;b]; h=[a b c];
i=[a;b;c]; (dimension mismatching)
Matrix: x=[1 2 3;4 5 6;7 8 9];
Array initialization
>> A=[1 2 3 4 5 6 7 8 9 10]
A=
1 2 3 4 5 6 7 8 9 10
>> A=1:1:10
A=
1 2 3 4 5 6 7 8 9 10
>> A=linspace(1,10,10)
A=
1 2 3 4 5 6 7 8 9 10
Accessing array/matrix elements
A is a matrix of size (M×N)
1. Accessing single element of A: A(ri , cj) – Element of ith row and jth column
2. Accessing a particular row: A(ri , :) – All the Element of ith row
3. Accessing a particular column: A(: , cj) – All the Element of jth column
4. Extracting a sub-matrix: A(ri : rj , ci : cj)
5. size(A)
6. diag(A)
7.triu(A)
8.tril(A)
9. mean(A)
>> A=[1 2 3 ; 4 5 6; 7 8 9] >> A(2,3)

A= ans =
1 2 3 6
4 5 6
7 8 9
>> A(2,:) >> A(:,3) >> A(2:3, 2:3)
ans = ans = ans =
4 5 6 3 5 6
6 8 9
9
Concatenation of matrices
X=[1 2] Y=[3 4] >> a=[1 2] >> b=[3 4]
f=[x y]
a= b=
g=[x ; y]
1 2 3 4

>> f=[a b]
f=
1 2 3 4
>> g=[a ; b]
g=
1 2
3 4
Special matrices
1. eye(n) -Identity matrix
2. zeros(m,n)
3. ones(m,n)
4. rand(n)
MATHEMATICAL OPERATIONS:
Trigonometric Operations:
Sine-sin()
cosine-cos()
tangent-tan()
Inverses are:- asin(), acos(), atan().
Angle in radian only.
Exponential Operations :
exp()
log()
 log10()
log2()
.^
LOGICAL OPERATIONS:
Comparisons operators : Logical Expressions :
Other Functions :
round(x) ---- Rounds a number to the nearest integer
ceil(x) ------ Rounds a number up to the nearest integer
floor(x)----- Rounds a number down to the nearest integer
fix(x) ------Rounds a number to the nearest integer towards zero
rem(x,y) -----The remainder left after division
mod(x,y) ----The signed remainder left after division
abs(x) -----The absolute value of x
sign(x) -----The sign of x
display plots
disp(a) Continuous plot
plot(x ,y )
plot (x,y,’ro’)
Discrete plot
Stem(x,y)
Task to be done
Basic Matrix operation: a) Accessing elements, addition, multiplication, plotting row and columns, b) Plot continuous and discrete signals,
c) generate prime numbers up to 50, d) Matrix operations: Transpose, inversion, solving over determined set of equations,

Q1. Display a 4*3 matrix (4 rows, 3 columns) in MATLAB as following.

1 2 3
𝐴= 0 9 8
−2 −4 1
6 7 −4

Q2. Write the codes for displaying the following elements from the above matrix using MATLAB

a) A (4, 3) b) A (2, 2)

c) All elements from all columns in 1st row (A (1, :))

d) All elements from all rows in 3rd column (A (:,3))


Q3. Generate a 3*2 matrices, A, B as following.

2 3 −4 6
A= 0 9 B = 23 −7
−2 −4 1 2
Then find
a) A+B, b) A-B, c) AB, d)BA
04. A= [1 2 3 4 5 6 7 8 9 10]
a. Down sample by 2
b. Up sample by 2
c. Addition of all elements of an array
d. Averaging

Q4. Generate the following signals in continuous form


a) sin(2𝜋100𝑡), b)cos(2𝜋100𝑡), c) Step signal, d) Impulse signal, e) Ramp signal
Q5. Generate the above signals in 4th question in discrete form. Take 500 points in time interval of 5T (Time
period if it is applicable).
Flow control
If
for
while
Switch case
Break
return
If-else
x=1; y=2;
if x == y
disp( 'x is equal to y')
elseif x>y
disp( 'x is greater than y')
else
disp( 'x is smaller than y')
end

Output:x is smaller than y


For loop
for ii=1:10
fprintf('%d',ii);
end

Output:1 2 3 4 5 6 7 8 9 10
While loop
ii=1;
while(ii<=10)
fprintf('%d',ii);
ii=ii+1;
end

Output:1 2 3 4 5 6 7 8 9 10
Switch case
a=[1 2; 3 4]; b=[1 2;4 5]
x=input('enter the case value') enter the case value1
switch x
case 1 x=
y=a+b
case 2 1
y=a-b
case 3 y=
y=a*b
otherwise 2 4
disp('No operation') 7 9
end
Q7. Generate a 3×3 matrix, A as following.

1+𝑗 2−𝑗 3
A= 0 9 + 2𝑗 8
−2 + 𝑗 −4 1−𝑗
Then find
a) Determinant of A
b) Conjugate Matrix of A
c) Transpose Matrix of A
d) Hermitian matrix of A
e) Inverse Matrix of A
Q8. Write a MATLAB program to generate the Prime Numbers between 1 and 50. Learn “For”, “While” loop, and “IF-
ELSE” conditions.

You might also like