Professional Documents
Culture Documents
LABORATORY MANUAL
INTRODUCTION TO MATLAB
(LAB # 01)
Engr. Fakhar Abbas
_________________________________________________________________________________________________________________________________________________________
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:
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)]);
ANS:
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.
23 16
64 62
A= ,B=
Find AB, and A-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);
ANS:
z = (1:6)';
disp('Matrix z:');
disp(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);
ANS:
A = [1 2; 3 4];
disp('Original Matrix A:');
disp(A);
det_A = det(A);
disp(['Determinant of Matrix A: ' num2str(det_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);
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);