Professional Documents
Culture Documents
Lab No.2
Lab No.2
Arshi Khalid
Date: 27-09-2022
Lab No. 02
Matlab Commands:
For number of elements in an array:
numel (name_of_array)
For example, if v is an array having elements [1,2,3;4,5,6;7,8,9] then numel(v) will give us answer 9.
transpose (name_of_matrix)
max (name_of_vector)
For example, if v is a vector having elements [1,2,3] then max(v) will give us answer 3.
min (name_of_vector)
For example, if v is a vector having elements [1,2,3] then min(v) will give us answer 1.
For example, if x is a matrix having elements [1,2,3,67,56,89;2,4,1] then min (min(x)) will give us
answer 1. While max (max(x)) will give us answer 89.
diag (name_of_matrix)
inv (name_of_matrix)
Matlab Code:
1. clc;
2. clearvars;
3. % solve a system x+y=1, x-y+z=0, x+y+z=2
4. A = [1 1 0; 1 -1 1; 1 1 1]
5. B = [1;0;2]
6. C = inv (A)
7. D = C*B
To set the format of the variables:
format long/short
1
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
a == b
It will compare both a and b. And if they will be equal than it will return logical 1 otherwise it’ll return
logical 0.
For complex phase angle:
angle(complex_phase_angle)
Matlab Code:
1. % for phase angle of complex
2. D = 30i;
3. angle (D)
To generate a vector:
Not sign:
~ (not sign)
Plot (x,y)
Grid on/off
2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
Title (any_title)
hold on
1. x = linspace(-pi,pi,100);
2. y = sin(x);
3. figure, plot(x,y,'c--h')
4. xlim ([-3 3]) % to limit the plot b/w -3 to 3 on x-axis
5. ylim ([-1 1]) % to limit the plot b/w -1 to 1 on y-axis
6. grid on % to on the grid on plot
7. title ('Graph') % to give the plot a title
8. xlabel ('Sin(x)') % to label the x-axis
9. ylabel ('x') % to label the y-axis
Graph:
In plot(x,y,'c--h') we have plotted a graph b/w the values of x & y. Here, c represents the color the
curve which is cyan, “--" represents the curve itself has dashed lines and “h” represents the points on the
curve which are hexagon. They can be specified in different styles & colors using the following table.
3
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
v triangle (down)
> triangle (left)
p pentagram
h hexagram
1. x = linspace(-pi,pi,100);
2. y = sin(x);
3. z= cos(x);
4. plot (x,y,'c-',x,z,'k--') % new way to plot two curves using single plot command.
5. xlim ([-3 3]) % to limit the plot b/w -3 to 3 on x-axis
6. ylim ([-1 1]) % to limit the plot b/w -1 to 1 on y-axis
7. title ('Graph') % to give the plot a title
8. xlabel ('Sin(x)') % to label the x-axis
9. ylabel ('x') % to label the y-axis
10. grid on % to on the grid on plot
Graph:
4
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
5
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid
Graph: