Lecture7

advertisement
Engineering Analysis ENG 3420 Fall 2009
Dan C. Marinescu
Office: HEC 439 B
Office hours: Tu-Th 11:00-12:00
Lecture 7

Last time:



Today:






Roundoff and truncation errors
More on Matlab
Approximations
Finding the roots of the equation f(x)=0
Structured programming
File creation and file access
Relational operators
Next Time

Open methods for finding the roots of the equation f(x) = 0
Note: No office hours on Thursday. The TA will come to the
class and answer questions about Project1.
Lecture 7
2
The Taylor Series
''
(3)
f
x
f
xi  3



'
2
i
f x i1  f x i   f x i h 
h 
h 
2!
3!
f (n ) x i  n

h  Rn
n!
3
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.
4
Numerical Differentiation



Problem approximate the derivative of the function f(x)
knowing the values of the function at discrete values of x.
The first order Taylor series can be used to calculate
approximations to derivatives:

Given:

Then:
f (x i1)  f (x i )  f ' (x i )h  O(h 2 )
f (x i1)  f (x i )
'
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.

5
Differentiation (cont’d)
There are also backward difference and centered
difference approximations, depending on the points used:

Forward:

f (x i1)  f (x i )
f (x i ) 
 O(h)
h
'
Backward:

f (x i )  f (x i1)
f (x i ) 
 O(h)
h
'
Centered:

f (x i1 )  f (x i1)
f (x i ) 
 O(h 2 )
2h
'
6
Truncation and round-off errors


Truncation error  results from ignoring all but a finite number of terms of an
infinite series.
Round-off error  the difference between an approximation of a number used in
computation and its exact (correct) value.

An example of round-off error is provided by an index devised at the Vancouver stock
exchange. At its inception in 1982, the index was given a value of 1000.000. After 22
months of recomputing the index and truncating to three decimal places at each
change in market value, the index stood at 524.881, despite the fact that its "true"
value should have been 1009.811.
 Another example is the fate of the Ariane rocket launched on June 4, 1996 (European
Space Agency 1996). In the 37th second of flight, the inertial reference system
attempted to convert a 64-bit floating-point number to a 16-bit number, but instead
triggered an overflow error which was interpreted by the guidance system as flight
data, causing the rocket to veer off course and be destroyed.

Important: the textbook uses the term truncation error incorrectly.
Truncation error is not a function of the step size. The next slide which
presents a figure in the text is incorrect.
7
The effect of the step size on the total error

Truncation and round-off errors behave differently function of the
step size h:

the truncation error increases as the step size increases,
 the round-off error decreases as the step size increases


The total numerical error  truncation + round-off error.
There is an optimum step size which minimizes the total error.
Wrong formulation
8
The effect of the step size:
''
(3)
f
x
f
xi  3



'
2
i
f x i1  f x i   f x i h 
h 
h 
2!
3!
f (n ) x i  n

h  Rn
n!
9
The effect of the step size:

The smaller the step size


the larger the number of calculations thus the larger the round-off
error;
the smaller the error made when approximation the value of the
function at the intermediate points.
10
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.
11
Finding the roots of the equation f(x)=0


Graphical method  plot of the function and observe where it crosses the
x-axis
Bracketing methods  making two initial guesses that “bracket” the root that is, are on either side of the root
 Bisection  divide the interval in half
 False position  connect the endpoints of the interval with a straight
line and determine the location of the intercept of the x-axis.
12
Graphical Methods
A simple method for obtaining the estimate of
the root of the equation f(x)=0 is to make a
plot of the function and observe where it
crosses the x-axis.
Graphing the function can also indicate where
roots may be and where some root-finding
methods may fail:


a)
b)
c)
d)
Same sign, no roots
Different sign, one root
Same sign, two roots
Different sign, three roots
13
Bracketing Methods



Bracketing methods are based on making two initial guesses that
“bracket” the root - that is, are on either side of the root.
Brackets are formed by finding two guesses xl and xu where the sign
of the function changes; that is, where f(xl ) f(xu ) < 0
The incremental search method tests the value of the function at
evenly spaced intervals and finds brackets by identifying function
sign changes between neighboring points.
14
Incremental Search Hazards


If the spacing between the points of an incremental search are too far apart,
brackets may be missed due to capturing an even number of roots within
two points.
Incremental searches cannot find brackets containing even-multiplicity roots
regardless of spacing.
15
Bisection




The bisection method is a variation
of the incremental search method
in which the interval is always
divided in half.
If a function changes sign over an
interval, the function value at the
midpoint is evaluated.
The location of the root is then
determined as lying within the
subinterval where the sign change
occurs.
The absolute error is reduced by a
factor of 2 for each iteration.
16
17
Bisection Error

The absolute error of the bisection method is solely
dependent on the absolute error at the start of the
process (the space between the two guesses) and the
number of iterations: n x 0
Ea 

2n
The required number of iterations to obtain a particular
absolute error can be calculated based on the initial

guesses:
 0 
x
n  log 2 

E
 a,d 

18
False Position



The false position method is another bracketing method.
It determines the next guess not by splitting the bracket
in half but by connecting the endpoints with a straight
line and determining the location of the intercept of the
straight line (xr).
The value of xr then replaces whichever of the two initial
guesses yields a function value with the same sign as
f(xr).
19
False Position Illustration
f (x u )(x l  x u )
xr  xu 
f (x l )  f (x u )
20
Bisection vs. False Position


Bisection does not take into account the shape of the
function; this can be good or bad depending on the
function!
Bad:
f (x)  x10 1

21
Input comand

n = input('promptstring') 

