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

Lecture 3

Uploaded by

Nawaz Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 3

Uploaded by

Nawaz Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Computational Methods and Modeling for

Engineering Applications
(GENG-8030)

Department of Electrical and Computer Engineering,


University of Windsor, ON, Canada,

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:

Q1- For the and the are

a) -0.86 rad, 9.2 (correct)


b) 0.86 rad, -9.2
c) -0.86 rad, -9.2
d) 0.86 rad, 9.2

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:

To evaluate sin pi in MATLAB, we type sin(pi). The MATLAB function name


must be followed by a pair of parentheses that surround the argument.
To express in text the sine of the second element of the array x, we would type
sin[x(3)]. However, in MATLAB you cannot use square brackets or braces in
this way, and you must type sin(x(3)).

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Expressing function arguments:

To evaluate sin(x(2) 2 5), you type sin(x(2)^2 + 5).


To evaluate sin(x(1) 1), you type sin(sqrt(x(1))+1).
Using a function as an argument of another function is called function
composition.
A common mistake involves expressions like , which means .
In MATLAB we write this expression as (sin(x))^2, not as sin^2(x), sin^2x,
sin(x^2), or sin(x)^2!

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Expressing function arguments:

The MATLAB trigonometric functions operate in radian mode. Thus sin(5)


computes the sine of 5 rad, not the sine of 5.

To convert between degrees and radians, use the relation qradians (π


/180)qdegrees.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Trigonometric functions:

cos(x) Cosine; cos x. acos(x) Inverse cosine

cot(x) Cotangent; cot x. acot(x) Inverse cotangent

csc(x) Cosecant; csc x. acsc(x) Inverse cosecant

sec(x) Secant; sec x. asec(x) Inverse secant

sin(x) Sine; sin x. asin(x) Inverse sine

tan(x) Tangent; tan x. atan(x) Inverse tangent

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

coth(x) Hyperbolic cotangent. acoth(x) Inverse hyperbolic cotangent

csch(x) Hyperbolic cosecant acsch(x) Inverse hyperbolic cosecant

sech(x) Hyperbolic secant asech(x) Inverse hyperbolic secant

sinh(x) Hyperbolic sine asinh(x) Inverse hyperbolic sine

tanh(x) Hyperbolic tangent atanh(x) Inverse hyperbolic tangent;

𝑒 𝑥 + 𝑒− 𝑥
cosh 𝑥=
2

𝑥 −𝑥
𝑒 −𝑒
sinh 𝑥=
2

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Question:

Q3- For the acsc is

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.

function [output variables] = name(input variables)

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.

Note also the use of the array


exponentiation operator (.^). This enables
the function to accept y as an array.

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.

function [Area, Circ] = Circle(r)

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 and V for r=3 and h=5 are

a) 643.1, 545.3
b) 231.3, 214.2
c) 350.8, 481.6 Fig. 1

d) 150.8, 141.4 (correct)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Example of function definition lines:

1. One input, one output:

function [area_square] = square(side)

2. Brackets are optional for one input, one output:

function area_square = square(side)

3. Three inputs, one output:

function [volume_box] = box(height,width,length)

4. One input, two outputs:

function [area_circle,circumf] = circle(radius)

5. No named output: function sqplot(side)

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Example:

function [dist,vel] = drop(g,vO,t);

% Computes the distance travelled and the

% velocity of a dropped object,

% as functions of g,

% the initial velocity vO, and

% the time t.

vel = g*t + vO;

dist = 0.5*g*t.^2 + vO*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 pass the function as an


argument to another function. For
example, we can plot the function
over
-1 x 6 as follows:

>> fplot(@f1,[-1 6])


Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Finding zeros of a function:

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.

The fzero function returns a value of x that is near x0.

It identifies only points where the function crosses the x-axis, not points
where the function just touches the axis.

For example, fzero(@cos,2) returns the value 1.5708.

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.

For example, if , define the following 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.

a) 1.03, 4.43 (correct)


b) 2, 6.7
c) 3, 7.5
d) 4, 9.4

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Finding the minimum of a function:

The fminbnd function finds the minimum of a function of a single variable.


One form of its syntax is

fminbnd(@function, x1, x2)

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.

For example, fminbnd(@cos,0,4) returns the value 3.1416 radians(.

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

To find the value of x that gives a


minimum of y for 0 x 5, type
1

0.95

>>x = fminbnd(@f2,0,5) 0.9

0.85
Plot
The answer is x 1. To find the 0.8

minimum value of y, type y = f2(x). 0.75

The result is y = 0.6321. 0.7

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)

where @function is the function handle of the function in question. The


vector x0 is a guess that must be supplied by the user.

Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Finding the minimum of a function:

To minimize the function f xex2 y2 ,


we first define it in an M-file, using
the vector x whose elements are
x(1)=x and x(2)= y.
function f = f4(x)
f = x(1).*exp(-x(1).^2-x(2).^2);
end
Suppose we guess that the minimum
is near x y 0. The session is
>>fminsearch(@f4,[0,0])
ans = -0.7071 0.000
Thus the minimum occurs at x
0.7071, y 0.

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 𝜃 𝑠𝑖𝑛 𝜃

a) d= 24.7 θ = 1.24 rad


b) d= 12.6 θ = 1.12 rad
c) d= 18.9 θ = 1.41 rad
d) d= 14.8 θ = 1.04 rad (correct)

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

fhandle = @(arglist) expr

where arglist is a comma-separated list of input arguments to be passed to the


function, and expr is any single, valid MATLAB expression.

To create a simple function called sq to calculate the square of a number, type

>>sq = @(x) x.^2;

To improve readability, you may enclose the expression in parentheses, as sq =


@(x) (x.^2);.

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

>>poly1 = @(x) 4*x.^2 - 50*x + 5;


800
>>fminbnd(poly1, -10, 10)
ans = 600

6.2500 400

6.25 shows the x value in which the 200

function gets minimized.


0

-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

The result for x=4, y=3 is

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

You might also like