Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views11 pages

Numerical Analysis Lab Report Week 4

Uploaded by

rayanjutt451
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)
21 views11 pages

Numerical Analysis Lab Report Week 4

Uploaded by

rayanjutt451
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/ 11

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, LAHORE

DEPARTMENT OF CHEMICAL ENGINEERING

Submitted by:
K.M Arslan Khan Khilji 2022-CH-3
Abu Hurraira 2022-CH-9
Muhammad Kaif 2022-CH-37
Muhammad Haseeb 2022-R/2021-CH-13
Submitted to:
Mr. Muhammad Naeem

Week 4: Conditional Statements in Matlab


MA 204L: Numerical Analysis Lab

1|Page
“Conditional Statements”
 Example 1
>> if 5 > 8
disp ('It is True')
else
disp ('It is False')
end

It is False

 Example 2
a = input ('Enter a number; a = ');
b = input ('Enter a number; b = ');
if a < b
disp ('a < b')
elseif a > b
disp ('a > b')
else
disp ('a = b')
end

Enter a number; a =
2
Enter a number; b =
5
a < b

 Example 3
yournumber = input ('Enter a number: ')
if yournumber < 0
disp ('negative')
elseif yournumber > 0
disp ('positive')
else ('zero')
end

Enter a number:
0

yournumber =

ans =

'zero'

 Example 4
nrows = 4;
ncols = 6;
A = ones (nrows, ncols);

2|Page
for c = 1:ncols
for r = 1:nrows
if r == c
A(r,c) = 2
elseif abs(r-c) == 1
A(r,c) = -1
else
A(r,c) = 0
end
end
end

A =

2 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

A =

2 1 1 1 1 1
-1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

A =

2 1 1 1 1 1
-1 1 1 1 1 1
0 1 1 1 1 1
1 1 1 1 1 1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 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 1
0 0 -1 2 -1 1

A =

2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 1

A =

2 -1 0 0 0 0
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0

 Example 5
x = input ('Enter the x coefficient: ');
y = input ('Enter the y coefficient: ');
if (x>=0&&y>=0)
fun = x + y;
elseif (x>=0&&y<0)
fun = x + y^2;
elseif (x<0&&y>=0)
fun = x^2 + y;
else (x<0&&y<0)
fun = x^2 + y^2
end

Enter the x coefficient:

6|Page
-2
Enter the y coefficient:
-4

ans =

logical

fun =

20

 Example 6
marks = input('Enter the marks (0-100): ');

if marks < 0 || marks > 100


disp('Invalid marks. Please enter a value between 0 and 100.');
else
if marks >= 90
grade = 'A';
elseif marks >= 80
grade = 'B';
elseif marks >= 70
grade = 'C';
elseif marks >= 60
grade = 'D';
elseif marks >= 50
grade = 'E';
else
grade = 'F';
end
fprintf('Marks: %f, Grade: %s\n', marks, grade)
end

Enter the marks (0-100):


75
Marks: 75.000000, Grade: C

 Example 7
Number is even or odd
value = input ('Enter the input value: ');
switch (value)
case {1,3,5,7,9}
disp ('The value is ODD')
case {2,4,6,8,10}
disp ('The value is EVEN')
otherwise
disp ('The value is out of the range')
end

Enter the input value:

7|Page
6
The value is EVEN

 Example 8
>> day = input ('Enter the Day Number: ');
switch day
case 1
disp('Monday');
case 2
disp('Tuesday');
case 3
disp('Wednesday');
case 4
disp('Thursday');
case 5
disp('Friday');
case 6
disp('Saturday');
case 7
disp('Sunday');
otherwise
disp('Invalid day');
end

Enter the Day Number:


7
Sunday

 Example 9
>> C = input('kelvin, celsius, fahrenheit')
text = input ('c','s')
switch c
case 'kelvin'
x=input('enter temprature')
C=x-273.5
F=1.8.*x+32
case 'celsius'
x=input('enter temprature')
K=x+273.5
F=1.8.*x+32
end
kelvin, celsius, Fahrenheit

 Example 10
sum = 0;
for i = 1:10
sum= sum + i;
end
disp(sum);

55

8|Page
 Example 11
A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
for i = 1:size(A, 1)
for j = 1:size(A, 2)
fprintf('Element at (%d, %d): %d\n', i, j, A(i, j));
end
end

Element at (1, 1): 1


Element at (1, 2): 2
Element at (1, 3): 3
Element at (2, 1): 4
Element at (2, 2): 5
Element at (2, 3): 6
Element at (3, 1): 7
Element at (3, 2): 8
Element at (3, 3): 9

 Example 12(a)
for i = 1:5
switch i
case 1
disp('This is case 1.');
case 2
disp('This is case 2.');
case 3
disp('This is case 3.');
case 4
disp('This is case 4.');
case 5
disp('This is case 5.');
otherwise
disp('This case does not exist.');
end
end

This is case 1.
This is case 2.
This is case 3.
This is case 4.
This is case 5.

 Example 12(b)
for month = 1:12
% month = input (‘Enter the Month Number: ’);
switch month
case {12, 1, 2}
season = 'Winter';
case {3, 4, 5}
season = 'Spring';
case {6, 7, 8}
season = 'Summer';
case {9, 10, 11}
season = 'Fall';

9|Page
otherwise
season = 'Invalid month';
end
fprintf('Month %d: %s\n', month, season);
end

Month 1: Winter
Month 2: Winter
Month 3: Spring
Month 4: Spring
Month 5: Spring
Month 6: Summer
Month 7: Summer
Month 8: Summer
Month 9: Fall
Month 10: Fall
Month 11: Fall
Month 12: Winter

 Example 13
for i = 1:10
if i == 5
disp('Reached 5, exiting the loop.');
break
end
fprintf('Current number: %d\n', i);
end

Current number: 1
Current number: 2
Current number: 3
Current number: 4
Reached 5, exiting the loop.

 Example 14
for i = 1:10
if mod(i, 2) == 0
continue;
end
fprintf('Odd number: %d\n', i);
end

Odd number: 1
Odd number: 3
Odd number: 5
Odd number: 7
Odd number: 9

 Example 15
for i = 1:10

10 | P a g e
if i == 8
disp('Reached 8, exiting the loop.');
break;
elseif mod(i, 2) == 0
continue;
end
fprintf('Current odd number: %d\n', i);
end

Current odd number: 1


Current odd number: 3
Current odd number: 5
Current odd number: 7
Reached 8, exiting the loop.

11 | P a g e

You might also like