Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th...

41
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00

Transcript of Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th...

Page 1: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

Engineering Analysis ENG 3420 Fall 2009

Dan C. Marinescu

Office: HEC 439 B

Office hours: Tu-Th 11:00-12:00

Page 2: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

22Lecture 7

Lecture 7 Last time:

Roundoff and truncation errors More on Matlab

Today: 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.

Page 3: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

33

The Taylor Series

f x i1 f x i f ' x i h f '' x i 2!

h2 f (3) x i 3!

h3 f (n ) x i n!

hn Rn

Page 4: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

44

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.

Page 5: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

55

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:

This is termed a “forward” difference because it utilizes data at i and i+1 to estimate the derivative.

f (x i1) f (x i) f'(x i)h O(h2)

f '(x i)f (x i1) f (x i)

hO(h)

Page 6: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

66

Differentiation (cont’d) There are also backward difference and centered

difference approximations, depending on the points used:

Forward:

Backward:

Centered:

f '(x i)f (x i1) f (x i)

hO(h)

f '(x i)f (x i) f (x i 1)

hO(h)

f '(x i)f (x i1) f (x i 1)

2hO(h2)

Page 7: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

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

Page 8: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

88

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

Page 9: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

The effect of the step size:

f x i1 f x i f ' x i h f '' x i 2!

h2 f (3) x i 3!

h3 f (n ) x i n!

hn Rn

9

Page 10: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

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

Page 11: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

1111

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.

Page 12: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

12

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.

Page 13: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

13

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) Same sign, no rootsb) Different sign, one rootc) Same sign, two rootsd) Different sign, three roots

Page 14: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

14

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.

Page 15: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

15

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.

Page 16: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

16

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.

Page 17: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

17

Page 18: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

18

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:

The required number of iterations to obtain a particular absolute error can be calculated based on the initial guesses:

Ean

x 0

2n

n log2x 0

Ea,d

Page 19: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

19

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).

Page 20: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

20

False Position Illustration

xr xu f (xu)(x l xu)f (x l ) f (xu)

Page 21: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

21

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

Page 22: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

22

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.

Page 23: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

23

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.

Page 24: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

24

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.

Page 25: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

25

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

Page 26: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

26

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!

Page 27: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

27

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.

Page 28: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

28

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.

Page 29: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

29

Relational Operators

Example Operator Relationship

x == 0 == Equal

unit ~= ‘m’ ~= Not equal

a < 0 < Less than

s > t > Greater than

3.9 <= a/3 <= Less than or equal to

r >= 0 >= Greater than or equal to

Page 30: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

30

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

Page 31: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

31

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.

Page 32: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

32

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).

Page 33: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

33

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.

Page 34: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

34

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.

Page 35: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

35

for Loops

One common way to use a for…end structure is:for index = start:step:finish statementsendwhere the index variable takes on successive values in the vector created using the : operator.

Page 36: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

36

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 Vectorization

i = 0;for t = 0:0.02:50 i = i + 1; y(i) = cos(t);end

t = 0:0.02:50;

y = cos(t);

Page 37: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

37

while Loops

A while loop can run an indeterminate number of times. The general syntax is

while condition statementsendwhere 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.

Page 38: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

38

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 = 24while (1) x = x - 5 if x < 0, break, endend

will produce x values of 24, 19, 14, 9, 4, and -1, then stop.

Page 39: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

39

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.

Page 40: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

40

Anonymous & Inline Functions

Anonymous functions are simple one-line functions created without the need for an M-filefhandle = @(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.

Page 41: Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.

41

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.