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

Slides:



Advertisements
Similar presentations
Part 1 Chapter 4 Roundoff and Truncation Errors PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The McGraw-Hill.
Advertisements

Roundoff and truncation errors
Roots: Bracketing Methods
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Introduction to C Programming
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Chapter 4 Roots of Equations
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
JavaScript, Third Edition
Dr. Marco A. Arocha Aug,  “Roots” problems occur when some function f can be written in terms of one or more dependent variables x, where the.
Introduction to C Programming
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
ES 240: Scientific and Engineering Computation. Chapter 4 Chapter 4: Errors Uchechukwu Ofoegbu Temple University.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Input, Output, and Processing
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Part 2 Chapter 5 Roots: Bracketing Methods PowerPoints organized by Dr. Michael R. Gustafson II, Duke University All images copyright © The McGraw-Hill.
Flow of Control Part 1: Selection
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Chapter 3 Roots of Equations. Objectives Understanding what roots problems are and where they occur in engineering and science Knowing how to determine.
Numerical Methods for Engineering MECN 3500
Numerical Methods.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Part 1 Chapter 3 Programming with MATLAB PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Today’s class Numerical differentiation Roots of equation Bracketing methods Numerical Methods, Lecture 4 1 Prof. Jinbo Bi CSE, UConn.
1 Approximation. 2 Taylor Series 3 Truncation Error  In general, the nth order Taylor series expansion will be exact for an nth order polynomial  In.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Digital Image Processing Introduction to M-function Programming.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Roots: Bracketing Methods
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
EEE 161 Applied Electromagnetics
Programming with MATLAB
Chapter 4: Making Decisions.
MATLAB: Structures and File I/O
Roundoff and Truncation Errors
Roots: Bracketing Methods
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
244-2: MATLAB/Simulink FUNDAMENTALS
Matlab Basics.
Roundoff and Truncation Errors
Roots: Bracketing Methods
Presentation transcript:

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

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.

33 The Taylor Series

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.

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.

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

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 After 22 months of recomputing the index and truncating to three decimal places at each change in market value, the index stood at , despite the fact that its "true" value should have been  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

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

The effect of the step size: 9

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

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 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 False Position Illustration

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 Input comand n = input('promptstring')   whatever value is typed is stored in n.  displays the characters in promptstring.  Example: if you type pi, n = … 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 Anonymous & Inline Functions Anonymous functions are simple one-line functions created without the need for an M-file fhandle 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 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.