0% found this document useful (0 votes)
17 views

Lab No.3

This lab report discusses flow control in MATLAB using if-else statements, switch statements, and for loops. It provides examples of using these control structures to create pie charts, bar charts, and 3D pie charts from data arrays. It also demonstrates their use in evaluating discriminants and printing tables. The key MATLAB commands covered are pie(), bar(), pie3(), switch, if-else, and for loops.

Uploaded by

David James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lab No.3

This lab report discusses flow control in MATLAB using if-else statements, switch statements, and for loops. It provides examples of using these control structures to create pie charts, bar charts, and 3D pie charts from data arrays. It also demonstrates their use in evaluating discriminants and printing tables. The key MATLAB commands covered are pie(), bar(), pie3(), switch, if-else, and for loops.

Uploaded by

David James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Numerical Analysis Lab

Lab No.3

Submitted By:
Muhammad Hassan (2020-MC-274)
Submitted to:
Dr. Arshi Khalid
--------------------------------------------------------------------------------------------------------
-

Department of Mechatronics & Control Engineering


University of Engineering & Technology Lahore,
Faisalabad Campus.
Lab report By: M.Hassan (2020-MC-274) Submitted To: Dr. Arshi Khalid

Date: 06-10-2022

Flow Control:-
Computer programming languages offer features that allow you to control the flow of
command execution based on decision making structures. MATLAB has several flow control constructions:

o If-Else statement.
o Switch and case statement.
o For Loop.
Matlab Commands:
 To make pie chart:

pie (name_of_array)

 For example, if v is an array having elements [1,2,3;4,5,6;7,8,9] then pie(v) will give us a pie chart of v.

Figure 3.1: Pie Chart

 To make bar chart:

bar (name_of_array)

 For example, if v is an array having elements [1,2,3;4,5,6;7,8,9] then bar(v) will give us a bar chart of v.

Figure 3.2: Bar Chart

 To make 3D pie chart:

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

Pie3(name_of_array)

 For example, if v is an array having elements [1,2,3;4,5,6;7,8,9] then pie3(v) will give us a 3d pie chart
of v.

Figure 3.3: 3D Pie Chart

 For switch statements:


switch statement
case '1'
statement
case '2'
statement
otherwise
statement
end
 For example, if we have a variable name method having a string Bilinear as its value. Then we can write
a program to check for the value of method

Matlab Code:

1. method = 'Bilinear';
2. switch lower(method)
3. case 'bilinear'
a. disp('Method is Bilinear')
4. case 'cubic'
a. disp('Method is cubic')
5. case 'nearest'
a. disp('Method is nearest')
6. otherwise
a. disp('Unknown method.')
7. end
 It gives the answer “Method is Bilinear”.

Matlab Code: (For different types of charts using switch statements)

1. x = [1,23,45];
2. method = 'bar';
3. switch (method)
4. case 'pie'
a. disp(pie(x))
5. case 'bar'
a. disp(bar(x))
6. end

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

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

Result:

 Use of If-Else:
if condition 1
Statement 1
elseif condition 2
Statement 2
else condition 3
Statement 3
end

 For example, if we have Z = 3*x^2 + 2*x – 1 and we have to find it’s discriminant for real or
complex values then.
1. syms x;
2. Z = 3*x^2 + 2*x - 1
3. a = 3;
4. b= 2;
5. c = -1;
6. disc = b^2 - 4*a*c
7. if disc == 0
8. disp('Discrminent is real and equal')
9. elseif disc>0
10. disp('Discriminant is real')
11. else disc<0
12. disp('Discriminat is iamginary')
13. end
 disc = 16

Discriminant is real

 Use of For Loop:

For name_of_variable=starting_vlaue : step_size : End_value

 For example, table of 2 using for loop.


Matlab Code:
1. for t=0:1:20
2. disp(['2 x ' num2str(t) ' = ' num2str(2*t)])

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

 For example, print the following table using for loop.


1 2 3 4 5
2 4 6 8 10
3 3 9 12 15
4 8 12 16 20
5 10 15 20 25

Matlab Code:
1. for a=1:5
2. for b=1:5
a. c(a,b)=a*b;
3. end
4. end
5. c

You might also like