Applied Mathematics & Engineering Numericals
(AMEN)
M. Sc. Anoj Winston
Lehrstuhl CVT Gladius
BCI – TU Dortmund
Prof. Dr. D. W. Agar
09/04/2020
Conditional and Loop control statements
▪ Conditional statements enable you to determine which block of
code to execute at run
• if , elseif, else - execute statements if condition is true
• switch - execute one of several groups of statements
▪ Loop control statements enables to execute a block of code
repeatedly
• for statements loop a specific number of times, and keep track of
each iteration with an incrementing index variable
• while statements loop as long as a condition remains true
▪ Both conditional and loop control statements requires
the end keyword to exit the block of code
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -2-
(AMEN)
If, elseif, else statements
▪ if statement evaluates a logical expression and executes a group of
statements when the expression is true
▪ elseif and else blocks are optional
▪ elseif statement allows to check multiple expressions and execute the
one which evaluates to true
▪ If no expressions evaluate to true, the statements in the else are
executed
o SYNTAX:
if expression
statements or commands to execute
elseif expression
statements or commands to execute
else
statements or commands to execute
end
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -3-
(AMEN)
Switch statement
▪ A switch statement executes at most one of a number of bodies
of commands depending upon an object (switch_expression)
matching one of a set of objects (case_expression)
o SYNTAX
switch switch_expression
case case_expression
statements
case case_expression
statements
otherwise
statements
end
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -4-
(AMEN)
Switch statement
▪ The switch block tests each case until one of the case
expressions is true
▪ switch_expression can be a scalar or a string
▪ case_expression can be a scalar, string or collection of scalars
or strings enclosed in braces {scalar1,scalar2}
▪ For scalars, a case is true when,
• case_expression == switch_expression
▪ The otherwise block is optional which executes the
statements only when no case is true
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -5-
(AMEN)
For loop
▪ A for loop iterates a body of commands fixed number of times,
each time assigning an identifier a different value
o SYNTAX
for index = initialvalue:stepsize:finalvalue
statements
end
▪ Stepsize Increments index by the value step on each iteration,
or decrements index when stepsize is negative from initialvalue
to finalvalue
▪ When stepsize is not explicitly mentioned, default value of 1 is
taken
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -6-
(AMEN)
While loop
▪ While loop repeats the execution of a group of statements in a
loop until the expression is true
o SYNTAX:
while expression
statements
end
▪ To programmatically exit the loop, use a break statement
▪ An expression can include relational operators (such
as < or ==) and logical operators (such as &&, ||, or ~)
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -7-
(AMEN)
Logical operators
▪ expr1 && expr2 => AND operation
• It represents a logical AND operation that
employs short-circuiting behavior. That is, expr2 is not
evaluated if expr1 is logical 0 (false)
▪ expr1 || expr2 => OR operation
• It represents a logical OR operation that employs short-
circuiting behavior. That is, expr2 is not evaluated if expr1 is
logical 1 (true)
▪ ~ => NOT operator
▪ any => determine if any array elements are nonzero
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -8-
(AMEN)
Problems
▪ Write a MATLAB functions using for loop and while loop to calculate
the factorial of a number
▪ The value of f(x) is given by,
f(x) = ex for x<0
sin(x) for [0,1]
x(x-1) for x>1
Write a function using if elseif statement to calculate f(x)
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 -9-
(AMEN)
Linear Interpolation
▪ Linear interpolation is a method of curve fitting using linear
polynomials to construct new data points within the range of a
discrete set of known data points
▪ If the two known points are given by the coordinates (xo,yo)
and (x1,y1), the linear interpolant is the straight line between these
points. For a value x in the interval x0 and x1,the value y along the
straight line is given from the equation of slopes,
𝑦−𝑦0 𝑦1 −𝑦0
=
𝑥−𝑥0 𝑥1 −𝑥0
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 10 -
(AMEN)
Problems
The sketch shows a lawn sprinkler with
two horizontal arms of radial length R,
at the termination of which are nozzles
(exit Area A2) pointing in a direction
which is at an angle θ relative to the
tangent of a circumferential line, as
shown. The sprinkler is free to rotate,
but the bearing on which it is mounted
Solution:
https://ocw.mit.edu/courses/mechanical-engineering/2-25-advanced-fluid-mechanics-fall-
exerts a torque kω in the direction
2013/control-volume-theorems-and-applications/MIT2_25F13_Shapi5.29_Solut.pdf
opposing the rotation, ω being the
angular rate of rotation. A constant
volume flow rate Q passes through the
sprinkler, the flow being incompressible
at density ρ.
- Advanced Fluid Mechanics, MIT Department of Mechanical Engineering
This problem is from “Advanced Fluid Mechanics Problems” by A.H. Shapiro and A.A. Sonin
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 11 -
(AMEN)
Problems
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 12 -
(AMEN)
Problems
✓ Write functions to evaluate numerator and denominator for the
analytical solution of Omega separately and a function to evaluate
Omega in the same .m file. Declare Rho as a global constant,
Numerator function as nested function and Denominator function as
local function inside the same .m file. Calculate the Omega value for
Q=1 m3/sec, ϴ = 30 degrees, A2 = 2 m2, R=1m and k=0.1
✓ Define numerator and denominator as separate global functions with
their own .m files (‘numerator.m’ and ‘denominator.m’) and define a
new function to calculate Omega using these new global functions
defined
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 13 -
(AMEN)
Problems
▪ Write functions to vary Theta and R from 0-90 degrees and 1-2.5 m
respectively (n discretisation points for Theta and m discretisation points for
R) and calculate the value of omega at each data points to form a n x m
matrix. First with ‘for’ loops and second only with vector operations. Use
n=11 and m=16
▪ Calculate hence, the Omega values for (30,2); (55.7,1.4); (78.34,2.28). (Use
separate functions to perform 2D linear interpolation for the Omega values
not calculated using the discretisation).
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 14 -
(AMEN)
Questions
▪ Plot the Omega results in a 2D graph varying radius in the X-axis for
selected 5 points of theta (27,36,45,54,63)
▪ Write a separate function to extract the values of a Theta
Array[15,75,8,41,34,22,87] from an array of the discretized data points
calculated and plot them and compare the values to the previous plot
obtained
▪ Vary Values of A1 and A2 in the expression for outlet pressure of the nozzles
and plot graphs. (The graph should be descriptive, everything else is
individual choice)
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 15 -
(AMEN)
References
▪ https://www.mathworks.com/help/matlab/index.html
▪ https://ocw.mit.edu/courses/mechanical-engineering/2-25-advanced-
fluid-mechanics-fall-2013/control-volume-theorems-and-
applications/MIT2_25F13_Shapi5.29_Solut.pdf
Applied Mathematics & Engineering Numericals
Anoj Winston Gladius| 09.04.2020 - 16 -
(AMEN)