0% found this document useful (0 votes)
13 views34 pages

Lecture 4

The document outlines the use of conditional statements and loops in programming, specifically focusing on if/else, for loops, and while loops. It provides examples and exercises for implementing these concepts in MATLAB, emphasizing their application in engineering computations. The content is part of a lecture series for a course on computational methods and modeling at the University of Windsor.

Uploaded by

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

Lecture 4

The document outlines the use of conditional statements and loops in programming, specifically focusing on if/else, for loops, and while loops. It provides examples and exercises for implementing these concepts in MATLAB, emphasizing their application in engineering computations. The content is part of a lecture series for a course on computational methods and modeling at the University of Windsor.

Uploaded by

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

Computational Methods and Modeling for

Engineering Applications
(GENG-8030)

Department of Electrical and Computer Engineering,


University of Windsor, ON, Canada,

By:
Dr. Mohammad Hassanzadeh

Lecture number:
(4)

Winter 2023
Subjects
Conditional statements
for loop
While loop
if/else statements
• First, we learn about if/else to code conditional statements.
• The if statement's basic form is
• if logical expression
• statements
• end
• Every if statement must have an accompanying end statement. The
end statement marks the end of the statements that are to be executed
if the logical expression is true.
The else statements:
The basic structure for the use of the else statement is

if logical expression
statement group 1
else
statement group 2
end

if b~=c
disp('b~=c') %
Statement 1
else
disp('a~=c') %
Statement 2
end
Computational methods and modeling for engineering applications (GENG-8030) 4
Example 1
Example 2
The elseif statement:

First scenario Second scenario

Computational methods and modeling for engineering applications (GENG-8030) 7


If/elseif/else
• If there are more than one conditions in the statement, then we
should use if/elseif/else.
Flowchart of the general if-elseif-else structure:

Computational methods and modeling for engineering applications (GENG-8030) 9


Example 3
Example 4
Example 5
Q5- Write a script file using conditional statements to evaluate the following
function, assuming that the scalar variable x has a value. The function is

Use your file to evaluate y for x = 11,


and check the results by hand.

Computational methods and modeling for engineering applications (GENG-8030) 12


Example 6
Q6- Create a MATLAB function called fxy to evaluate the function f(x,y)
defined as follows:

For x=-5 , y=7 the value returned by the function f(x,y) is

Computational methods and modeling for engineering applications (GENG-8030) 13


Example 7
Q7- Given a number x and the quadrant , write a function, func1, to compute in
degrees, taking into account the quadrant. The program should display an error
message if . The value returned by func1( -0.6, 3) is

Computational methods and modeling for engineering applications (GENG-8030) 14


for loop
• We use loops to compute iteration type of computations.
• There are two important type of loops: for loop and while loop.
• If we know the number of iterations then we use for loop.
• If we do not know the number of iterations, and instead we know
Limitations and boundaries for the iteration then we use while loop.
Example 5
Example 6
Example 7
Learn how the code is working,
• In the previous example, there are 5 iterations. If you want to actually
see what is happening in all these iterations, you should remove the
semicolon inside the for loop:
Example 8
Example 9:
For example, the following code uses a continue statement to avoid
computing the logarithm of a negative number.

Computational methods and modeling for engineering applications (GENG-8030) 21


Example 10
Q8–Write a script file to compute the sum of the first 15 terms in the series, ,
k = 1, 2, 3, . . . , 15.

a) 5960 (correct)
b) 1815
c) 6643
d) 3983

Computational methods and modeling for engineering applications (GENG-8030) 22


Example 11
Q9-Write a program to produce the following matrix:

Then calculate , the value of is


a) 400 (correct)
b) 576
c) 600
d) 900

Computational methods and modeling for engineering applications (GENG-8030) 23


Example 12: for-loop
Example 13
While loop
• Now we learn about while loop
While loop: Example 14
Flowchart of the while loop:

Computational methods and modeling for engineering applications (GENG-8030) 28


While loop, Example 15
here we find the actual exponent in the previous example.
While loop, example 16
Example 17
Q10- Use a while loop to determine how many terms in the series , are
required for the sum of the terms to exceed 5620. What is the sum for this
number of terms?
a) 25, 9327
b) 21, 7753
c) 19, 6327 (correct)
d) 17, 4315

Computational methods and modeling for engineering applications (GENG-8030) 31


Example 18
Q11-Use a while loop to find two decimal places the largest value of x before
the error in the exceeds 15 percent.

a) 0.89
b) 2.04 (correct)
c) 1.75
d) 3.21

Computational methods and modeling for engineering applications (GENG-8030) 32


Question:
Q11-Use a while loop to find two decimal places the largest value of x before
the error in the exceeds 15 percent.

a) 0.89
b) 2.04 (correct)
c) 1.75
d) 3.21

Computational methods and modeling for engineering applications (GENG-8030) 33


More practice
• In class, we used for loop for Taylor series of e^1 and e^2 and
generally e^x. Practice on these.
• Generally try to use for loop to write Taylor series of any function, you
may use factorial(n) and diff(f,x,n). Think about it.

You might also like