Introduction to MATLAB
Introduction to MATLAB
Introduction to MATLAB
By
Swarna Laxmi Panda
What is MATLAB
The name MATLAB stands for MATrix LABoratory.
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,
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)
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
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.