Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr.

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.

 To find the transpose of a given matrix:

transpose (name_of_matrix)

 To search a maximum number from a vector:

max (name_of_vector)

 For example, if v is a vector having elements [1,2,3] then max(v) will give us answer 3.

 To search a minimum number from a vector:

min (name_of_vector)

 For example, if v is a vector having elements [1,2,3] then min(v) will give us answer 1.

 To search a maximum/minimum number from a matrix:

max (max (name_of_matrix)) / min (min (name_of_matrix))

 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.

 To find the diagonal matrix:

diag (name_of_matrix)

 To find inverse of a square matrix:

inv (name_of_matrix)

 To solve a system x+y=1, x-y+z=0, x+y+z=2:

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

 Pi is predefined in matlab having value of 3.14…:


 To compare some variables:

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)

 ceil (x) round off to +infinity and floor(x) towards -infinity:


 To write something the power of e:

Exp (2) (⁘ Here it means e^2)

 To generate a vector:

Name_of_vector = starting_number : ending_number

 For example, a = 1:9 will generate a vector [1,2,3,4,5,6,7,8,9].

 To check a condition for the entities of a vector:

name_of_vector > number_to_compare_with

for example: a = 1:9; % generate a vector for 1 to 9;


a>4 % compares the entities of a with 4 and returns 0 if condition
is not true
Answer: 0 0 0 0 1 1 1 1 1

 Not sign:

~ (not sign)

for example: a = 1:9; % generate a vector for 1 to 9;


b = [0 4 0 -3 -5 2];
a = ~b
Answer: 1 0 1 0 0 0

 To plot a graph b/w x and y:

Plot (x,y)

 To limit the plotted graph in x & y boundries:

xlim ([start end]) / ylim ([start end])

 To on/off the grid:

Grid on/off

2
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

 To give the plotted graph a title:

Title (any_title)

 To label the x/y axis in the plot:

xlabel / ylabel (any_label)

 To hold on a plotted graph while it plots other:

hold on

Example Matlab Code:

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:

Explanation of the Matlab Code:

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.

Color Marker Line Style


b blue . point - solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond

3
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

v triangle (down)
> triangle (left)
p pentagram
h hexagram

 To plot more than one graph using single plot command:

plot (x1, y1, x2, y2)

Example Matlab Code:

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:

Lab task_1 (plot multiple graphs using subplot):


 Matlab Program:
1. clc;
2. clearvars;
3. t = 0:pi/10:2*pi;
4. x=sin(t); y=cos(t); z= 2*y-3*x; v=5-z;
5. subplot(2,2,1); plot(x)
6. title(Sin)
7. subplot(2,2,2); plot(y)
8. title(Cos)
9. subplot(2,2,3); plot(z)
10. title(2*y-3*x)
11. subplot(2,2,4); plot(v)
12. title(5-z)

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:

Lab task_2 (Plot a Circle in matlab):


 Matlab Program:
1. clc;
2. clearvars;
3. angle = linspace (0,2*pi,360);
4. x = cos(angle);
5. y = sin(angle);
6. plot(x,y,'r-.h');
7. axis('equal')
Output:

You might also like