Mathematical Modeling, Numerical Methods, and Problem Solving
Mathematical Modeling, Numerical Methods, and Problem Solving
Mathematical Modeling,
Numerical Methods,
and Problem Solving
Chapter Objectives
• Learning how mathematical models can be formulated on
the basis of scientific principles to simulate the behavior of a
simple physical system.
• Understanding how numerical methods afford a means to
generalize solutions in a manner that can be implemented
on a digital computer.
• Understanding the different types of conservation laws that
lie beneath the models used in the various engineering
disciplines and appreciating the difference between steady-
state and dynamic solutions of these models.
• Learning about the different types of numerical methods we
will cover in this book.
A Simple Mathematical Model
• A mathematical model can be broadly
defined as a formulation or equation that
expresses the essential features of a
physical system or process in mathematical
terms.
• Models can be represented by a functional
relationship between dependent variables,
independent variables, parameters, and
forcing functions.
Model Function
Dependent independent forcing
f , parameters,
variable variables functions
gm gc
vt tanh d
t
cd m
• Dependent variable - velocity v
• Independent variables - time t
• Parameters - mass m, drag coefficient cd
• Forcing function - gravitational
acceleration g
Model Results
• Using a computer (or a calculator), the model can be used
to generate a graphical representation of the system. For
example, the graph below represents the velocity of a 68.1
kg jumper, assuming a drag coefficient of 0.25 kg/m
Numerical Modeling
• Some system models will be given as implicit
functions or as differential equations - these can be
solved either using analytical methods or numerical
methods.
• Example - the bungee jumper velocity equation
from before is the analytical solution to the
differential equation
dv cd 2
g v
dt m
where the change in velocity is determined by the
gravitational forces acting on the jumper versus the
drag force.
Numerical Methods
• To solve the problem using a numerical
method, note that the time rate of change of
velocity can be approximated as:
dv v vti1 vti
dt t ti1 ti
Euler's Method
• Substituting the finite difference into the
differential equation gives
dv cd 2
= − v
g
dt m
v(ti+1) v(ti) cd
ti+1 ti =g v2
m
• Solve for
cd
v(ti+1) = v(ti) + g v(ti)2 (ti+1 ti)
m
new = old + slope step
Numerical Results
• Applying Euler's method in 2 s intervals yields:
1 5 9
2 6 10
3 7 11
4 8 12
Accessing Array Entries (cont)
• Assuming some matrix C:
C =
2 4 9
3 3 16
3 0 8
10 13 17
• C(2) would report 3
• C(4) would report 10
• C(13) would report an error!
• Entries can also be access using the row and column:
• C(2,1) would report 3
• C(3,2) would report 0
• C(5,1) would report an error!
Array Creation - Built In
• There are several built-in functions to create
arrays:
– zeros(r,c) will create an r row by c column
matrix of zeros
– zeros(n) will create an n by n matrix of zeros
– ones(r,c) will create an r row by c column
matrix of ones
– ones(n) will create an n by n matrix one ones
• help elmat has, among other things, a list
of the elementary matrices
Array Creation - Colon Operator
• The colon operator : is useful in several contexts. It
can be used to create a linearly spaced array of
points using the notation
start:diffval:limit
where start is the first value in the array,
diffval is the difference between successive
values in the array, and limit is the boundary for
the last value (though not necessarily the last
value).
>>1:0.6:3
ans =
1.0000 1.6000 2.2000 2.8000
Colon Operator - Notes
• If diffval is omitted, the default value is 1:
>>3:6
ans =
3 4 5 6
• To create a decreasing series, diffval must be negative:
>> 5:-1.2:2
ans =
5.0000 3.8000 2.6000
• If start+diffval>limit for an increasing series or
start+diffval<limit for a decreasing series, an empty
matrix is returned:
>>5:2
ans =
Empty matrix: 1-by-0
• To create a column, transpose the output of the colon
operator, not the limit value; that is, (3:6)’ not 3:6’
Array Creation - linspace
• To create a row vector with a specific number of linearly
spaced points between two numbers, use the linspace
command.
• linspace(x1, x2, n) will create a linearly spaced array
of n points between x1 and x2
>>linspace(0, 1, 6)
ans =
0 0.2000 0.4000 0.6000 0.8000 1.0000
• If n is omitted, 100 points are created.
• To generate a column, transpose the output of the
linspace command.
Array Creation - logspace
• To create a row vector with a specific number of
logarithmically spaced points between two numbers, use the
logspace command.
• logspace(x1, x2, n) will create a logarithmically
spaced array of n points between 10x1 and 10x2
>>logspace(-1, 2, 4)
ans =
0.1000 1.0000 10.0000 100.0000
• If n is omitted, 100 points are created.
• To generate a column, transpose the output of the
logspace command.
Character Strings & Ellipsis
• Alphanumeric constants are enclosed by apostrophes (')
>> f = 'Miles ';
>> s = 'Davis'
• Concatenation: pasting together of strings
>> x = [f s]
x =
Miles Davis
• Ellipsis (...): Used to continue long lines
>> a = [1 2 3 4 5 ...
6 7 8]
a =
1 2 3 4 5 6 7 8
• You cannot use an ellipsis within single quotes to continue a string. But you can
piece together shorter strings with ellipsis
>> quote = ['Any fool can make a rule,' ...
' and any fool will mind it']
quote =
Any fool can make a rule, and any fool will mind it
Mathematical Operations
• Mathematical operations in MATLAB can be
performed on both scalars and arrays.
• The common operators, in order of priority,
are:
^ Exponentiation 4^2 = 8
- Negation -8 = -8
(unary operation)
* Multiplication and 2*pi = 6.2832
Division pi/4 = 0.7854
/
\ Left Division 6\2 = 0.3333
+ Addition and 3+5 = 8
Subtraction 3-5 = -2
-
Order of Operations
• The order of operations is set first by
parentheses, then by the default order given
above:
y = -4 ^ 2 gives y = -16
since the exponentiation happens first due to its
higher default priority, but
y = (-4) ^ 2 gives y = 16
since the negation operation on the 4 takes
place first
Complex Numbers
• All the operations above can be used with complex
quantities (i.e. values containing an imaginary part
entered using i or j and displayed using i)
where
– outvar: output variable name
– funcname: function’s name
– arglist: input argument list; comma-delimited list of what the
function calls values passed to it
– helpcomments: text to show with help funcname
– statements: MATLAB commands for the function
Subfunctions
• A function file can contain a single function, but it
can also contain a primary function and one or
more subfunctions
• The primary function is whatever function is listed
first in the M-file - its function name should be the
same as the file name.
• Subfunctions are listed below the primary function.
Note that they are only accessible by the main
function and subfunctions within the same M-file
and not by the command window or any other
functions or scripts.
Input
• The easiest way to get a value from the user is the
input command:
◦ n = input('promptstring')
MATLAB will display the characters in promptstring,
and whatever value is typed is stored in n. For example,
if you type pi, n will store 3.1416…
◦ n = input('promptstring', 's')
MATLAB will display the characters in promptstring,
and whatever characters are typed will be stored as a
string in n. For example, if you type pi, n will store the
letters p and i in a 2x1 char array.
Output
• The easiest way to display the value of a
matrix is to type its name, but that will not
work in function or script files. Instead, use
the disp command
disp(value)
fprintf('format', x, y,...)
The Taylor Theorem and Series
• The Taylor theorem states that any smooth
function can be approximated as a
polynomial.
• The Taylor series provides a means to
express this idea mathematically.
The Taylor Series
f x i1 f x i f x i h
' f ''
x i 2
h
f (3)
x i 3
h
f (n )
xi n
h Rn
2! 3! n!
Truncation Error
• In general, the nth order Taylor series
expansion will be exact for an nth order
polynomial.
• In other cases, the remainder term Rn is of
the order of hn+1, meaning:
The more terms are used, the smaller the error,
and
The smaller the spacing, the smaller the error for
a given number of terms.
Numerical Differentiation
• The first order Taylor series can be used to
calculate approximations to derivatives:
' 2
Given: f (x i1 ) f (x i ) f (x i )h O(h )
' f (x i1 ) f (x i )
Then: f (x i ) O(h)
h
• This is termed a “forward” difference
because
it utilizes data at i and i+1 to
estimate the derivative.
Differentiation (cont)
• There are also backward and centered difference
approximations, depending on the points used:
• Forward:
' f (x i1 ) f (x i )
f (x i ) O(h)
h
• Backward:
' f (x i ) f (x i1 )
f (x i ) O(h)
h
• Centered:
' f (x i1 ) f (x i1 )
f (x i ) O(h 2 )
2h
Total Numerical Error
• The total numerical error is the summation of the
truncation and roundoff errors.
• The truncation error generally increases as the step
size increases, while the roundoff error decreases
as the step size increases - this leads to a point of
diminishing returns for step size.
Other Errors
• Blunders - errors caused by malfunctions of
the computer or human imperfection.
• Model errors - errors resulting from
incomplete mathematical models.
• Data uncertainty - errors resulting from the
accuracy and/or precision of the data.