Computational Methods and Modeling For Engineering Applications (GENG-8030)
Computational Methods and Modeling For Engineering Applications (GENG-8030)
Computational Methods and Modeling For Engineering Applications (GENG-8030)
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(1)
Winter 2022
General course information:
Professor:
o Dr. Mohammad Sedigh Toulabi,
Department of Electrical and Computer Engineering,
University of Windsor, ON, Canada,
Contact information:
o toulabi@uwindsor.ca (preferred)
o toulabi@ieee.org (alternative)
o Office: CEI 3052
Office hours:
o Tuesdays, 1:00 PM to 2:30 PM
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Course description:
Students will learn how to model, implement, analyze and predict the
performance of electrical and/or mechanical systems using MATLAB and
Simulink.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Grading scheme:
o For all activities and exams a laptop with MATLAB and Simulink
installed is needed.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Text books and references:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Course schedule:
Week Plan
1 Class starts
2 Quiz 1
3 Quiz 2
4 Project/midterm 1
5 Quiz 3
6 Quiz 4
8 Project/midterm 2
9 Quiz 5
10 Quiz 6
11 Quiz 7
12 Project/midterm 3
13 Class finishes
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Chapter 1:
MATLAB Overview
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Entering commands and expressions:
o MATLAB retains your previous keystrokes,
o Use the up-arrow key to scroll back through the commands,
o Press the key once to see the previous entry, and so on,
o Use the down-arrow key to scroll forward. Edit a line using the left- and
right-arrow keys the Backspace key, and the Delete key,
o Press the Enter key to execute the command,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Scalar operators:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Example:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Order of precedence:
Precedence Operation
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Example of precedence:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Example of precedence:
>>27^(1/3) + 32^(0.2)
ans = 5
>>27^(1/3) + 32^0.2
ans = 5
>>27^1/3 + 32^0.2
ans = 11
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Question:
Q1-What answer is produced by the following MATLAB expressions.
10^ 1/2 𝑎𝑛𝑑 10 ^ 1/2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Question:
a) 10
b) 23
c) 66 (correct)
d) 67
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Managing the work session:
clc Clears the Command window.
clear Removes all variables from memory.
clear x y Removes the variables x and y from memory.
Determines if a file or variable exists having the name
exist('abc')
‘abc’.
quit Stops MATLAB.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Managing the work session:
who Lists the variables currently in memory.
Lists the current variables and sizes and indicate if they have
whos
imaginary parts.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Special variables and constants:
ans Temporary variable containing the most recent answer.
i, j The imaginary unit 1.
Inf Infinity.
NaN Not-a-Number (e.g. 0/0).
pi The number π.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Complex number operations:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Question:
Q3-Given x=-5+9i and y=6-2i, The answers to the following expressions
are
𝑥 2 ∗ 𝑦 𝑎𝑛𝑑 𝑥^2/𝑦.
a) 4 + i , -9.3 -11.2i
b) 17- 5i , 3.9 +11.2i
c) 8 + 5i , 3.9 +16.3i
d) 7 + 5i , -3.9 -16.3i (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Numeric display format:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Numeric display format:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Array:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Polynomial roots:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Question:
Q4-Use MATLAB to determine how many elements are in the following
array and what is the 25th element
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Question:
Q5-Use MATLAB to find the roots of the polynomial
290 11𝑥 6𝑥 𝑥
a) 10, 2±5i
b) 10, -1.3±5.6i
c) 8.6 , -1.3 ± 5.6i (correct)
d) 8.6, 2±5i
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Some commonly used mathematical functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Some plotting commands:
Command Description
[x,y] = ginput(n) Enables the mouse to get n points from a plot, and returns
the x and y coordinates in the vectors x and y.
grid Puts grid lines on the plot.
gtext('text') Enables placement of text with the mouse.
plot(x,y) Generates a plot of the array y versus the array x on
rectilinear axes.
xlabel(‘text') Adds a text label to the horizontal axis (the abscissa).
ylabel(‘text') Adds a text label to the vertical axis (the ordinate).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
A plot example:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Question:
Q6-Use MATLAB to plot the functions
𝑦 2 𝑥 and 𝑧 4𝑠𝑖𝑛3𝑥 for 0 𝑥 5 on the same plot. Which of the
following plots is the answer?
(a) (b)
(d)
(c) correct
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q7-Which graph represents the plot for the following two functions
Y=4 6𝑥 1 𝑎𝑛𝑑 𝑧 5𝑒 . 2𝑥 𝑜𝑣𝑒𝑟 0 𝑥 5
(a) (b)
(d)
(c) correct
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Question:
Q8-Which graph represents the plot of the following function s=4𝑠𝑖𝑛 7𝑡
5 6𝑡 2 𝑜𝑣𝑒𝑟 0 𝑡 5
(c) (d)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Performing an operation in MATLAB:
A) In the interactive mode, in which all
commands are entered directly in the Command
window, or
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Inserting comments:
The comment symbol may be put anywhere in the line. MATLAB ignores
everything to the right of the % symbol. For example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
About naming and using script (.m) files:
1. The name of a script file must begin with a letter, and may include digits
and the underscore character.
2. Do not give a script file the same name as a variable.
3. Do not give a script file the same name as a MATLAB command or
function. You can check to see if a command, function or file name
already exists by using the exist command.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Debugging script files:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Suggested programming style:
1. Comments section
• The name of the program and any key words in the first line.
• The date created, and the creators’ names in the second line.
• The definitions of the variable names for every input and output
variable. Include definitions of variables used in the calculations and
units of measurement for all input and all output variables!
• The name of every user-defined function called by the program.
2. Input section: Includes input data and/or the input functions and
comments for documentation.
3. Calculation section
4. Output section: This section might contain functions for displaying the
output on the screen.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
Some input and output commands:
disp(A) Displays the contents, but not the name, of the array A.
disp('abc') Displays the text string enclosed within single quotes.
x = input('abc') Displays the text in quotes, waits for user input from the
keyboard, and stores the value in x.
x = input('abc', 's') Displays the text in quotes, waits for user input from the
keyboard, and stores the input as a string in x.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Example of a script file:
Problem:
The speed v of a falling object
dropped with no initial velocity is
given as a function of time t by v =
gt.
Plot v as a function of t for 0 < t <
tfinal, where tfinal is the final time
entered by the user.
For 3s
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Example of a script file:
a) 3.5e+4
b) 4.8e+2
c) 2.1e+3 (correct)
d) 8.9e+4
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Getting help from the textbook:
• Throughout each chapter margin notes identify where key terms are
introduced.
• Each chapter contains tables summarizing the MATLAB commands
introduced in that chapter.
• At the end of each chapter is a summary guide to the commands covered in
that chapter.
• Appendix A contains tables of MATLAB commands, grouped by category,
with the appropriate page references.
• There are four indices. The first lists MATLAB commands and symbols, the
second lists Simulink blocks, the third lists commands for the Symbolic Math
toolbox, and the fourth lists topics.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Getting help from the MATLAB:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
MATLAB help functions:
help func_name: Displays in the Command window a description of the
specified function func_name.
lookfor topic: Displays in the Command window a brief description for all
functions whose description includes the specified key word topic.
doc func_name: Opens the Help Browser to the reference page for the
specified function func_name, providing a description, additional remarks, and
examples.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
MATLAB help functions:
Q10-Use the Help system to learn about the built-in function “nthroot”.
(a) Use “nthroot” to calculate the cube root of 216.
(b) Use “lookfor” to find out how many “nthroot” functions are supported by
MATLAB. The answers are:
a) 6 , 2 (correct)
b) 7, 9
c) 4, 5
d) 8, 14
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Steps in engineering problem solving:
1. Understand the purpose of the problem.
2. Collect the known information. Realize that some of it might later found
to be unnecessary.
3. Determine what information you must find.
4. Simplify the problem only enough to obtain the required information.
State any assumptions you make.
5. Draw a sketch and label any necessary variables.
6. Determine which fundamental principles are applicable.
7. Think generally about your proposed solution approach and consider other
approaches before proceeding with the details.
8. Label each step in the solution process.
9. If you solve the problem with a program, hand check the results using a
simple version of the problem. Checking the dimensions and units and
printing the results of intermediate steps in the calculation sequence can
uncover mistakes.
10. Perform a “reality check” on your answer. Does it make sense? Estimate
the range of the expected result and compare it with your answer.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
Steps for developing a computer solution:
1. State the problem concisely.
2. Specify the input data to be used by the program.
3. Specify the output information to be generated by the program.
4. Work through the solution steps by hand or with a calculator; use a
simpler set of data if necessary.
5. Write and run the program.
6. Check the output of the program with your hand solution.
7. Run the program with your input data and perform a “reality check” on
the output. Does it make sense? Estimate the range of the expected result
and compare it with your answer.
8. If you will use the program as a general tool in the future, test it by
running it for a range of reasonable data values; perform a reality check on
the results.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Question:
Q11-(Problem 1.16) The ideal gas law relates the pressure P, volume V,
absolute temperature T, and amount of gas n. The law is
𝑛𝑅𝑇
𝑃
𝑉
where R is the gas constant. An engineer must design a large natural gas
storage tank to be expandable to maintain the pressure constant at 2.2 atm.
In December when the temperature is 4°F (-15°C), the volume of gas in the
tank is 18 500 𝑓𝑡 . What will the volume of the same quantity of gas be in
July when the temperature is 88°F (31°C)? (Hint: Use the fact that n, R, and
P are constant in this problem. Note also that K=C+273.2.)
a) 3.3577e+04
b) 2.1796e+04 (correct)
c) 4.1565e+01
d) 6.2377e+06
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 47
Question:
Q12-(Problem 1.29) The Fourier series is a series representation of a periodic
function in terms of sines and cosines. The Fourier series representation of the
function
1 0 𝑥 𝜋
𝑓 𝑥
1 𝜋 𝑥 0
is
4 sin𝑥 sin3𝑥 sin5𝑥 sin7𝑥
⋯
π 1 3 5 7
Plot on the same graph the function f(x) and its series representation, using the
four terms shown.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 48
Question:
Q13-(Problem1.35) Write a script file to compute the three roots of the cubic
equation 𝑥 𝑎𝑥 𝑏𝑥 𝑐 0 Use the input function to let the user
enter values for a, b, and c.
The answers for a=1, b=-8, c=-12 are
a) 1, -1, 2
b) 4, 18, 5
c) 1, 4, -7
d) 3, -2, -2 (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 49
Question:
Q14- Suppose x takes on the values x = 1, 1.2, 1.4, 1.6, … , 5. Use
MATLAB to compute the array y that results from the function y = 7
sin(4x). The number of elements are in the array y and the value of its third
element are.
a) 7, -1
b) 4, -18.2
c) 17, 4
d) 21, -4.4 (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 50
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(2)
Winter 2022
Vectors:
To create a row vector, separate the elements by commas. Use square brackets.
For example,
>>p = [3,7,9]
p=
3 7 9
You can create a column vector by using the transpose notation (') or by
separating the elements by semicolons.
>>p = [3,7,9]'
p=
3
7
9
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Vectors:
For example, to create the row vector u whose first three columns contain the
values of r = [2,4,20] and whose fourth, fifth, and sixth columns contain the
values of w = [9,-6,3], you type u = [r,w]. The result is the vector u =
[2,4,20,9,-6,3].
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Vectors:
The colon operator (:) easily generates a large vector of regularly spaced
elements. Parentheses are not needed but can be used for clarity. Do not use
square brackets.
Typing
>>𝑥 𝑚: 𝑞: 𝑛
creates a vector 𝑥 values with a spacing 𝑞. The first value is 𝑚. The last value
is n if 𝑚 𝑛 is an integer multiple of 𝑞. If not, the last value is less than 𝑛.
For example, typing x = 0:2:8 creates the vector x = [0,2,4,6,8], whereas
typing x = 0:2:7 creates the vector x = [0,2,4,6].
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Vectors:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Vectors:
The linspace command also creates a linearly spaced row vector, but instead
you specify the number of values rather than the increment.
The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper
limits and n is the number of points.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Vectors:
Its syntax is logspace(a,b,n), where n is the number of points between 10a and
10b.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Magnitude, length and absolute value of a vector:
Keep in mind the precise meaning of these terms when using MATLAB.
The absolute value of a vector x is a vector whose elements are the absolute
values of the elements of x.
For example, if x 2, 4,5 ,
• its length is 3; (computed from length x )
• its magnitude is 22 –4 2 52
6.7082 (computed by norm(x))
• its absolute value is 2,4,5 (computed
from abs x ).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Matrices:
A matrix has multiple rows and columns. For example, the matrix
2 4 10
16 3 7
M
8 4 9
3 12 15
has four rows and three columns.
Vectors are special cases of matrices having one row or one column.
If the matrix is small you can type it row by row, separating the elements in a
given row with spaces or commas and separating the rows with semicolons.
For example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Creating matrices from vectors:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Array addressing:
You need not use symbols to create a new array. For example, you can type
>> D = [[1,3,5];[7,9,11]];
The colon operator selects individual elements, rows, columns, or “subarrays”
of arrays. Here are some examples:
• D(:) represents all the row or column elements of the vector D.
• D(2:5) represents the second through fifth elements; that is D(2), D(3),
D(4), D(5).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Array addressing:
D(:,3) denotes all the elements in the third column of the matrix D.
D(:,2:3) denotes all the elements in the second through fifth columns of D.
D(2:3,1:3) denotes all the elements in the second and third rows that are also in
the first through third columns.
D(end,:) denotes the last row in D, and D(:,end) denotes the last column.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Array addressing:
You can use array indices to extract a smaller array from another array. For
example, if you first create the array B
B=[2,4,10,13;16,3,7,1;8,4,9,25;3,12,15,17]
2 4 10 13
16 3 7 1
B
8 4 9 25
3 12 15 17
then type C = B(2:3,1:3), you can produce the following array:
16 3 7
C
8 4 9
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Additional array functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Additional array functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Question:
Q1-For the matrix A given below, the values retuned by max(A) functions is
a) Max=4+3i 4 (correct)
b) Max= 1 4
c) Max=4i+3 4
d) Max= 1 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Question:
Q2- For the matrix B, use MATLAB to find B .
2 4 6
B 16 3 7
32 1 9
7 6 1
a B 4 3 32
2 16 9
2 16 32
b B 4 3 1 (correct)
6 7 9
16 2 32
c B 3 4 1
6 7 9
6 7 32
d B 4 3 1
2 16 9
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Question:
Q3-For the matrix B, find the array that results from the operation A
B; B . Use MATLAB to determine what number is in row 6, column 3 of
the result.
2 4 6
B 16 3 7
32 1 9
a) 4
b) 16
c) 2
d) 9 (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Workspace browser and variable editor:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Array addition and subtraction:
6 2 9 8 15 6
= (1)
10 3 12 14 2 17
Array subtraction is performed in a similar way.
The addition shown in Equation 1 is performed in MATLAB as follows:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Multiplication:
2 9 6 27
3 =
5 7 15 21
This multiplication is performed in MATLAB as:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Multiplication:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Element by element operations:
[3,5].^2=[3^2,5^2]
.^ Array exponentiation A.^B 2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Question:
Q4- Given the vectors x = [ 6 5 10 12 ] and y = [ 3 9 8 1]
a) 25 18 80 12
b) 44 45 17 10
c) 18 45 80 12 (correct)
d) 62 15 10 8
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Question:
Q5- Given the vectors x = [ 6 5 10 12 ] and y = [ 3 9 8 1]
z = sqrt(y)+ exp(x) :
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Question:
21 7 7 3
Q6- Given the matrices A B
28 8 9 4
The array product w = A.*B and the array product z = B.*A are
147 21
a (correct)
252 32
147 21
b
252 32
147 21
c
252 32
147 21
d
252 32
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Question:
Q7- Given the vectors x = [ 6 15 10 ] and y = [ 3 19 8 ]
The array quotient w = x./y and z = y./x. are
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q8- Given the matrices 𝐴
6 5 10 3 9 8
and B
3 9 8 4 5 6
Use MATLAB to find C = A./B and E =
A.\B.
x = [2, 4, – 5] and y = [– 7, 3, – 8]
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Element by element operations:
If x and y are column vectors, the result of x.*y is a column vector. For
example z = (x’).*(y’) gives
2( 7) 14
z 4(3) 12
5( 8) 40
Note that x’ is a column vector with size 3 1 and thus does not have the
same size as y, whose size is 1 3.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Element by element operations:
11 5 7 8
A B
9 4 6 2
then C = A.*B gives this result:
11( 7) 5(8) 77 40
C
9(6) 4(2) 54 8
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Element by element operations:
For example, in the following session the result y has the same size as the
argument x.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Element by element operations:
z = exp(y).*sin(x).*(cos(x)).^2.
You will get an error message if the size of x is not the same as the size of y. The
result z will have the same size as x and y.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Array division:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
Array exponentiation:
MATLAB enables us not only to raise arrays to powers but also to raise
scalars and arrays to array powers.
For example, if x = [3, 5, 8], then typing x.^3 produces the array [33, 53, 83] =
[27, 125, 512].
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Question:
21 7 2 7
Q9- Given the matrices A and B
28 8 12 5
Their array product is
42 49
a C (correct)
336 40
142 29
b C
14 63
234 249
c C
162 40
21 43
d C
136 48
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Question:
2 7
Q10- Given the matrices B
12 5
Use MATLAB to calculate B raised to the third power element by element. The
answer is
1232 217
a B
104 115
8 343
b B (correct)
1728 125
801 313
c B
1725 25
128 423
d B
1725 125
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
Matrix-to-matrix multiplication:
In the product of two matrices AB, the number of columns in A must equal the
number of rows in B. The row-column multiplications form column vectors,
and these column vectors form the matrix result. The product AB has the same
number of rows as A and the same number of columns as B. For example,
6 2 6 9 + ( 2)( 5) 6 8 + ( 2)(12)
9 8
10 3 = 10 9 + (3)( 5) 10 8 + (3)(12)
5 12
4 7 4 9 + (7)( 5) 4 8 + (7)(12)
64 24
= 75 116
1 116
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Question:
6
Q11- Given the vectors x 7 y 2 4 7
3
Use MATLAB to find the matrix product w = x*y and z = y*x.
6 2 21
a w 14 28 49 , z=78
12 24 42
12 24 42
b w 61 , z= 14 28 49
6 2 21
12 24 42
c w 14 28 49 , z=61 (correct)
6 12 21
6 2 21
d) W= 78 , z 14 28 49
12 24 42
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Question:
Q12- Use MATLAB to compute the dot product of the following vectors:
𝑢 6𝑖 12𝑗 4𝑘 𝑤 7𝑖 3𝑗 4𝑘
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Matrix-to-matrix multiplication:
Matrix multiplication does not have the commutative property; that is, in
general, AB BA. A simple example will demonstrate this fact:
6 2 9 8 78 20
AB
10 3 12 14 54 122
Whereas
9 8 6 2 134 6
BA
12 14 10 3 68 66
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Special matrices:
Two exceptions to the non-commutative property are the null or zero matrix,
denoted by 0 and the identity, or unity, matrix, denoted by I.
The null matrix contains all zeros and is not the same as the empty matrix [ ],
which has no elements.
1 0
I
0 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Special matrices:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
Special matrices:
Sometimes we want to initialize a matrix to have all zero elements. The zeros
command creates a matrix of all zeros.
Typing zeros(size(A)) creates a matrix of all zeros having the same dimension
as the matrix A. This type of matrix can be useful for applications in which
we do not know the required dimension ahead of time.
The syntax of the ones command is the same, except that it creates arrays
filled with ones.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Matrix left division and linear equation:
6x 12y 4z 70
7x 2y 3z 5
2x 8y 9z 64
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
Question:
Q13- Use MATLAB to solve the following set of equations.
14𝑥 2𝑦 6
3 𝑥 15𝑦 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Question:
Q14- Use MATLAB to solve the following set of equations.
6𝑥 4𝑦 8𝑧 12
15𝑥 3𝑦 7𝑧 17
4𝑥 9𝑦 5𝑧 6
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 47
Polynomial coefficients:
The function poly(r) computes the coefficients of the polynomial whose roots
are specified by the vector r. The result is a row vector that contains the
polynomial’s coefficients arranged in descending order of power.
For example,
x 7x 10 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 48
Polynomial coefficients:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 49
Question:
Q15- Use MATLAB to obtain the roots of
x 23 x 15x 6 0
Use the poly function to confirm your answer.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 50
Polynomial multiplication and division:
The function conv (a,b) computes the product of the two polynomials
described by the coefficient arrays a and b. The result is the coefficient array of
the product polynomial.
9𝑥 5𝑥 3𝑥 7 6𝑥 𝑥 2)=
54𝑥 39𝑥 41𝑥 29𝑥 𝑥 14
9𝑥 5𝑥 3𝑥 7 0.5833𝑥 8.1667
1.5𝑥 0.5833
6𝑥 𝑥 2 6𝑥 𝑥 2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 51
Question:
Q16- Use MATLAB to calculate the quotient and the remainder of
18x 5x 2x 3
3x 7x 4
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 52
Plotting polynomial:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 53
Example of plotting a polynomial:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 54
Question:
Q18- Plot the polynomial
y 18𝑥 5𝑥 2𝑥 3
over the range −7 ≤ x ≤ 1.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 55
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(3)
Winter 2022
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 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:
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:
a 2tan 2x
b tan 2x 2
c tan 2x
d tan 2x (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 10 𝑥 10 use MATLAB to plot
𝑦 𝑠𝑖𝑛ℎ 𝑥 𝑙𝑛 𝑥 𝑥 1 . 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 𝑉 𝜋 𝑟 ℎ/3. 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, 𝐴 2𝜋𝑟ℎ 2𝜋𝑟 ,
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
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:
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 2e x 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;
end
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q8- The equation 𝑒 . 𝑠𝑖𝑛 𝑥 2 0.1 has two solutions in the
interval 1 𝑥 5. 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 𝑦 1 𝑥𝑒 ,
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 𝑦 0.5 𝑒 . 𝑠𝑖𝑛 3𝑥 2 has one minimum point
in the interval 8 𝑥 7. Find the value of x at the minimum.
a) -7.23
b) -7.35
c) -7.53 (correct)
d) -7.78
Plot
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.
fminsearch(@function, x0)
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 xe x2
y2 , we first define it in an M-file,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Finding the minimum of a function:
To minimize the function f xe x2
y2 , we first define it in an M-file,
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 20𝑥 200𝑥 12 over
the range 0 𝑥 10.
a) 341
b) -488 (correct)
c) 120 0
-50
d) -14 -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 𝑦 𝑥 4. The values returned by
𝑓𝑚𝑖𝑛𝑠𝑒𝑎𝑟𝑐ℎ @𝑓𝑢𝑛1, 1 and 𝑓𝑧𝑒𝑟𝑜 @𝑓𝑢𝑛1,0 are:
a) 1.7e-15, 4
b) 6.3e-17, -4
c) 8.9e-16, -2 (correct)
5
d) 7.4e-14, 2 4
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 380 𝑓𝑡 .
𝑆 𝑑 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 10 cos 𝑒 and use it to plot the
function over the range 0 𝑥 2. Which of the following graphs
represents the plot.
(a) (b) 10
9.5
8.5
7.5
6.5
5.5
0 0.5 1 1.5 2
(C) (d)
Correct
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 𝑓 @ 𝑥 𝑥. ^3; 𝑔 @ 𝑥 5 ∗ 𝑠𝑖𝑛 𝑥 ;
Use MATLAB to calculate ℎ 𝑥, 𝑦 𝑔 𝑓 𝑥 5 ∗ 𝑦;
The value retuned by ℎ 6,1 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 6𝑒 ,
which is composed of the functions ℎ 𝑧 6𝑒 , 𝑔 𝑦 3 cos 𝑦 , and
𝑓 𝑥 𝑥 . Use the anonymous functions to plot 6𝑒 over the range
0 𝑥 4.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(4)
Winter 2022
Programming:
Algorithm: an ordered sequence of precisely defined instructions that
performs some task in a finite amount of time.
Ordered means that the instructions can be numbered, but an algorithm must
have the ability to alter the order of its instructions using a control structure.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Structured programming:
A technique for designing programs in which a hierarchy of modules is used,
each having a single entry and a single exit point, and in which control is
passed downward through the structure without unconditional branches to
higher levels of the structure.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Advantages of structured programming:
1. Structured programs are easier to write because the programmer can
study the overall problem first and then deal with the details later.
2. Modules (functions) written for one application can be used for other
applications (this is called reusable code).
7. Run the program with your input data and perform a reality check on
the output.
8. If you will use the program as a general tool in the future, test it by
running it for a range of reasonable data values; perform a reality check
on the results.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Effective documentation can be accomplished with the use of:
4. Use of flowcharts.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Documenting with charts:
Two types of charts aid in developing structured programs and in
documenting them.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Use of flowcharts:
Flowcharts are useful for developing and documenting programs that
contain conditional statements, because they can display the various paths
(called “branches”) that a program can take, depending on how the
conditional statements are executed.
Flowchart of ‘if’
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Finding bugs:
Debugging a program is the process of finding and removing the “bugs,” or
errors, in a program.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Locating runtime errors:
To locate a runtime error, try the following:
1. Always test your program with a simple version of the problem, whose
answers can be checked by hand calculations.
5. Breakpoint.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Developing large programs:
1. Writing and testing of individual modules (the unit-testing phase).
2. Writing of the top-level program that uses the modules (the build
phase). Not all modules are included in the initial testing. As the build
proceeds, more modules are included.
3. Testing of the first complete program (the alpha release phase). This
is usually done only in-house by technical people closely involved
with the program development. There might be several alpha releases
as bugs are discovered and removed.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Relational operators:
Operator Meaning
== Equal to.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Relational operators:
For example, suppose that x = [6,3,9] and y = [14,2,9]. The following
MATLAB session shows some examples.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Relational operators:
The relational operators can be used for array addressing.
z = x(x<y)
finds all the elements in x that are less than the corresponding elements in y.
The result is z = 6.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Relational operators:
The arithmetic operators , െ , *, / , and \ have precedence over the
relational operators. Thus the statement
z=5>2+7
is equivalent to
z = 5 >(2+7)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
The logical class:
When the relational operators are used, such as
x = (5 > 2)
Logical variables may have only the values 1 (true) and 0 (false).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
The logical class:
Just because an array contains only 0s and 1s, however, it is not necessarily
a logical array. For example, in the following session k and w appear the
same, but k is a logical array and w is a numeric array, and thus an error
message is issued.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Accessing arrays using logical arrays:
When a logical array is used to address another array, it extracts from that
array the elements in the locations where the logical array has 1s.
So typing A(B), where B is a logical array of the same size as A, returns the
values of A at the indices where B is 1.
Specifying array subscripts with logical arrays extracts the elements that
correspond to the true (1) elements in the logical array.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Logical operators:
Operator Name Definition
~A returns an array the same dimension as A; the new
~ NOT array has ones where A is zero and zeros where A is
nonzero
A & B returns an array the same dimension as A and B;
& AND the new array has ones where both A and B have nonzero
elements and zeros where either A or B is zero.
A | B returns an array the same dimension as A and B; the
| OR new array has ones where at least one element in A or B is
nonzero and zeros where A and B are both zero.
&& Short- Operator for scalar logical expressions. A && B returns
Circuit true if both A and B evaluate to true, and false if they do
AND not.
|| Short- Operator for scalar logical expressions. A || B returns true
Circuit if either A or B or both evaluate to true, and false if they
OR do not.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Order of precedence for operator types:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Order of precedence for operator types:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Logical functions:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Logical functions:
Logical function Definition
any(A) Returns a row vector having the same number of columns as A and
containing ones and zeros, depending on whether or not the
corresponding column of the matrix A contains any nonzero
elements.
isfinite(A) Returns an array of the same dimension as A with ones where the
elements of A are finite and zeros elsewhere.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Logical functions:
Logical function Definition
ischar(A) Returns a 1 if A is a character array and 0 otherwise.
isempty(A) Returns a 1 if A is an empty matrix and 0 otherwise.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Logical functions:
Logical function Definition
isinf(A) Returns an array of the same dimension as A, with ones
where A has 'inf' and zeros elsewhere.
isnan(A) Returns an array of the same dimension as A with ones
where A has 'NaN' and zeros elsewhere. ('NaN' stands for
“not a number,” which means an undefined result.)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Logical functions:
Logical function Definition
isnumeric(A) Returns a 1 if A is a numeric array and 0 otherwise.
isreal(A) Returns a 1 if A has no elements with imaginary parts
and 0 otherwise.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Logical functions:
Logical function Definition
logical(A) Converts the elements of the array A into logical values.
xor(A,B) Returns an array the same dimension as A and B; the
new array has ones where either A or B is nonzero, but
not both, and zeros where A and B are either both
nonzero or both zero.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
The find function:
find(A) Computes an array containing the indices of the nonzero
elements of the array A.
[u,v,w] = find(A) Computes the arrays u and v containing the row and
column indices of the nonzero elements of the array A
and computes the array w containing the values of the
nonzero elements. The array w may be omitted.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Logical operators and find function:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Logical operators and find function:
Note that the find function returns the indices, and not the values.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Question:
Q1- If x = [-10,14,2,5] and y = [-9,13,0,4], what will be the result of the
~y > x.
a) 1 0 0 0 (correct)
b) 1 1 1 1
c) 1 1 1 0
d) 0 1 1 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q2- Suppose that x = [-5,4,3,-8,6] and y = [-10,6,3,5,4]. What is the result of
the B = (x ~= y) operation?
a) 1 1 1 1 1
b) 1 1 1 0 0
c) 1 1 0 1 1 (correct)
d) 1 1 0 0 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Question:
Q3- Suppose that x = [-2,5,4,7,1] and y = [3,5,-2,1,6]. Use MATLAB to find
the indices of the elements in x that are greater than the corresponding
elements in y.
a) 1 2 5
b) 3 4 (correct)
c) 2 3
d) 1 3 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Question:
Q4-The height and speed of a projectile (such as a thrown ball) launched with a
speed of 𝑣 at an angle 𝐴 to the horizontal are given by ℎ 𝑡
𝑣 𝑡 𝑠𝑖𝑛 𝐴 0.5𝑔 𝑡
𝑣 𝑡 𝑣 2𝑣 𝑔𝑡 𝑠𝑖𝑛 𝐴 𝑔 𝑡
where 𝑔 is the acceleration due to gravity. The projectile will strike the ground
when h(t) = 0, which gives the time to hit 𝑡 2 𝑣 /𝑔 𝑠𝑖𝑛 𝐴. Suppose that
𝐴 40°, 𝑣 20 𝑚/𝑠, and 𝑔 9.81 𝑚/𝑠 .
Use the MATLAB relational and logical operators to find the first time when ℎ
6. Hint: When computing the times 𝑡 and 𝑡 , you have to keep in mind that
the first element in the array 𝑡 corresponds to 𝑡 0 (that is, 𝑡 1 = 0).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
The if statements:
The if statement's basic form is
if logical expression
statements
end
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
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) By: Dr. Mohammad Sedigh Toulabi 35
The logical expression in the else structure:
The logical expression may be an array. If so, the test returns a value of true
only if all the elements of the logical expression are true!
For example, if we fail to recognize how the test works, the following statements
do not perform the way we might expect.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
The logical expression in the else structure:
Instead, consider what happens if we test for x positive.
Corrected code
First scenario
Second scenario
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
The elseif statement:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Flowchart of the general if-elseif-else structure:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Example of an if-elseif-else structure:
For example, suppose that y = log(x) for x > 10, y =sqrt(x) for 0 <= x <= 10,
and y = exp(x) - 1 for x < 0.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Strings and conditional statements:
A string is a variable that contains characters. Strings are useful for creating
input prompts and messages and for storing and operating on data such as
names and addresses.
To create a string variable, enclose the characters in single quotes. For
example, the string variable name is created as follows:
The following string, number, is not the same as the variable number created
by typing number = 123.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Strings and conditional statements:
The following prompt program is a script file that allows the user to answer
Yes by typing either Y or y or by pressing the Enter key. Any other response
is treated as a No answer.
x = input('prompt', 's')
S represents a string
This function displays the string prompt on the screen, waits for input from
the keyboard, and returns the entered value in the string variable x.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Question:
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
𝑦 𝑥 1 𝑓𝑜𝑟 𝑥 0,
𝑦 3𝑥 1 𝑓𝑜𝑟 0 𝑥 10
𝑦 9 sin 5𝑥 50 31 𝑓𝑜𝑟 𝑥 10.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
Question:
Q6- Create a MATLAB function called fxy to evaluate the function f(x,y)
defined as follows:
𝑥𝑦 𝑖𝑓 𝑥 0 𝑎𝑛𝑑 𝑦 0
𝑥𝑦 𝑖𝑓 𝑥 0 𝑎𝑛𝑑 𝑦 0
𝑓 𝑥, 𝑦
𝑥 𝑦 𝑖𝑓 𝑥 0 𝑎𝑛𝑑 𝑦 0
𝑥 𝑦 𝑖𝑓 𝑥 0 𝑎𝑛𝑑 𝑦 0
For x=-5 , y=7 the value returned by the function f(x,y) is
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Question:
Q7- Given a number x and the quadrant 𝑞 𝑞 1, 2, 3, 4 , write a function,
func1, to compute 𝑠𝑖𝑛 𝑥 in degrees, taking into account the quadrant. The
program should display an error message if | 𝑥 | 1. The value returned by
func1( -0.6, 3) is
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
For loops:
A simple example of a for loop is
for k = 5:10:35
x = k^2
end
The loop variable k is initially assigned the value 5, and x is calculated from x
= k^2. Each successive pass through the loop increments k by 10 and
calculates x until k exceeds 35. Thus k takes on the values 5, 15, 25, and 35,
and x takes on the values 25, 225, 625, and 1225. The program then continues
to execute any statements following the end statement.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Flowchart of For loops:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 47
Loop variable expression:
Note the following rules when using for loops with the loop variable
expression k = m:s:n:
• If the step value s is not an integer, round-off errors can cause the loop to
execute a different number of passes than intended.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 48
Loop variable expression:
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) By: Dr. Mohammad Sedigh Toulabi 49
Using a logical array as a mask:
We can often avoid the use of loops and branching and thus create simpler and
faster programs by using a logical array as a mask that selects elements of
another array. Any elements not selected will remain unchanged.
The following session creates the logical array C from the numeric array A
given previously.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 50
Example of the mask technique:
We can use this mask technique to compute the square root of only those
elements of A given in the previous program that are no less than 0 and add 50
to those elements that are negative. The program is:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 51
Question:
Q8–Write a script file to compute the sum of the first 15 terms in the series,
5𝑘 2𝑘, k = 1, 2, 3, . . . , 15.
a) 5960 (correct)
b) 1815
c) 6643
d) 3983
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 52
Question:
Q9-Write a program to produce the following matrix:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 53
While loops:
The while loop is used when the looping process terminates because a
specified condition is satisfied, and thus the number of passes is not known in
advance. A simple example of a while loop is:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 54
The typical structure of a while loop:
For the while loop to function properly, the following two conditions must
occur:
1. The loop variable must have a value before the while statement is
executed.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 55
Flowchart of the while loop:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 56
Question:
Q10- Use a while loop to determine how many terms in the series 3𝑘 , 𝑘
1, 2, 3, . . . , 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) By: Dr. Mohammad Sedigh Toulabi 57
Question:
Q11-Use a while loop to find two decimal places the largest value of x before
the error in the 𝑒 1 𝑥 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) By: Dr. Mohammad Sedigh Toulabi 58
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(5)
Winter 2022
Nomenclature for a typical xy plot:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Generating a plot:
Example: The following MATLAB session plots 𝑦 0.4 1.8𝑥 𝑓𝑜𝑟 0
52, where 𝑦 represents the height of a rocket after launch, in miles, and 𝑥 is
the horizontal (downrange) distance in miles.
3.5
2.5
1.5
0.5
0
0 10 20 30 40 50 60
Distance (miles)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Obtaining a hard copy of the plot:
The plot will appear in the Figure window. You can obtain a hard copy of the
plot in several ways:
1. Use the menu system. Select Print on the File menu in the Figure
window. Answer OK when you are prompted to continue the printing
process.
2. Type print at the command line. This command sends the current plot
directly to the printer.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Closing the figure window:
When you are finished with the plot, close the figure window by selecting
Close from the File menu in the Figure window.
Note that using the Alt-Tab key combination in Windows-based systems will
return you to the Command window without closing the figure window.
If you do not close the window, it will not reappear when a new plot command
is executed. However, the figure will still be updated.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Requirements of a correct plot:
The following list describes the essential features of any plot:
1. Each axis must be labeled with the name of the quantity being plotted and
its units! If two or more quantities having different units are plotted (such
as when plotting both speed and distance versus time), indicate the units in
the axis label if there is room, or in the legend or labels for each curve.
2. Each axis should have regularly spaced tick marks at convenient
intervals—not too sparse, but not too dense—with a spacing that is easy to
interpret and interpolate. For example, use 0.1, 0.2, and so on, rather than
0.13, 0.26, and so on.
3. If you are plotting more than one curve or data set, label each on its plot or
use a legend to distinguish them.
4. If you are preparing multiple plots of a similar type or if the axes’ labels
cannot convey enough information, use a title.
5. If you are plotting measured data, plot each data point with a symbol such
as a circle, square, or cross (use the same symbol for every point in the
same data set). If there are many data points, plot them using the dot
symbol.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Requirements of a correct plot:
6. Sometimes data symbols are connected by lines to help the viewer
visualize the data, especially if there are few data points. However,
connecting the data points, especially with a solid line, might be
interpreted to imply knowledge of what occurs between the data points.
Thus you should be careful to prevent such misinterpretation.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
The grid and axis commands:
The grid command displays gridlines at the tick marks corresponding to the
tick labels. Type grid on to add gridlines; type grid off to stop plotting
gridlines. When used by itself, grid toggles this feature on or off, but you
might want to use grid on and grid off to be sure.
You can use the axis command to override the MATLAB selections for the
axis limits. The basic syntax is axis([xmin xmax ymin ymax]). This command
sets the scaling for the x- and y-axes to the minimum and maximum values
indicated. Note that, unlike an array, this command does not use commas to
separate the values.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
The fplot function:
The fplot function chooses a small enough spacing to display the function’s
full behavior.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
The fplot function:
The same function plotted with the plot command. A spacing of 0.01 misses
some oscillations.
-0.5
-1
-1.5
-2
-2.5
-3
1 1.2 1.4 1.6 1.8 2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Plotting polynomials with the polyval function:
To plot the polynomial 3 𝑥 5 2 𝑥 4 100 𝑥 3 2𝑥 2 7x 90 over the
range 6 𝑥 6 with a spacing of 0.01, you type:
5000
4000
3000
2000
1000
-1000
-2000
-3000
-6 -4 -2 0 2 4 6
x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Question:
Q1- Plot the equation 𝑦 0.4 1.8𝑥 𝑓𝑜𝑟 0 𝑥 35
𝑎𝑛𝑑 0 𝑦 3.5.
Title = Rocket height versus distance
X axis label= Distance (miles)
Y axis label=Height (miles)
2.5
1.5
0.5
0
0 5 10 15 20 25 30 35
Distance (miles)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Question:
Q2- Use the fplot command to plot and investigate the function
tan cos 𝑥 𝑠𝑖𝑛 𝑡𝑎𝑛 𝑥
𝑓𝑜𝑟 0 𝑥 2𝜋. How many values of x are needed to obtain the same plot
using the plot command?
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Question:
Q3- Plot the imaginary part versus the real part of the function 0.2 0.8𝑖
for 0 𝑛 20. Choose enough points to obtain a smooth curve. Label
each axis and put a title on the plot. Use the axis command to change the tick-
label spacing.
0.5
-0.5
-1
-1 -0.5 0 0.5 1
Real
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Saving figures:
To save a figure that can be opened in subsequent MATLAB sessions, save it
in a figure file with the .fig file name extension.
To do this, select Save from the Figure window File menu or click the Save
button (the disk icon) on the toolbar.
If this is the first time you are saving the file, the Save As dialog box appears.
Make sure that the type is MATLAB Figure (*.fig). Specify the name you want
assigned to the figure file. Click OK.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Hints for improving plots:
The following actions, while not required, can nevertheless improve the
appearance of your plots:
1. Start scales from zero whenever possible. This technique prevents a false
impression of the magnitudes of any variations shown on the plot.
3. Minimize the number of zeros in the data being plotted. For example, use
a scale in millions of dollars when appropriate, instead of a scale in dollars
with six zeros after every number.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Hints for improving plots:
4. Determine the minimum and maximum data values for each axis before
plotting the data. Then set the axis limits to cover the entire data range
plus an additional amount to allow convenient tick-mark spacing to be
selected.
For example, if the data on the x-axis ranges from 1.2 to 9.6, a good choice
for axis limits is 0 to 10. This choice allows you to use a tick spacing of 1 or
2.
5. Use a different line type for each curve when several are plotted on a
single plot and they cross each other; for example, use a solid line, a
dashed line, and combinations of lines and symbols. Beware of using
colors to distinguish plots if you are going to make black-and-white
printouts and photocopies.
6. Do not put many curves on one plot, particularly if they will be close to
each other or cross one another at several points.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Hints for improving plots:
7. Use the same scale limits and tick spacing on each plot if you need to
compare information on more than one plot.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Plotting functions:
Other useful plotting functions are title and gtext. These functions place text
on the plot. Both accept text within parentheses and single quotes, as with
the xlabel function.
The title function places the text at the top of the plot.
The gtext function places the text at the point on the plot where the cursor is
located when you click the left mouse button.
1
Sin(x)
0.5
-0.5
-1
0 1 2 3 4 5 6 7
X
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Subplots:
You can use the subplot command to obtain several smaller “subplots” in the
same figure. The syntax is subplot (m,n,p). This command divides the Figure
window into an array of rectangular panes with m rows and n columns. The
variable p tells MATLAB to place the output of the plot command following
the subplot command into the pth pane.
For example, subplot(3,2,5) creates an array of six panes, three panes deep
and two panes across, and directs the next plot to appear in the fifth pane (in
the bottom-left corner).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Application of the subplot command:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Subplots:
The following script file created a figure, which shows the plots of the
functions 𝑦 𝑒 . sin 10𝑥 5 for 0 𝑥 5 , and 𝑦 |𝑥
100| and 𝑦 𝑥 for 6 𝑥 6.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Data markers and line types:
To plot y versus x with a solid line and u versus v with a dashed line, type
plot(x,y,u,v,‘--’), where the symbols ‘--’ represent a dashed line.
To plot y versus x with asterisks (*) connected with a dotted line, you must
plot the data twice by typing plot(x,y,‘*’,x,y,‘:’).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Data markers and line types:
To plot y versus x with green asterisks (*) connected with a red dashed line,
you must plot the data twice by typing plot(x,y,‘g*’,x,y,‘r--’).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Specifiers for data markers, line types and colors:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Labeling curves and data:
The legend command automatically obtains from the plot the line type used for
each data set and displays a sample of this line type in the legend box next to
the string you selected.
4
sinh(x)
3.5 tanh(x)
2.5
1.5
0.5
0
0 0.5 1 1.5 2
X
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Application of hold command:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Application of hold command:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Question:
Q4- Pick a suitable spacing for 𝑡 0: 0.01: 8; 𝑎𝑛𝑑 𝑣 8: 0.01: 8; and then
use the subplot command to plot the following functions
0.5
-0.5
-1
0 1 2 3 4 5 6 7 8
t
12
11
10
8
-8 -6 -4 -2 0 2 4 6 8
v
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Question:
Q5- Plot the following two data sets on the same plot. For each set, x = 0, 1, 2, 3,
4, 5. Use a different data marker for each set. Connect the markers for the first set
with solid lines. Connect the markers for the second set with dashed lines. Use a
legend, and label the plot axes appropriately. The first set is y = 11, 13, 8, 7, 5, 9.
The second set is y = 2, 4, 5, 3, 2, 4.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q6- Plot 𝑦 sinh 𝑥 and 𝑦 0.5𝑒 on the same plot for 0 𝑥 2. Use a
solid line type for each, the gtext command to label the sinh x curve, and the
0.5𝑒 curve. Label the plot axes appropriately.
3.5
2.5
1.5
1 0.5exp(x)
0.5 sinh(x)
0
0 0.5 1 1.5 2
x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Why use a log scale?:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Why use a log scale?:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Logarithmic plots:
1. You cannot plot negative numbers on a log scale, because the logarithm of
a negative number is not defined as a real number.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
Logarithmic plots:
5. Equal distances on a log scale correspond to multiplication by the same
constant (as opposed to addition of the same constant on a rectilinear
scale).
The plot shown in Figure 5.3–2 covers three decades in x (from 0.1 to
100) and four decades in y and is thus called a four-by-three-cycle plot.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Logarithmic plots:
MATLAB has three commands for generating plots having log scales. The
appropriate command depends on which axis must have a log scale.
2. Use the semilogx(x,y) command to have the x scale logarithmic and the y
scale rectilinear.
3. Use the semilogy(x,y) command to have the y scale logarithmic and the x
scale rectilinear.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Polar plots:
𝑝𝑜𝑙𝑎𝑟 𝜃, 𝜌 : plot using polar coordinates , in radians, versus the radius 𝜌.
Obtain a polar plot of 𝑦 2𝑥 𝑓𝑜𝑟 0 𝑥 2𝜋.
180 0
210 330
240 300
270
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 37
Question:
Q7- Plot the following functions using axes that will produce a straight-line
plot. The power function is 𝑦 2𝑥 . , and the exponential function is 𝑦
10 . 0.01 𝑥 3, 𝑙𝑜𝑔𝑙𝑜𝑔, 𝑠𝑒𝑚𝑖𝑙𝑜𝑔𝑦
101
y=2*x. -0.5 y=10. 1-x
101
100
10-1
100 10-2
10-2 10 0 0 1 2 3
x x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 38
Question:
Q8- Plot the function 𝑦 8𝑥 𝑓𝑜𝑟 1 𝑥 1 with a tick spacing of 0.25
on the 𝑥 axis and 2 on the y axis.
y=8*x.3
8
-2
-4
-6
-8
-1 -0.75 -0.5 -0.25 0 0.25 0.5 0.75 1
x
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 39
Specialized plot commands:
Command Description
bar(x,y) Creates a bar chart of y versus x.
plotyy(x1,y1,x2,y2) Produces a plot with two y-axes, y1 on the left and
y2 on the right.
polar(theta,r,’type’) Produces a polar plot from the polar coordinates
theta and r, using the line type, data marker, and
colors specified in the string type.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 40
Specialized plot commands:
1 1
0.8
0.6
0.5
0.4
0.2
0 0
-0.2
-0.4
-0.5
-0.6
-0.8
-1 -1
-1 -0.5 0 0.5 1 -1 -0.5 0 0.5 1
1
0.5
-0.5
-1
-1 -0.5 0 0.5 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 41
Plotting implicit functions:
MATLAB provides the function fimplicit(f) to plot the implicit function
defined by the equation f(x,y) 0 over the default interval [ 5 5] for x and y.
For example, to plot the hyperbola defined by x^2 y^2 1 0 over the
default interval of [ 5 5], you type
If the limits for x are [−2 2] and the limits for y are [−4 4], you would type
4
4
3
3
2
2
1
1
0 0
-1 -1
-2
-2
-3
-3
-4
-4
-5 0 5 -2 -1 0 1 2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 42
Three dimensional line plots:
The following program uses the plot3 function to generate the spiral curve.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 43
Surface plots:
The following session shows how to generate the surface plot of the function
𝑧 𝑥𝑒 for 2 x 2 and 2 y 2, with a spacing of 0.1.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 44
Contour plots:
The following session generates the contour plot of
𝑧 𝑥𝑒 , for 2 x 2 and 2 y 2,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 45
Surface plots of implicit functions:
MATLAB provides the function fimplicit3(f) to plot the three-dimensional
implicit function defined by the equation
You can specify the interval with the syntax 𝑓𝑖𝑚𝑝𝑙𝑖𝑐𝑖𝑡3 𝑓, 𝑖𝑛𝑡𝑒𝑟𝑣𝑎𝑙 .
For example, to plot the upper half of the hyperboloid
𝑥 𝑦 𝑧 0 you specify the interval as for z as 0 5 , and for x and y,
use the default interval 5 5 , as follows.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 46
Other three-dimensional plotting functions:
Function Description
surf(x,y,z) Creates a shaded 3D mesh surface plot.
surfc(x,y,z) Same as surf but draws contours under the surface.
waterfall(x,y,z) Same as mesh but draws mesh lines in one direction only.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 47
Example:
Plots of the surface 𝑧 𝑥𝑒 created with the mesh function and
its variant forms: a) surf, b) surfc, c) waterfall, d) meshc,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 48
Question:
Q9- Obtain the plot of the following implicit function, known as the
Ampersand curve. Use the axis equal command.
𝑦 𝑥 𝑥 1 2𝑥 3 2 𝑥 𝑦 2𝑥
axis equal sets the aspect ratio so that the data units are the same in every
direction.
Ampersand curve
0.5
-0.5
-1
0 0.5 1 1.5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 49
Question:
Q10- Use plot3 and fplot3 to plot the 3-D line plot described by 𝑥
sin 𝑡 , 𝑦 cos 𝑡 , 𝑧 ln 𝑡 for 0 𝑡 30.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 50
Question:
Q11-Use mesh, fmesh, contour, and fcontour to create a surface plot and a
contour plot of the function 𝑧 𝑥 2 2𝑥𝑦 𝑦 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 51
Question:
Q12- Use the fimplicit3 function to create a surface plot of the function
𝑥 𝑦 𝑧 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 52
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(6)
Winter 2022
Model building and regression:
An important application of the plotting techniques is function discovery, the
technique for using data plots to obtain a mathematical function or
“mathematical model” that describes the process that generated the data.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Using the linear, power and exponential functions to describe data:
Each function gives a straight line when plotted using a specific set of axes:
2. The power function y ൌ bxm gives a straight line when plotted on log-log
axes.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Function discovery:
The power function y ൌ 2x െ0.5 and the exponential function y ൌ 101െx plotted
on linear, semi-log, and log-log axes..
Semi-log
Linear
Log-log
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Steps for function discovery:
1. Examine the data near the origin. The exponential function y ൌ b(10)mx or
y ൌ b(e)mx can never pass through the origin (unless of course b ൌ 0, which is
a trivial case).
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Steps for function discovery:
The linear function y ൌ mx b can pass through the origin only if b ൌ 0. The
power function y ൌ bxm can pass through the origin but only if m 0.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Steps for function discovery:
2. Plot the data using rectilinear scales. If it forms a
straight line, then it can be represented by the linear
function and you are finished. Otherwise, if you have data
at x ൌ 0, then
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Steps for function discovery:
4. In function discovery applications, we use the log-log and semi-log plots
only to identify the function type, but not to find the coefficients b and m.
The reason is that it is difficult to interpolate on log scales.
Power function y ൌ bxm
Exponential function y ൌ b(10)mx
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
The polyfit function:
Command Description
𝑝 𝑥 𝑝 𝑥 𝑝 𝑥 ⋯ 𝑝 𝑥 𝑝
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Power function:
The power function: y bxm.
In this case
log10 y m log10x log10b
which has the form
w p 1z p2
where the polynomial variables w and z are related to the original data
variables x and y by w log10 y and z log10x. Thus we can find the power
function that fits the data by typing
p = polyfit(log10(x),log10(y),1)
The first element p1 of the vector p will be m, and the second element p2 will
be log10b. We can find b from b 10p2 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Exponential function:
The exponential function: y b(10)mx.
In this case
log10 y mx log10b
which has the form
w p 1z p2
where the polynomial variables w and z are related to the original data
variables x and y by w log10 y and z x. We can find the exponential
function that fits the data by typing
p = polyfit(x, log10(y),1)
The first element p1 of the vector p will be m, and the second element p2
will be log10b. We can find b from b 10p2 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Example: Fitting a linear function:
Sonar measurements of the range of an approaching underwater vehicle are
given in the following table, where the distance is measured in nautical
miles (𝑛𝑚𝑖). Assuming the relative speed v is constant, the range as function
of time is given by 𝑟 𝑣𝑡 𝑟 where 𝑟 is the initial range at 𝑡 0.
Estimate the speed v and when the range will be zero.
Time, t (min) 0 2 4 6 8 10
Range, r (nmi) 3.8 3.5 2.7 2.1 1.2 0.7
4
r = −0.3286t + 3.9762.
3.5
2.5
1.5
1
r=0 at t = 12.1 minutes
0.5
0 2 4 6 8 10
t(min)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Example: Fitting an exponential function:
The temperature of coffee cooling in a mug at room temperature (68°F) was
measured at various times. The data follow.
Time t (sec) Temperature T (°F)
0 145
620 130
2266 103
3482 90
Develop an exponential function model of the coffee’s temperature as a
function of time, and use the model to estimate how long it took the
temperature to reach 120°F.
𝑇 68 𝑏 10
we must solve the equation
120 68
77.4 10 1.55e 4 for t.
𝑡 1112 sec
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Question:
Q1-The distance a spring stretches from its “free length” is a function of how
much tension force is applied to it. The following table gives the spring
length y that the given applied force f produced in a particular spring. The
spring's free length is 4.7 in. Find a functional relation between f and x, the
extension from the free length, 𝑓 𝑘𝑥 𝑏. The spring constant k is:
a) 0.1
b) 0.2
c) 0.3
d) 0.4 (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Question:
Q2. The population data for a certain country are as follows:
Obtain an exponential function that describes these data. How many years after
2012 will the population be double its 2012 size (20 millions)?
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
The least squares criterion:
The Least Squares Criterion: used to fit a function f (x). It minimizes the
sum of the squares of the residuals, J. J is defined as
m
J f xi yi
2
i 1
We can use this criterion to compare the quality of the curve fit for two or
more functions used to describe the same data. The function that gives the
smallest J value gives the best fit.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Illustration of least squares criterion:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Example: Regression using polynomials:
Consider the data set where 𝑥 1, 2, 3, . . . , 9 and
𝑦 5, 6, 10, 20, 28, 33, 34, 36, 42. Fit polynomials of first through fourth
degree to this data and compare the results.
40 40
30 30
20 20
10 10
0 0
0 5 10 0 5 10
40 40
30 30
20 20
10 10
0 0
0 5 10 0 5 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Assessing the quality of a curve fit:
Denote the sum of the squares of the deviation of the y values from their
mean y by S, which can be computed from
m
S yi y
2
i 1
This formula can be used to compute another measure of the quality of the
curve fit, the coefficient of determination, also known as the r-squared value.
It is defined as
m m
J
J f xi yi S yi y
2
r 1
2 2
i 1 i 1 S
The value of S indicates how much the data is spread around the mean, and
the value of J indicates how much of the data spread is unaccounted for by
the model.
Thus the ratio J/S indicates the fractional variation unaccounted for by the
model.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Assessing the quality of a curve fit:
For a perfect fit, J 0 and thus r 2 1. Thus the closer r 2 is to 1, the
better the fit. The largest r 2 can be is 1.
As a rule of thumb, a good fit accounts for at least 99 percent of the data
variation. This value corresponds to r 2 0.99.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Effect of coefficient accuracy:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Scaling the data:
The effect of computational errors in computing the coefficients can be
lessened by properly scaling the 𝑥 values. You can scale the data yourself
before using polyfit. Some common scaling methods are
1. Subtract the minimum 𝑥 value or the mean 𝑥 value from the 𝑥 data, if
the range of 𝑥 is small, or
2. Divide the 𝑥 values by the maximum value or the mean value, if the
range is large.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Example: Scaling the data:
The following data give the number of vehicles (in millions) crossing a
bridge each year for 10 years. Fit a cubic polynomial to the data and use the
fit to estimate the flow in the year 2010.
Year 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
Vehicle flow(millions) 2.1 3.4 4.5 5.3 6.2 6.6 6.8 7 7.4 7.8
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Example: Scaling the data:
Correct solution using scaling!
Acceptable r2 3
2
0 2 4 6 8 10
40
30
20
10
-10
0 1 2 3 4 5
50
40
Acceptable r2 30
20
10
-10
-20
0 1 2 3 4 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Question:
Q4- The U.S. census data from 1790 to 1990 is stored in the file census. dat,
which is supplied with MATLAB. Type load census to load this file. The
first column, cdate, contains the years, and the second column, pop, contains
the population in millions. First try to fit a cubic polynomial to the data. If
you get a warning message, scale the data by subtracting 1790 from the
years, and fit a cubic. The coefficient of determination (r-squared value) is:
Acceptable r2
Example pop in 1965
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Question:
Q5- The following data give the stopping distance 𝑑 as a function of initial
speed 𝑣, for a certain car model. Find a quadratic polynomial that fits the
data. Determine the quality of the curve fit by computing 𝑟 .
𝑣 𝑚𝑖/ℎ𝑟 20 30 40 50 60 70
𝑑 𝑓𝑡 25 50 130 185 250 330
a) 0.9921 350
c) 0.9962 250
d) 0.9998
200
150
100
50
0
20 30 40 50 60 70
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q6-The following function is linear in the parameters 𝑎 and 𝑎 .
𝑦 𝑥 𝑎 𝑎 ln 𝑥 .
Use least-squares regression with the following data to estimate the values of
𝑎 and 𝑎 .
x 1 2 3 4 5 6 7 8 9 10
y 10 14 16 18 19 20 21 22 23 23
a a 14.4353 a 2.4543
b a 7.34764 a 6.6895
c a 5.7518 a 9.9123 (correct)
d a 3.4344 a 11.472
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Multiple linear regression:
Suppose that y is a linear function of two or more variables 𝑥1, 𝑥2, . . . ,
for example, 𝑦 𝑎 𝑎 𝑥 𝑎 𝑥 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Example: Multiple linear regression:
Obtain a linear model 𝑦 𝑎 𝑎 𝑥 𝑎 𝑥 to describe the relationship.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Linear-in-parameters regression:
Sometimes we want to fit an expression that is neither a polynomial nor a
function that can be converted to linear form by a logarithmic or other
transformation.
Example: The following data gives the output voltage of a certain device as
a function of time. Obtain a function that describes this data.
/
𝑣 𝑡 𝑎 𝑎 𝑒 (first-order model)
The first-order model written for each of the n data points results in n
equations, which can be expressed as follows:
Xa = y′
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Example: Linear-in-parameters regression:
𝑦 𝑡 𝑎 𝑎 𝑒 (first-order model)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 32
Cftool:
𝑦 𝑡 𝑎 𝑎 𝑒 (first-order model)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 33
Question:
Q7- Obtain a linear model 𝑦 𝑎 𝑎 𝑥 𝑎 𝑥 for the following data to
describe the relationship. The maximum error is
y x1 x2
3.8 7.5 6
a) Max_error = 1.0847 (correct)
5.6 6 9
b) Max_error = 2.1249
6 13.5 10.5
c) Max_error = 3.8926
5 16.5 18
d) Max_error = 4.3285
5.8 19.5 21
5.6 21 25.5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 34
Question:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 35
Question:
Q8- Using cftool toolbox, find a polynomial function that fits following t and
y data.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 36
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(7)
Winter 2022
Linear algebraic equation:
Linear algebraic equations such as;
5𝑥 2𝑦 13
7𝑥 3𝑦 24
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Matrix methods for linear equations:
Consider the following set:
2𝑥 9𝑥 5
3𝑥 4𝑥 7
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Limitations of matrix methods:
• The matrix inverse method will warn us if a unique solution does not
exist, but it does not tell us whether there is no solution or an infinite
number of solutions.
• The method is limited to cases where the matrix A is square, that is,
cases where the number of equations equals the number of unknowns.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Singular problem:
A singular problem refers to a set of equations having either no unique
solution or no solution at all. For example, the set:
3𝑥 4𝑦 5
6𝑥 8𝑦 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Example:
The equations
6𝑥 – 10𝑦 2
3𝑥 – 4𝑦 5
6𝑥 8𝑦 3
is singular but has no solution. The graphs of these two equations are distinct
but parallel (see the next slide). Because they do not intersect, no solution
exists.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Intersection:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
No intersection or no solution exists:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Homogenous equations:
Consider the following set of homogeneous equations (which means that
their right sides are all zero
6𝑥 𝑎𝑦 0
2𝑥 4𝑦 0
𝑎 12 𝑦 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Matrix notation to represent multiple equations as a single matrix
equation:
For example, consider the following set:
3𝑥 4𝑥 7
2𝑥 9𝑥 5
Ax b
For the equation set Ax b,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Existence and uniqueness of solutions:
The set 𝑨𝒙 𝒃 with 𝑚 equations and 𝑛 unknowns has solutions if and only
if
Let 𝑟 𝑟𝑎𝑛𝑘 𝑨 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Homogenous case:
The homogeneous set Ax 0 is a special case in which b 0.
For this case 𝑟𝑎𝑛𝑘 𝑨 𝑟𝑎𝑛𝑘 𝑨 𝒃] always, and thus the set always has the
trivial solution x 0.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Left division method:
MATLAB provides the left-division method for solving the equation set Ax
b. The left-division method is based on Gauss elimination. To use the left-
division method to solve for x, type x = A\b. For example,
When there are more equations than unknowns, the left-division method
will give a solution with some of the unknowns set equal to zero.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Example:
Use the left-division method to solve the following set.
3𝑥 2𝑥 9𝑥 65
9𝑥 5𝑥 2𝑥 16
6𝑥 7𝑥 3𝑥 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Undetermined systems:
An underdetermined system does not contain enough information to solve
for all of the unknown variables, usually because it has fewer equations
than unknowns.
Thus an infinite number of solutions can exist, with one or more of the
unknowns dependent on the remaining unknowns.
𝑥 3𝑦 6
All we can do is to solve for one of the unknowns in terms of the other;
for example, 𝑥 6 3𝑦. An infinite number of solutions satisfy this
equation.
When there are more equations than unknowns, the left-division method
will give a solution with some of the unknowns set equal to zero. For
example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Example:
Determine the forces in the three equally spaced
supports that hold up a light fixture. The supports
are 5 ft apart. The fixture weighs 400 lb, and its
mass center is 4 ft from the right end. Obtain the
solution using the MATLAB left-division method
and the pseudoinverse method.
𝑇1 𝑇2 𝑇3 400
10𝑇1 5𝑇2 1600
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Example:
No variable is
necessary zero
One variable is
always zero
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Question:
Q1- Use the matrix inverse method to solve the following set.
3𝑥 4𝑥 6𝑥 = 15
2𝑥 8𝑥 12𝑥 = 5
𝑥 7𝑥 61𝑥 = 10
𝑥 is equal to:
a) 4.2638 (correct)
b) 1.2432
c) 0.4965
d) 0.0371
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Question:
Q2- Use the matrix inverse method to solve the following set.
3𝑥 4𝑥 2𝑥 5
6𝑥 3𝑥 6𝑥 2
6𝑥 8𝑥 4𝑥 3
𝑥 is equal to:
a) 12.134
b) 1.2432
c) 0
d) Inf (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Question:
Q3- Represent the following equations in vector-matrix form 𝐴𝑥 𝑏:
5𝑥 4𝑦 3𝑧 5
4𝑥 3𝑦 2𝑧 23
2𝑥 6𝑦 3𝑧 63
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
An ill-conditioned set of equations:
An ill-conditioned set of equations is a set of equations close to being
singular.
The ill-conditioned status depends on the accuracy with which the solution
calculations are made.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Example: An ill-conditioned set of equations:
3𝑥 4𝑥 5
6𝑥 8𝑥 2
3𝑥 4𝑥 5
6.000000000000001𝑥 8𝑥 2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Question:
Q4- Use the pseudoinverse method (pinv) to solve the following set of
equations
3𝑥 15𝑥 6𝑥 4
𝑥 2𝑥 3𝑥 10
𝑥 is equal to:
a) 0
b) -4.7879
c) 1.0427 (correct)
d) 3.1214
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(8)
Winter 2022
Numerical methods for calculus and differential equations:
Integral
Integral of f(x) interpreted as the area A under the curve of f(x) from x a
to x b
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Integral:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Numerical integration functions:
Command Description
integral(fun,a,b) Uses an adaptive Simpson’s rule to compute
the integral of the function whose handle is
fun, with ‘a’ as the lower integration limit and
‘b’ as the upper limit.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Numerical integration functions:
Although the integral function is more accurate than trapz, it is restricted to
computing the integrals of functions and cannot be used when the integrand
is specified by a set of points. For such cases, use the trapz function.
sin x dx
0
First use 10 panels with equal widths of 𝜋/10. The script file is
The answer is 1.9797, which gives a relative error of 100 ((2 1.9797)/2)
1%.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Numerical integration functions:
Script to integrate cos 𝑥2 from 0 to 2𝜋, create the function:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Polynomial integration:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Double integrals:
A = integral2(fun, a, b, c, d) computes the integral of 𝑓 𝑥, 𝑦 from x a to
b, and y c to d.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Triple integrals:
𝐴 𝑖𝑛𝑡𝑒𝑔𝑟𝑎𝑙3 𝑓𝑢𝑛, 𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓 computes the triple integral of
𝑓 𝑥, 𝑦, 𝑧 from 𝑥 𝑎 to 𝑏, 𝑦 𝑐 to 𝑑 , and 𝑧 𝑒 to 𝑓.
𝑓 𝑥, 𝑦, 𝑧 𝑑𝑥𝑑𝑦𝑑𝑧 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Question:
Q1-A tank having vertical sides and a bottom area of 100 ft2 is used to store
water. The tank is initially empty. To fill the tank, water is pumped into the
top at the rate given in the following table. Determine the water height h(t)
at t = 10 min.
Time (min) 0 1 2 3 4 5 6 7 8 9 10
Flow rate (ft3/min) 0 80 130 150 150 160 165 170 160 140 120
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Question:
Q2-An accelerometer is used in aircraft, rockets, and other vehicles to
estimate the vehicle’s velocity and displacement. The accelerometer
integrates the acceleration signal to produce an estimate of the velocity, and
it integrates the velocity estimate to produce an estimate of displacement.
Suppose the vehicle starts from rest at time t = 0, and its measured
acceleration is given in the following table.
𝑇𝑖𝑚𝑒 𝑠 0 1 2 3 4 5 6 7 8 9 10
𝐴𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑚/𝑠 0 2 4 9 16 17 24 32 41 48 51
The estimated velocity v after 10s is.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Question:
Q3-Use the integral function to compute the integral
1
𝐴 𝑑𝑥
𝑥
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Numerical differentiation:
Illustration of three methods for estimating the derivative 𝑑𝑦/𝑑𝑥.
A backward difference
B forward difference
C central difference
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Numerical differentiation:
MATLAB provides the diff function to use for computing derivative
estimates. Its syntax is d = diff(x), where x is a vector of values, and the
result is a vector d containing the differences between adjacent elements in
x.
𝑑 𝑥 2 𝑥 1 ,𝑥 3 𝑥 2 ,...,𝑥 𝑛 𝑥 𝑛 1 .
For example, if x = [5, 7, 12, -20], then diff(x) returns the vector [2, 5, -32].
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Polynomial differentiation functions:
Command Description
b = polyder(p) Returns a vector b containing the coefficients
of the derivative of the polynomial
represented by the vector p.
[num, den] = polyder(p2,p1) Returns the vectors num and den containing
the coefficients of the numerator and
denominator polynomials of the derivative
p2/p1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Polynomial differentiation functions:
𝑝1 5𝑥 2
𝑝2 10𝑥 4𝑥 3
𝑝 𝑝 50𝑥 40𝑥 7𝑥 6
𝑑 𝑝
20𝑥 4
𝑑𝑥
𝑑 𝑝 𝑝
150𝑥 80𝑥 7
𝑑𝑥
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Solving first order differential equations:
An ordinary differential equation (ODE) is an equation containing
ordinary derivatives of the dependent variable. An equation containing
partial derivatives with respect to two or more independent variables is a
partial differential equation (PDE). Solution methods for PDEs are an
advanced topic, and we will not treat them in this text.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Solving first order differential equations:
ode45 is used as follows to solve 𝑑𝑦/𝑑𝑡 𝑓 𝑡, 𝑦
where @ydot is the handle of the function file whose inputs must be 𝑡 and 𝑦,
and whose output must be a column vector representing 𝑑𝑦/𝑑𝑡; that is,
𝑓 𝑡, 𝑦 . The number of rows in this column vector must equal the order of
the equation.
• The array tspan contains the starting and ending values of the independent
variable t, and optionally any intermediate values.
• The array y0 contains the initial values of 𝑦. If the equation is first order,
then y0 is a scalar.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Response of an RC circuit:
The circuit model for zero input voltage v is
𝑑𝑦/𝑑𝑡 10𝑦 0 𝑦 0 2
Analytical solution : 𝑦 2𝑒
1.5
0.5
0
0 0.1 0.2 0.3 0.4 0.5
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Nonlinear example:
The equation for the height is
dh 0.0344 h
dt 10h h 2
Draining of a
spherical tank
10
-2
0 500 1000 1500 2000 2500
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Extension to higher order equations:
To use the ODE solvers to solve an equation higher than order 2, you must
first write the equation as a set of first-order equations.
For example, consider the equation
y 7 y 4 y sin(t )
5
x1 x2
1 4 7
x2 sin(t ) x1 x2
5 5 5
This form is sometimes called the Cauchy form or the state-variable form.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Extension to higher order equations:
x1 x2
1 4 7
x2 sin(t ) x1 x2
5 5 5
Suppose we want to solve the equation for 0
t 6 with the initial conditions x1(0) 3,
x2(0) 9.
10
4 Plot of x2
2
-2
-4
0 1 2 3 4 5 6
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(7)
Winter 2022
Linear algebraic equation:
Linear algebraic equations such as;
5𝑥 2𝑦 13
7𝑥 3𝑦 24
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Matrix methods for linear equations:
Consider the following set:
2𝑥 9𝑥 5
3𝑥 4𝑥 7
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Limitations of matrix methods:
• The matrix inverse method will warn us if a unique solution does not
exist, but it does not tell us whether there is no solution or an infinite
number of solutions.
• The method is limited to cases where the matrix A is square, that is,
cases where the number of equations equals the number of unknowns.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Singular problem:
A singular problem refers to a set of equations having either no unique
solution or no solution at all. For example, the set:
3𝑥 4𝑦 5
6𝑥 8𝑦 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Example:
The equations
6𝑥 – 10𝑦 2
3𝑥 – 4𝑦 5
6𝑥 8𝑦 3
is singular but has no solution. The graphs of these two equations are distinct
but parallel (see the next slide). Because they do not intersect, no solution
exists.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Intersection:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
No intersection or no solution exists:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Homogenous equations:
Consider the following set of homogeneous equations (which means that
their right sides are all zero
6𝑥 𝑎𝑦 0
2𝑥 4𝑦 0
𝑎 12 𝑦 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Matrix notation to represent multiple equations as a single matrix
equation:
For example, consider the following set:
3𝑥 4𝑥 7
2𝑥 9𝑥 5
Ax b
For the equation set Ax b,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Existence and uniqueness of solutions:
The set 𝑨𝒙 𝒃 with 𝑚 equations and 𝑛 unknowns has solutions if and only
if
Let 𝑟 𝑟𝑎𝑛𝑘 𝑨 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Homogenous case:
The homogeneous set Ax 0 is a special case in which b 0.
For this case 𝑟𝑎𝑛𝑘 𝑨 𝑟𝑎𝑛𝑘 𝑨 𝒃] always, and thus the set always has the
trivial solution x 0.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Left division method:
MATLAB provides the left-division method for solving the equation set Ax
b. The left-division method is based on Gauss elimination. To use the left-
division method to solve for x, type x = A\b. For example,
When there are more equations than unknowns, the left-division method
will give a solution with some of the unknowns set equal to zero.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Example:
Use the left-division method to solve the following set.
3𝑥 2𝑥 9𝑥 65
9𝑥 5𝑥 2𝑥 16
6𝑥 7𝑥 3𝑥 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Undetermined systems:
An underdetermined system does not contain enough information to solve
for all of the unknown variables, usually because it has fewer equations
than unknowns.
Thus an infinite number of solutions can exist, with one or more of the
unknowns dependent on the remaining unknowns.
𝑥 3𝑦 6
All we can do is to solve for one of the unknowns in terms of the other;
for example, 𝑥 6 3𝑦. An infinite number of solutions satisfy this
equation.
When there are more equations than unknowns, the left-division method
will give a solution with some of the unknowns set equal to zero. For
example,
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Example:
Determine the forces in the three equally spaced
supports that hold up a light fixture. The supports
are 5 ft apart. The fixture weighs 400 lb, and its
mass center is 4 ft from the right end. Obtain the
solution using the MATLAB left-division method
and the pseudoinverse method.
𝑇1 𝑇2 𝑇3 400
10𝑇1 5𝑇2 1600
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Example:
No variable is
necessary zero
One variable is
always zero
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Question:
Q1- Use the matrix inverse method to solve the following set.
3𝑥 4𝑥 6𝑥 = 15
2𝑥 8𝑥 12𝑥 = 5
𝑥 7𝑥 61𝑥 = 10
𝑥 is equal to:
a) 4.2638 (correct)
b) 1.2432
c) 0.4965
d) 0.0371
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Question:
Q2- Use the matrix inverse method to solve the following set.
3𝑥 4𝑥 2𝑥 5
6𝑥 3𝑥 6𝑥 2
6𝑥 8𝑥 4𝑥 3
𝑥 is equal to:
a) 12.134
b) 1.2432
c) 0
d) Inf (correct)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Question:
Q3- Represent the following equations in vector-matrix form 𝐴𝑥 𝑏:
5𝑥 4𝑦 3𝑧 5
4𝑥 3𝑦 2𝑧 23
2𝑥 6𝑦 3𝑧 63
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
An ill-conditioned set of equations:
An ill-conditioned set of equations is a set of equations close to being
singular.
The ill-conditioned status depends on the accuracy with which the solution
calculations are made.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Example: An ill-conditioned set of equations:
3𝑥 4𝑥 5
6𝑥 8𝑥 2
3𝑥 4𝑥 5
6.000000000000001𝑥 8𝑥 2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Question:
Q4- Use the pseudoinverse method (pinv) to solve the following set of
equations
3𝑥 15𝑥 6𝑥 4
𝑥 2𝑥 3𝑥 10
𝑥 is equal to:
a) 0
b) -4.7879
c) 1.0427 (correct)
d) 3.1214
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(8)
Winter 2022
Numerical methods for calculus and differential equations:
Integral
Integral of f(x) interpreted as the area A under the curve of f(x) from x a
to x b
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Integral:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
Numerical integration functions:
Command Description
integral(fun,a,b) Uses an adaptive Simpson’s rule to compute
the integral of the function whose handle is
fun, with ‘a’ as the lower integration limit and
‘b’ as the upper limit.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Numerical integration functions:
Although the integral function is more accurate than trapz, it is restricted to
computing the integrals of functions and cannot be used when the integrand
is specified by a set of points. For such cases, use the trapz function.
sin x dx
0
First use 10 panels with equal widths of 𝜋/10. The script file is
The answer is 1.9797, which gives a relative error of 100 ((2 1.9797)/2)
1%.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Numerical integration functions:
Script to integrate cos 𝑥2 from 0 to 2𝜋, create the function:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Polynomial integration:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Double integrals:
A = integral2(fun, a, b, c, d) computes the integral of 𝑓 𝑥, 𝑦 from x a to
b, and y c to d.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Triple integrals:
𝐴 𝑖𝑛𝑡𝑒𝑔𝑟𝑎𝑙3 𝑓𝑢𝑛, 𝑎, 𝑏, 𝑐, 𝑑, 𝑒, 𝑓 computes the triple integral of
𝑓 𝑥, 𝑦, 𝑧 from 𝑥 𝑎 to 𝑏, 𝑦 𝑐 to 𝑑 , and 𝑧 𝑒 to 𝑓.
𝑓 𝑥, 𝑦, 𝑧 𝑑𝑥𝑑𝑦𝑑𝑧 .
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Question:
Q1-A tank having vertical sides and a bottom area of 100 ft2 is used to store
water. The tank is initially empty. To fill the tank, water is pumped into the
top at the rate given in the following table. Determine the water height h(t)
at t = 10 min.
Time (min) 0 1 2 3 4 5 6 7 8 9 10
Flow rate (ft3/min) 0 80 130 150 150 160 165 170 160 140 120
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Question:
Q2-An accelerometer is used in aircraft, rockets, and other vehicles to
estimate the vehicle’s velocity and displacement. The accelerometer
integrates the acceleration signal to produce an estimate of the velocity, and
it integrates the velocity estimate to produce an estimate of displacement.
Suppose the vehicle starts from rest at time t = 0, and its measured
acceleration is given in the following table.
𝑇𝑖𝑚𝑒 𝑠 0 1 2 3 4 5 6 7 8 9 10
𝐴𝑐𝑐𝑒𝑙𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑚/𝑠 0 2 4 9 16 17 24 32 41 48 51
The estimated velocity v after 10s is.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Question:
Q3-Use the integral function to compute the integral
1
𝐴 𝑑𝑥
𝑥
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Numerical differentiation:
Illustration of three methods for estimating the derivative 𝑑𝑦/𝑑𝑥.
A backward difference
B forward difference
C central difference
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Numerical differentiation:
MATLAB provides the diff function to use for computing derivative
estimates. Its syntax is d = diff(x), where x is a vector of values, and the
result is a vector d containing the differences between adjacent elements in
x.
𝑑 𝑥 2 𝑥 1 ,𝑥 3 𝑥 2 ,...,𝑥 𝑛 𝑥 𝑛 1 .
For example, if x = [5, 7, 12, -20], then diff(x) returns the vector [2, 5, -32].
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Polynomial differentiation functions:
Command Description
b = polyder(p) Returns a vector b containing the coefficients
of the derivative of the polynomial
represented by the vector p.
[num, den] = polyder(p2,p1) Returns the vectors num and den containing
the coefficients of the numerator and
denominator polynomials of the derivative
p2/p1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Polynomial differentiation functions:
𝑝1 5𝑥 2
𝑝2 10𝑥 4𝑥 3
𝑝 𝑝 50𝑥 40𝑥 7𝑥 6
𝑑 𝑝
20𝑥 4
𝑑𝑥
𝑑 𝑝 𝑝
150𝑥 80𝑥 7
𝑑𝑥
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Solving first order differential equations:
An ordinary differential equation (ODE) is an equation containing
ordinary derivatives of the dependent variable. An equation containing
partial derivatives with respect to two or more independent variables is a
partial differential equation (PDE). Solution methods for PDEs are an
advanced topic, and we will not treat them in this text.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Solving first order differential equations:
ode45 is used as follows to solve 𝑑𝑦/𝑑𝑡 𝑓 𝑡, 𝑦
where @ydot is the handle of the function file whose inputs must be 𝑡 and 𝑦,
and whose output must be a column vector representing 𝑑𝑦/𝑑𝑡; that is,
𝑓 𝑡, 𝑦 . The number of rows in this column vector must equal the order of
the equation.
• The array tspan contains the starting and ending values of the independent
variable t, and optionally any intermediate values.
• The array y0 contains the initial values of 𝑦. If the equation is first order,
then y0 is a scalar.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Response of an RC circuit:
The circuit model for zero input voltage v is
𝑑𝑦/𝑑𝑡 10𝑦 0 𝑦 0 2
Analytical solution : 𝑦 2𝑒
1.5
0.5
0
0 0.1 0.2 0.3 0.4 0.5
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Nonlinear example:
The equation for the height is
dh 0.0344 h
dt 10h h 2
Draining of a
spherical tank
10
-2
0 500 1000 1500 2000 2500
Time(s)
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Extension to higher order equations:
To use the ODE solvers to solve an equation higher than order 2, you must
first write the equation as a set of first-order equations.
For example, consider the equation
y 7 y 4 y sin(t )
5
x1 x2
1 4 7
x2 sin(t ) x1 x2
5 5 5
This form is sometimes called the Cauchy form or the state-variable form.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Extension to higher order equations:
x1 x2
1 4 7
x2 sin(t ) x1 x2
5 5 5
Suppose we want to solve the equation for 0
t 6 with the initial conditions x1(0) 3,
x2(0) 9.
10
4 Plot of x2
2
-2
-4
0 1 2 3 4 5 6
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Pendulum example:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 23
Pendulum example:
𝑥 𝑥 𝜃
𝑔
𝑥 𝑠𝑖𝑛 𝑥
𝐿 2
1.5
0.5
-0.5
-1
-1.5
-2
0 1 2 3 4 5
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
A mass and spring:
A mass and spring with viscous surface friction. Its equation of motion is
my cy ky f (t )
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
A mass and spring:
The equation 𝑚𝑦 𝑐𝑦 𝑘𝑦 𝑓 𝑡 of motion can be put into the state
variable form. Assuming 𝑥 𝑦 and 𝑥 𝑦 we have
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
A mass and spring:
Plot of x1
For 0 𝑡 5, 𝑥 0 0 and 𝑥 0
0 The equations can be solved and the
solution plotted follows.
Plot of x2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Question:
Q4-Use MATLAB to evaluate the following double integral:
𝐴 𝑥 𝑥𝑦 𝑑𝑥𝑑𝑦
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Question:
Q5- Use MATLAB to evaluate the following triple integral:
𝐴 𝑥𝑦𝑧𝑑𝑥𝑑𝑦𝑑𝑧
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29
Question:
Q6- Use MATLAB to compute and plot the solution of the following
equation. 10𝑦 𝑦 20 7 sin 2𝑡 𝑦 0 15
𝑦 at 𝑡 60 is around:
a y~50
b y~40
c y~30
d y~20 (correct) 21
20
19
18
17
16
15
0 10 20 30 40 50 60 70
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 30
Question:
Q7- Plot the position and velocity of a mass with a spring and damping,
having the parameter values 𝑚 2, 𝑐 3, and 𝑘 7. The applied force is
𝑢 35, the initial position is 𝑦 0 2, and the initial velocity is 𝑦 0
3.
The displacement at 𝑡 5 is
a d~4m
b d~5m (correct) 6
c d~6m 5.5
d d~7m 5
4.5
3.5
2.5
1.5
0 1 2 3 4 5 6
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 31
Computational Methods and Modeling for
Engineering Applications
(GENG-8030)
By:
Dr. Mohammad Sedigh Toulabi
Lecture number:
(9)
Winter 2022
Simulink:
Simulink is a graphical programming environment for modeling,
simulating and analyzing systems.
𝑑𝑦/𝑑𝑡 10 𝑓 𝑡
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 2
Block diagram elements:
(a) The summer element.
(b) Simulation diagram for
𝑑𝑦/𝑑𝑡 𝑓 𝑡 10𝑦
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 3
The Simulink library browser:
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 4
Simulink model for a differential equation:
Use Simulink to solve the following problem for 0 𝑡 10.
𝑑𝑦/𝑑𝑡 10𝑠𝑖𝑛𝑡 𝑦 0 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 5
Simulink model for a differential equation:
𝑦 𝑡 10 1 𝑐𝑜𝑠𝑡
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 6
Block parameters window:
Note that blocks have a Block Parameters window that opens when you
double-click on the block.
This window contains several items, the number and nature of which
depend on the specific type of block.
In general, you can use the default values of these parameters, except where
we have explicitly indicated that they should be changed.
You can always click on Help within the Block Parameters window to
obtain more information.
Note that most blocks have default labels.
You can edit text associated with a block by clicking on the text and
making changes.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 7
Block parameters window:
You can save the Simulink model as an .slx file by selecting Save from the
File menu in Simulink.
You can also print the diagram by selecting Print on the File menu.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 8
Simulink model with “Mux” and “To workspace” blocks:
20
15
10
-5
-10
0 2 4 6 8 10
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 9
Question:
Q1-Create a Simulink model to plot the solution of the following equation for
0 𝑡 6. 𝑦 7𝑠𝑖𝑛 4𝑡 5 𝑐𝑜𝑠 3𝑡 𝑦 0 2 𝑦 0 3
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 10
Question:
Q2-The following equation has no analytical solution even though it is linear.
𝑥 𝑥 tan 𝑡 𝑥 0 0
The approximate solution, which is less accurate for larger values of t, is
1
𝑥 𝑡 𝑡 𝑡 3𝑡 3 3𝑒
3
Create a Simulink model to solve this problem, and compare its solution with the
approximate solution over the range 0 𝑡 1
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 11
Linear state variables model:
The following are the equations of motion of the two-mass suspension model
shown below.
𝑚 𝑥 𝑘 𝑥 𝑥 𝑐 𝑥 𝑥
𝑚 𝑥 𝑘 𝑥 𝑥 𝑐 𝑥 𝑥 𝑘 𝑦 𝑥
𝑦 𝑥
𝑦 𝑥
Assuming 𝑧 𝑥 , 𝑧 𝑥 ,
𝑧 𝑥 , 𝑧 𝑥 ,
We have Two-mass
1 suspension model
𝑧 𝑧 , 𝑧 𝑘 𝑧 𝑐 𝑧 𝑘 𝑧 𝑐 𝑧
𝑚
1
𝑧 𝑧 , 𝑧 𝑘 𝑧 𝑐 𝑧 𝑘 𝑘 𝑧 𝑐 𝑧 𝑘 𝑦
𝑚
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 12
Linear state variables model:
1
𝑧 𝑧 , 𝑧 𝑘 𝑧 𝑐 𝑧 𝑘 𝑧 𝑐 𝑧
𝑚
1
𝑧 𝑧 , 𝑧 𝑘 𝑧 𝑐 𝑧 𝑘 𝑘 𝑧 𝑐 𝑧 𝑘 𝑦
𝑚
𝒛 𝑨𝒛 𝑩𝑦 𝑡
0 1 0 0 0
𝑎 𝑎 𝑎 𝑎 0 𝑘 𝑐
𝐴 𝐵 𝑎 𝑎
0 0 0 1 0 𝑚 𝑚
𝑘 𝑐
𝑎 𝑎 𝑎 𝑎 𝑎 𝑎 𝑎
𝑚 𝑚
𝑦 𝑪𝒛 𝑫𝑢 𝑡 𝑘
𝑎 𝑎 𝑎 𝑎
𝑚
1 0 0 0 0
𝐶 𝐷
0 0 1 0 0
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 13
Question:
Q3-The input 𝑦 𝑡 is a unit step function,
and the initial conditions are zero. Use the
following values to determine the
response for a two-mass suspension
model.
𝑚 250 𝑘𝑔, 𝑚 40 𝑘𝑔,
𝑘 1.5 10 , 𝑘 1.5
10 𝑁/𝑚, and 𝑐 1917 𝑁 𝑠/𝑚.
x1
x2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 14
Question:
x1
x2
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 15
Simulink model example of a rocket-propelled sled:
The rocket thrust initially is horizontal, but the
engine accidentally pivots during firing and rotates
with an angular acceleration of 𝜃 𝑟𝑎𝑑/𝑠 .
Compute the sled’s velocity 𝑣 for 0 𝑡 10 if
𝑣 0 0. The rocket thrust is 4000 N and the sled
mass is 450 kg.
𝐹 𝑚𝑎 4000 cos 𝜃 𝑡 450 𝑣 𝜋
𝜋 𝜋 4000𝑐𝑜𝑠 𝑡 450 𝑣
𝜃 𝑡 𝜃𝑑𝑡 𝑑𝑡 𝑡 100
100 100
80 𝜋
𝑣 𝑡 cos 𝑡 𝑑𝑡
9 100
No closed-form solution is available for the integral, which is called
Fresnel’s cosine integral.
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 16
Simulink model example of a rocket-propelled sled:
Model of a Rocket-propelled Sled
𝜃 𝜃 𝑣
𝜃 𝑣 𝑡
𝜋 𝜋
t 𝑡 𝜋
50 100 cos 𝑡
100
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 17
Simulink model example of a rocket-propelled sled:
Velocity
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 18
Transfer function response:
Response
Transient Steady
state
Input
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 19
Transfer function response and PID controller:
my cy ky f (t )
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 20
Transfer function response and PID controller:
Taking the Laplace transform of the governing equation, we get
𝑚𝑠 𝑌 𝑠 𝑐𝑠𝑌 𝑠 𝑘𝑌 𝑠 𝐹 𝑠
The transfer function between the input force F(s) and the output
displacement Y(s) then becomes
𝑌 𝑠 1
𝐹 𝑠 𝑚𝑠 𝑐𝑠 𝑘
For instance if m=1 kg, c= 10 N s/m, k=20 N/m, then
𝑌 𝑠 1
𝑃 𝑠
𝐹 𝑠 𝑠 10𝑠 20
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 21
Transfer function response and PID controller:
Input
Output
Output
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 22
Transfer function response and PID controller:
Input
Output
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 24
Electric circuits response (AC input):
Current Voltage
Current Voltage
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 25
Electric circuits response (DC input):
R-Current Voltage
L-Current C-Current
R-Current L-Current
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 26
Electric circuits response (DC input):
C-Current Voltage
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 27
Electric circuits response (Non-linear input):
R-Current Voltage
C-Current
L-Current
Input
𝑉 𝑡 100sin 20𝜋𝑡 0 𝑡 3
Input
𝑉 𝑡 𝑡 100sin 20𝜋𝑡 3 𝑡
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 28
Electric circuits response (Non-linear input):
Input R-Current
C-Current
Computational methods and modeling for engineering applications (GENG-8030) By: Dr. Mohammad Sedigh Toulabi 29