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

)

DEPARTMENT OF AVIONICS ENGINEERING

SUBJECT : SIGNALS & SYSTEMS LAB

LAB NO : 02

TITLE : Introduction to MATLAB – II

SUBMITTED TO : Ms. Maha Intakhab Alam


SUBMITTED BY : MAHNOOR
BATCH : Avionics 07
SECTION : B
Marks Obtained :

Remarks:

DEADLINE:

DATE OF SUBMISSION: 7th March,2024


Instructions
Students are instructed to complete these exercises on their own. Paste the code and output
snapshots both for each exercise with proper heading and formatting. Copied work/tasks will
result in ZERO for the whole lab report.

Exercises
E.1.
Create a symmetric matrix ‘m’ with five rows and five columns. Calculate its
Transpose and verify that the matrix is symmetric by observing that m=m’.

INPUT:
m = zeros(5); % Create a 5x5 matrix (initially filled with zeros)
upper_triangle_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; % Define the values for the upper
triangle (excluding the diagonal)
for i = 1:5 % Fill both upper and lower triangles simultaneously
m(i, i:end) = upper_triangle_values(1:6-i);
m(i:end, i) = upper_triangle_values(1:6-i);
end
disp('Symmetric Matrix:'); % Display the symmetric matrix
disp(m);
disp('Transpose of m');
disp(m');
if m==m'
disp('The matrix is symmetric.');
else
disp('The matrix is not symmetric.');
end

1
OUTPUT:

E.2.
Create a random matrix with 3 rows and 5 columns. The matrix values should be
integer b/w (0—100), than change its 2nd row values by ‘7’.
The rand function in MATLAB generates uniformly distributed random numbers in the interval
(0,1). When you multiply the result by 100, you effectively scale these numbers to be in the range
from 0 to 100.
INPUT:
% Generate a 3x5 matrix of random numbers between 0 and 100 and round towards zero
M=fix(100*rand(3,5))
M(2,1:5)=7

2
OUTPUT:

E.3.
Define

𝟏 + 𝟐𝒋 𝟑
d=[ ] and e = [𝟐 + 𝟑𝒋 𝟓 + 𝟓𝒋]
𝟕 𝟓𝒋
𝟎 𝟏 − 𝟖𝒋
and calculate
(a) c=d+e
(b) c=d-e
(c) c=d*e
(d) c=d/e
Note: multiplication and division should be done element by element.

INPUT:
d = [1+2j 3 ; 7 5j]
e = [2+3j 5+5j ; 0 1-8j ]
c = d + e;
disp('d+e=');
disp(c);
c = d - e;
disp('d-e=');
disp(c);
3
c = d.*e;
disp('d.*e=');
disp(c);
c = d./e;
disp('d./e=');
disp(c);

OUTPUT:

4
E.4.
Plot the function y=3*exp (3*pi*t); Choose t from (0---150) with increment of
0.001
INPUT:
t=0,0.001,150;
y=3*exp(3*pi*t);
plot(y)
xlabel(‘t’)
ylabel(‘y(t)’)
OR
AS,

INPUT:

OUTPUT:

5
E.5.
Define a vector a = [1 2 3 4 5] and then using a “for” loop create another vector
‘b’ having the following values corresponding to ‘a’

INPUT:
a = [1 2 3 4 5];
b=a.^a;
disp(b);

OUTPUT:

E6.
Without using a “for” loop and using some alternative method define vector
a = [1 2 3 4 5] and create b1 = [11 22 33 44 55]
INPUT:
a = [1 2 3 4 5];
b1 = a * 11; % Create b1 by multiplying each element of a by 11
disp(b1);
OUTPUT:

E.7.
Indicate the error in the following. Tell the line # which contains the error and
what is the error.
1.......v=0:2:10

6
2.......for c=1:length(v)
3........ v(c-1)=v(c)+5;
4........end
5........v

SOLUTION:
INPUT:
v = 0:2:10;
for c = 2:length(v)
v(c-1) = v(c) + 5;
end
disp(v);
OUTPUT:

E.8.
We have vector
x = [20 40 60 80 100 120 140 160 180 200] and we want to multiply all the
elements of the vector ‘x’ e.g. (20 x 40 x 60 … … … … . x 200) and store the
result in temp.

7
INPUT:
x = [20 40 60 80 100 120 140 160 180 200] ;
temp=1;
for i=1:length(x)
temp = temp*x(i);
end
disp(temp);

OUTPUT:

E.9.
Refer to “example 6”. Change the code so that it creates vector ‘b’ having
values
INPUT:
b(1,1:10)=5;
i=1:10
b=b.^i;
disp(b);

OUTPUT:

OR
a=1:10
i=1;
while(i<=10)

8
b(i)=5^a(i);
i=i+1;
end
b

OUTPUT;

E.10.
Write the MATLAB code with input n, it should generate the following
sequence(n=30). [-1 -2j 3 4j -5 -6j 7 8j… … up to nth element]

INPUT:
n = 30; % Define the value of n
temp = 1:n; % Create an array from 1 to n
a = mod(temp, 2) == 0; % Identify even indices in the array
temp(a) = temp(a) * 1j; % Multiply even indexed elements by the imaginary unit j
for i = 1:2:n % Loop through every other element starting from the first element
temp(i) = temp(i) * (-1)^((i+1)/2); % Multiply the element by (-1)^((i+1)/2)
temp(i+1) = temp(i+1) * (-1)^((i+1)/2); % Multiply the next element by (-1)^((i+1)/2)
end
result = temp; % Assign the result to the variable result
disp(result); % Display the result

9
OUTPUT:

Conclusion:
From this assignment, we have learned how to use the basic operations that we have studied in
lab 1 and how to use loops in different scenarios. how to plot different functions.We have also
learned about arrays and perform different operations on it.
Overall, this lab has provided hands-on experience with MATLAB's matrix manipulation and on
other topics capabilities and reinforced the importance of understanding and documenting code.

END

10

You might also like