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

Date: 23 Nov 2021 Lab Manual Chemical Process Optimization (Ch-404) Lab No: 03 Roll No: Ch-18022

This lab manual document contains code examples demonstrating the use of if/else conditional statements and switch statements in MATLAB. The first example calculates a student's grade based on their percentage marks and uses if/elseif statements. The second example uses a switch statement to print different outputs based on the value of variable i. The third example uses a while loop with if/else statements to find the minimum of a function through bisection search. The conclusion summarizes that if/else and switch statements allow handling different code execution paths depending on conditions.

Uploaded by

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

Date: 23 Nov 2021 Lab Manual Chemical Process Optimization (Ch-404) Lab No: 03 Roll No: Ch-18022

This lab manual document contains code examples demonstrating the use of if/else conditional statements and switch statements in MATLAB. The first example calculates a student's grade based on their percentage marks and uses if/elseif statements. The second example uses a switch statement to print different outputs based on the value of variable i. The third example uses a while loop with if/else statements to find the minimum of a function through bisection search. The conclusion summarizes that if/else and switch statements allow handling different code execution paths depending on conditions.

Uploaded by

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

Date: 23rd Nov 2021

LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No: 03 Roll No: CH-18022

TITLE:
In MATLAB , we will now learn function of if and else conditions, switch statements ,how to
input variable as strings.
CODES
Lab3ex1
CODE
clear all
totalmarks = input('total marks =')
marksobtained = input('marksobtained=')
%Percentage = marks obtained/total marks
percentage=(marksobtained/totalmarks)*100
if percentage>100
disp('Not Possible')
elseif percentage>80&percentage<100
disp('Grade A+')
elseif percentage==100
disp('A++')
end

COMMAND WINDOW
>> lab3ex1
total marks =600
totalmarks =
600
marksobtained=560
marksobtained =
560
percentage =
93.3333
Grade A+

1
Date: 23rd Nov 2021
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No: 03 Roll No: CH-18022
Lab3ex2

CODE
switch i
case 3;fprintf('three\n');
case 4;fprintf('four\n');
otherwise;fprintf('^-^\n');
end
%first write i=7 in command window then press run and
result will be
%obtained

COMMAND WINDOW
>> i=7

i=

>> lab3ex2
^-^
>> i=3

i=

>> lab3ex2
three
>> i=4

i=

>> lab3ex2
four

2
Date: 23rd Nov 2021
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No: 03 Roll No: CH-18022
lab3ex3

CODE
a=0; fa=-Inf;
b=3; fb=Inf;
while b-a>eps*b
x=(a+b)/2;
fx=x^3-2*x-5
if sign(fx)==sign(fa)
a=x;fa=fx
else
b=x;fb=fx
end
end

COMMAND WINDOW
>> lab3ex3

fx =

-4.6250

fa =

-4.6250

fx =

1.8906

fb =

1.8906

fx =

-2.1582

3
Date: 23rd Nov 2021
LAB MANUAL
CHEMICAL PROCESS OPTIMIZATION (CH-404)
Lab No: 03 Roll No: CH-18022
fa =

-2.1582
.
.
.
.
.
.
.
.
.
fx =

-1.1546e-14

fa =

-1.1546e-14

fx =

-5.3291e-15

fa =

-5.3291e-15

fx =

-8.8818e-16

fa =

-8.8818e-16
RESULT AND DISCUSSION
In this lab we found out that we can solve different conditions and situations on the basis of if
and else conditions and sometimes in order to make the cases more simpler we can use switch
statement

You might also like