Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00."— Presentation transcript:

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

2 22 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.

3 33 The Taylor Series

4 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 R n is of the order of h n+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.

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

6 66 Differentiation (cont’d) There are also backward difference and centered difference approximations, depending on the points used: Forward: Backward: Centered:

7 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.floating-point 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

8 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

9 The effect of the step size: 9

10 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

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

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

13 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 roots b) Different sign, one root c) Same sign, two roots d) Different sign, three roots

14 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 x l and x u where the sign of the function changes; that is, where f(x l ) f(x u ) < 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.

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

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

17 17

18 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:

19 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 (x r ). The value of x r then replaces whichever of the two initial guesses yields a function value with the same sign as f(x r ).

20 20 False Position Illustration

21 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:

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

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

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

25 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

26 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!

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

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

29 29 Relational Operators ExampleOperatorRelationship 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

30 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

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

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

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

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

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

36 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 loopVectorization i = 0; for t = 0:0.02:50 i = i + 1; y(i) = cos(t); end t = 0:0.02:50; y = cos(t);

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

38 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 = 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.

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

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

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


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

Similar presentations


Ads by Google