You are on page 1of 10

Introduction to MATLAB LAB 1

Signals and Systems


(EE-223)

LABORATORY MANUAL

INTRODUCTION TO MATLAB
(LAB # 01)
Engr. Fakhar Abbas

Engr. Nimra Fatima


Student Name: MUHAMMAD SARMAD FOWAD
Roll No: 22i-2222 Section: D
Date performed: 24nd January, 2024

MARKS AWARDED: ________ / 10

_________________________________________________________________________________________________________________________________________________________

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES, ISLAMABAD


Lab # 01: Introduction to MATLAB
SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 1 of 10
Introduction to MATLAB LAB 1
Learning Objectives:
In this lab, you will learn:
 The interface of MATLAB
 Basic arithmetic operators
 Matlab variables
 Matrix manipulation

Software Used: MATLAB

Exercises:-
NOTE:
 Solve these questions in MATLAB and write answers/code in the manual.
 Submit your manual to lab instructor before leaving the lab.

Q.1 Run the MATLAB help desk by typing helpdesk. The help desk provides a hypertext interface to
the MATLAB documentation.

>> doc

Answer:

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 2 of 10


Introduction to MATLAB LAB 1
Q.2 Use MATLAB as a calculator. Try the following:
 pi*pi – 10
 sin(pi/4)
 ans ˆ 2 %<--- "ans" holds the last result

ANS:

result1=pi^2 - 10;
pi^2 - 10;
result2=sin(pi/4);
sin(pi/4);
result3 = ans^2;
disp(['pi^2 - 10: ' num2str(result1)]);
disp(['sin(pi/4): ' num2str(result2)]);
disp(['(ans)^2: ' num2str(result3)]);

Q.3 Create a vector


a. ‘A’ of even whole numbers between 31 and 75.
b. ‘B’ of odd whole numbers between 75 and 131.

ANS:

% Create vector 'A' of even whole numbers between 31 and 75


A = 32:2:74;
% Create vector 'B' of odd whole numbers between 75 and 131
B = 75:2:131;
% Display the vectors
disp('Vector A (even numbers between 31 and 75):');
disp(A);
disp('Vector B (odd numbers between 75 and 131):');
disp(B);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 3 of 10


Introduction to MATLAB LAB 1

Q.4 Complex numbers are natural in MATLAB. The basic operations are supported. Try the following:
z = 3 + 4i, w = -3 + 4j
 real(z), imag(z)
 abs([z,w]) %<-- Vector constructor
 conj(z+w)
 angle(z)
 exp( j*pi )
 exp(j*[ pi/4, 0, -pi/4 ])

Q.5 Make sure that you understand the colon notation. In particular, explain in words what the following
MATLAB code will produce.
 jkl = 0 : 6
 jkl = 2 : 4 : 17
 jkl = 99 : -1 : 88
 ttt = 2 : (1/9) : 4
 tpi = pi * [ 0:0.1:2 ];

Q.6 Extracting and/or inserting numbers into a vector are very easy to do. Consider the following definition
of xx:
 xx = [ zeros(1,3), linspace(0,1,5), ones(1,4) ]
 xx(4:6)
 size(xx)
 length(xx)
 xx(2:2:length(xx)
Explain the results echoed from the last four lines of the above code.

Q.7 Enter the following in MATLAB.

23 16

64 62
A= ,B=
Find AB, and A-1.

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 4 of 10


Introduction to MATLAB LAB 1

TASKS:
Matrix Manipulation:
A. Generate a 6x6 matrix A with magic command and replace first column with:
1
-2
-3
-4
-5
-6

ANS:

A = magic(6);
re = [-1; -2; -3; -4; -5; -6];
A(:, 1) = re;
disp('Original Matrix A:');
disp(A);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 5 of 10


Introduction to MATLAB LAB 1
B. Generate a 6x1 matrix z as given
1
2
3
4
5
6

ANS:

z = (1:6)';
disp('Matrix z:');
disp(z);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 6 of 10


Introduction to MATLAB LAB 1
C. Solve linear system of equations Ax=z

ANS:

A = [1 2; 3 4];
rep = [-1; -2];
A(:, 1) = rep;
z = [1; 2];
lam = 1e-6;
x_r = ridge(z, A, lam);
disp('Regularized Solution vector x:');
disp(x_r);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 7 of 10


Introduction to MATLAB LAB 1
D. Compute determinant of matrix A

ANS:

A = [1 2; 3 4];
disp('Original Matrix A:');
disp(A);
det_A = det(A);
disp(['Determinant of Matrix A: ' num2str(det_A)]);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 8 of 10


Introduction to MATLAB LAB 1
E. Extract a 4x4 matrix (first 4 rows and first 4 columns) from matrix A.

ANS:

A = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36];
sub = A(1:4, 1:4);
disp('Matrix Matrix A:');
disp(A);
disp('Extracted 4x4 Submatrix:');
disp(sub);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 9 of 10


Introduction to MATLAB LAB 1
F. Write commands to make a matrix of 2nd , 4th and 6th columns of the original matrix?

ANS:

A = [1 2 3 4 5 6;
7 8 9 10 11 12;
13 14 15 16 17 18;
19 20 21 22 23 24;
25 26 27 28 29 30;
31 32 33 34 35 36];
disp('Matrix A: ');
disp(A);
sel_col = A(:, [2, 4, 6]);
disp('Selected Columns Matrix:');
disp(sel_col);

SIGNALS AND SYSTEMS LAB NUCES FAST, ISLAMABAD Page 10 of 10

You might also like