Lecture 3
Lecture 3
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(3)
Fall 2023
Getting helps for functions:
You can use the lookfor command to find functions that are relevant to your
application.
For example, type lookfor imaginary to get a list of the functions that deal
with imaginary numbers. You will see listed:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Common mathematical functions:
Exponential
exp(x) Exponential; e x
sqrt(x) Square root; x
Logarithmic
log(x) Natural logarithm; ln x
log10(x) Common (base 10) logarithm; log x log10 x
Complex
abs(x) Absolute value.
angle(x) Angle of a complex number.
conj(x) Complex conjugate.
imag(x) Imaginary part of a complex number.
real(x) Real part of a complex number.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Examples:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Common mathematical functions:
Numeric
ceil(x) Round to nearest integer toward .
fix(x) Round to nearest integer toward zero.
floor(x) Round to nearest integer toward .
round(x) Round toward nearest integer.
sign(x) Signum function:
1 if x 0; 0 if x 0; 1 if x 0.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Operations with complex numbers:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Operations on arrays:
MATLAB will treat a variable as an array automatically. For example, to
compute the square roots of 25, 9, and 16, type:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Question:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Question:
Q2- Find the real part, and imaginary part of the
a) 3.2 , 2.3
b) 2.1 , 1.2 (correct)
c) 2.2 , 3.3
d) 3.3 , 2.2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Expressing function arguments:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Expressing function arguments:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Expressing function arguments:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Trigonometric functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Hyperbolic functions:
cosh(x) Hyperbolic cosine acosh(x) Inverse hyperbolic cosine
𝑒 𝑥 + 𝑒− 𝑥
cosh 𝑥=
2
𝑥 −𝑥
𝑒 −𝑒
sinh 𝑥=
2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Question:
a) -0.0409 - 0.1095i
b) 0.0409 + 0.1095i
c) 0.0409 - 0.1095i (correct)
d) -0.0409 + 0.1095i
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Question:
Q4- For x in the range use MATLAB to calculate - /() . From the results you
can conclude that:
a) (correct)
Very small y!
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Question:
Q5- For x in the range use MATLAB to plot . The result looks like
Very small y!
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
User defined functions:
To create a function file, open the Editor by selecting New under the HOME
tab on the Toolstrip and select Function.
The first line in a function file must begin with a function definition line that
has a list of inputs and outputs. This line distinguishes a function M-file from
a script M-file. Its syntax is shown below.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Example:
Note the use of a semicolon at the end of
the lines. This prevents the values of u and
z from being displayed.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Question:
Q6- Create a function called cone that computes the volume V of a cone
whose height is h and whose radius is r. (Do not forget to check if a file
already exists by that name!) The volume is given by . For r=4 and h=70 the
result is:
a) 3.27e+03
b) 2.14e+02
c) 1.17e+03 (correct)
d) 4.22e+02
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Multiple outputs:
A function may have more than one output. These are enclosed in square
brackets.
For example, the function circle computes the area Area and circumference
Circ of a circle, given its radius as an input argument.
Area = pi*r.^2;
Circ = 2*pi*r;
end
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Question:
Q7- Write a function to calculate the area, , and the volume, ,
of the cylinder shown in Fig. 1.
a) 643.1, 545.3
b) 231.3, 214.2
c) 350.8, 481.6 Fig. 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Example of function definition lines:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Example:
% as functions of g,
% the time t.
end
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Function handles:
You can create a function handle to
any function by using the at sign, ,
before the function name. You can
then use the handle to reference the
function. To create a handle to the
function y x 2ex 3, define the
following function file:
function y = f1(x)
y = x + 2*exp(-x) - 3;
end
You can use the fzero function to find the zero of a function of a single
variable, which is denoted by x. One form of its syntax is
fzero(@function, x0)
where @function is the function handle for the function function, and x0 is a
user-supplied guess for the zero.
It identifies only points where the function crosses the x-axis, not points
where the function just touches the axis.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Example:
To use the fzero function to find the zeros of more complicated functions, it
is more convenient to define a function file.
function y = f1(x)
y = x + 2*exp(-x) - 3;
10
end
8
6
It means, for x=2.8887 y=0.
4
-2
-2 -1 0 1 2 3 4
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q8- The equation has two solutions in the interval . The solutions are.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Finding the minimum of a function:
where @function is the function handle for the function. The fminbnd
function returns a value of x that minimizes the function in the interval x1 ≤
x ≤ x2.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Finding the minimum of a function:
When using fminbnd it is more
convenient to define a function file.
For example, if , define the
following function file:
function y = f2(x)
y = 1-x.*exp(-x);
end
0.95
0.85
Plot
The answer is x 1. To find the 0.8
0.65
0 1 2 3 4 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q9- The function has one minimum point in the interval . Find the value of
x at the minimum.
a) -7.23
b) -7.35
c) -7.53 (correct)
0
d) -7.78 -10
-20
-30
-40
-50
Plot
-60
-70
-80
-90
-8 -7.8 -7.6 -7.4 -7.2 -7
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Finding the minimum of a function:
A function can have one or more local minima and a global minimum.
If the specified range of the independent variable does not enclose the global
minimum, fminbnd will not find the global minimum.
fminbnd will find a minimum that occurs on a boundary.
To find the minimum of a function of more than one variable, use the
fminsearch function. One form of its syntax is
fminsearch(@function, x0)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Finding the minimum of a function:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Question:
Q10- Create a primary function that uses a function handle with a nested
function to compute the minimum of the function over the range .
a) 341
b) -488 (correct)
c) 120
0
d) -14 -50
-100
-150
-200
-250
-300
Plot
-350
-400
-450
0 2 4 6 8 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
Question:
Q11-Write a function to compute . The values returned by and are:
a) 1.7e-15, 4
b) 6.3e-17, -4
c) 8.9e-16, -2 (correct)
d) 7.4e-14, 2
5
0 Plot
-1
-2
-3
-4
-3 -2 -1 0 1 2 3
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Question:
Q12- Find the depth d and angle θ to minimize the perimeter length (L) of
the channel shown below to provide an area (S) of .
𝑆 𝑑 2𝑑
𝐿= − +
𝑑 tan 𝜃 𝑠𝑖𝑛 𝜃
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Anonymous functions:
Anonymous functions enable you to create a simple function without the need
to create an M-file. You can construct an anonymous function either at the
MATLAB command line or from within another function or script. The syntax
for creating an anonymous function from an expression is
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
Anonymous functions:
To execute the function, type the name of the function handle, followed by
any input arguments enclosed in parentheses. For example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Anonymous functions:
You can pass the handle of an
anonymous function to other functions.
For example, to find the minimum of
the polynomial 4x2 50x 5 over the
interval [10, 10], you type
6.2500 400
-10 -5 0 5 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Multiple input arguments:
You can create anonymous functions having more than one input. For
example, to define the function type
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Question:
Q13- Write an anonymous function to calculate
a) 403.4 (correct)
b) 340.2
c) 278.9
d) 169.4
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Question:
Q14- Create an anonymous function for and use it to plot the function over
the range Which of the following graphs represents the plot.
8 10
8
7.5
6
7
(a) 4 (b) 10
2 9.5
0 9
y
6.5
y
-2
8.5
6
-4
8
-6
5.5
-8
7.5
5 -10 7
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x x
6.5
6
10 10
9.5 8
5.5
0 0.5 1 1.5 2
9 6
8.5
(C) 4
(d)
8 2
7.5 Correct 0
y
7 -2
6.5 -4
6 -6
5.5 -8
5 -10
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
x x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Multi input arguments:
As another example, consider the function defining a plane, z Ax By. The
scalar variables A and B must be assigned values before you create the
function handle. For example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
Calling one function with another:
One anonymous function can call another to implement function
composition. Consider the function 5 sin(x3). It is composed of the functions
g(y) 5 sin(y) and f (x) x3. In the following session the function whose
handle is h calls the functions whose handles are f and g.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Question:
Q15- Assume ; ;
Use MATLAB to calculate ;
The value retuned by is
a) 9.7
b) 14.2
c) 8.48 (correct)
d) 13.8
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
Question:
Q17- Create four anonymous functions to represent the function , which is
composed of the functions , and . Use the anonymous functions to plot over
the range .
140
120
100
80
h
60
40
20
0
0 0.5 1 1.5 2 2.5 3 3.5 4
x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46