whatever value is typed is stored in n.
displays the characters in promptstring.

Example: if you type pi, n = 3.1416…


n = input('promptstring', 's')



characters typed in are stored as a string in n
display the characters in promptstring.
Example: if you type pi, n will store the letters p and i in a 2x1
char array.
22
Output


To display the value of a matrix type its name
In function or script files use the disp command
disp(value)
will show the value on the screen, and if it is a string,
will enclose it in single quotes.
23
Formatted Output

fprintf command  used for



formatted output,
output generated by combining variable values with literal text
fprintf('format', x, y, …)


format  a string specifying how to display the value of the
variables x, y, and so on; - literal text may be printed along with the
values.
the values in the variables are formatted based on format codes.
24
Format and Control Codes

Within the format string, the following format codes
define how a numerical value is displayed:
%d - integer format
%e - scientific format with lowercase e
%E - scientific format with uppercase E
%f - decidmal format
%g - the more compact of %e or %f

Control codes that can be included in the format string:
\n - start a new line
\t - tab
\\ - print the \ character

To print a ' put two a pair of ' in the format string
25
Creating and Accessing Files


MATLAB has its own file format.
Examples:



save filename var1 var2 … varn
- saves the listed variables into a file named filename.mat.
- if no variable is listed, all variables are saved.
load filename var1 var2 … varn
- loads the listed variables from a file named filename.mat.
- if no variable is listed, all variables in the file are loaded.
Note - these are not text files!
26
ASCII Files



Append the flag -ascii to the end of a save command
to create ascii (user-readable) files.
Add an extension such as .txt or .dat to the file name as
the system does not generate one.
Use the load command and the file name to load a
rectangular array from a text file. The array will have the
same name as the file.
27
Structured Programming

Decisions 


based on the result of logical and relational operations
implemented with




if,
if…else, and
if…elseif structures.
Selections 


are based on comparisons with a test expression
are implemented with switch structures.
28
Relational Operators
Example
x == 0
unit ~= ‘m’
Operator
==
~=
Relationship
Equal
Not equal
a < 0
s > t
3.9 <= a/3
<
>
<=
Less than
Greater than
Less than or equal to
r >= 0
>=
Greater than or equal to
29
Logical Operators




~x (Not): true if x is false (or zero); false otherwise
x & y (And): true if both x and y are true (or non-zero)
x | y (Or): true if either x or y are true (or non-zero)
Not is the highest priority logical operator, followed by
And and finally Or
30
Priority of Operations



Priority can be set using parentheses.
All things being equal, expressions are performed from left to right.
Priority (from low to high)
arithmetic operators  relational operators  logical operators.

Warning: do not combine two relational operators!

Example: If x=5, 3<x<4


should be false (mathematically), but it is calculated as an expression as:
3<5<4, which leads to true<4 at which point true is converted to 1, and 1<4 is true!
Use (3<x)&(x<4) to properly evaluate.
31
Decisions

Decisions  use if structures,
may also include several elseif branches
 catch-all else branch.



Deciding which branch runs is based on the result of conditions
which are either true or false.
 if tree hits a true condition  that branch (and that branch only)
runs, then the tree terminates.
 if tree gets to an else statement without running any prior
branch  that branch will run.
If the condition is a matrix, it is considered true if and only if all
entries are true (or non-zero).
32
Selections



Switch structures  allow selection among multiple cases.
A catch-all otherwise case may be present.
Which branch runs?
 Test expression matches the value attached to the case  that
case’s branch will run.
 No cases match and there is an otherwise statement  the
branch corresponding to otherwise will run.
33
Loops

Two types of loop:


A for loop ends after a specified number of repetitions
established by the number of columns given to an index variable.
A while loop ends on the basis of a logical condition.
34
for Loops

One common way to use a for…end structure is:
for index = start:step:finish
statements
end
where the index variable takes on successive values in
the vector created using the : operator.
35
Vectorization

Sometimes, it is more efficient to perform calculations on an entire
array rather than processing an array element by element. This can
be done through vectorization.
for loop
i = 0;
for t = 0:0.02:50
i = i + 1;
y(i) = cos(t);
end
Vectorization
t = 0:0.02:50;
y = cos(t);
36
while Loops



A while loop can run an indeterminate number of times.
The general syntax is
while condition
statements
end
where the condition is a logical expression. If the condition is
true, the statements will run and when that is finished, the loop
will again check on the condition.
Note - though the condition may become false as the
statements are running, the only time it matters is after all the
statements have run.
37
Early Termination

Sometimes it is useful to break out of a for or while loop
early - this can be done using a break statement,
generally in conjunction with an if structure.

Example:
x = 24
while (1)
x = x - 5
if x < 0, break, end
end
will produce x values of 24, 19, 14, 9, 4, and -1, then stop.
38
Nesting and Indentation


Structures can be placed within other structures. For
example, the statements portion of a for loop can be
comprised of an if…elseif…else structure.
For clarity of reading, the statements of a structure are
generally indented to show which lines are under the
control of which structure.
39
Anonymous & Inline Functions

Anonymous functions are simple one-line functions created without
the need for an
M-file
fhandle = @(arg1, arg2, …) expression

Inline functions are essentially the same as anonymous functions,
but with a different syntax:
fhandle = inline('expression', 'arg1', 'arg2',…)

Anonymous functions can use workspace the values of variables
upon creation, while inlines cannot.
40
Function Functions


Function functions are functions that operate on other
functions which are passed to it as input arguments.
The input argument may be the handle of an anonymous
or inline function, the name of a built-in function, or the
name of a M-file function.
Using function functions will allow for more dynamic
programming.
41
Download