Numerical Methods Instructor: Prof. Reiyu Chein Office: ME building room 307 Textbook: Applied Numerical Methods with MATLAB, 4th Ed., by S.C Chapra Part 1: Modeling, computers, and error analysis Chapter 1: 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 steadystate 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 forcing independent f , parameters, variable functions variables Dependent • Dependent variable - a characteristic that usually reflects the behavior or state of the system • Independent variables - dimensions, such as time and space, along which the system’s behavior is being determined • Parameters - constants reflective of the system’s properties or composition • Forcing functions - external influences acting upon the system Model Function Example • Assuming a bungee jumper is in midflight, an analytical model for the jumper’s velocity, accounting for drag, is vt • • • • gc gm d tanh 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 g cd 2 = v dt m • Solve for v(ti+1) v(ti) cd 2 = g v ti+1 ti m v(ti+1) = v(ti) + g new = old + cd m v(ti)2 (ti+1 ti) slope step Numerical Results • Applying Euler's method in 2 s intervals yields: • How do we improve the solution? – Smaller steps – More advanced numerical models Bases for Numerical Models • Conservation laws provide the foundation for many model functions. • Different fields of engineering and science apply these laws to different paradigms within the field. • Among these laws are: Conservation of mass Conservation of momentum Conservation of charge Conservation of energy Numerical Methods to be covered in the course Part 1: Modeling, computers, and error analysis Advanced approximation theory Orthogonal polynomials, Fourier series, Fourier integral Partial differential equations (PDE) Course Grading: Homework : 40%, Midterm exam: 30%, Final exam: 30% Class material: distributed in I-Learning every Thursday. Class office hour: Tuesday 9:30~11:30am, or by appointment. Instruction for Homework: In general, a homework will be assigned for each week. It will be posted in ILearning every Thursday. Submit your homework to i-Learning and the due date will be specified. Late submission is not accepted. Submit your homework in pdf format. Indicate clearly the problem number . If you have more than one page, put the page number at the bottom center of the page. Make your homework writing as clear and complete as possible. Submit the computer code if necessary. Your homework is an individual work. You can work together with other students, but make sure you do not copy from each others’ work. Chapter 2 MATLAB Fundamentals Chapter Objectives • Learning how real and complex numbers are assigned to variables. • Learning how vectors and matrices are assigned values using simple assignment, the colon operator, and the linspace and logspace functions. • Understanding the priority rules for constructing mathematical expressions. • Gaining a general understanding of built-in functions and how you can learn more about them with MATLAB’s Help facilities. • Learning how to use vectors to create a simple line plot based on an equation. The MATLAB Environment • MATLAB uses three primary windows Command window - used to enter commands and data Graphics window(s) - used to display plots and graphics Edit window - used to create and edit M-files (programs) • Depending on your computer platform and the version of MATLAB used, these windows may have different looks and feels. Calculator Mode • The MATLAB command widow can be used as a calculator where you can type in commands line by line. Whenever a calculation is performed, MATLAB will assign the result to the built-in variable ans • Example: >> 55 - 16 ans = 39 MATLAB Variables • While using the ans variable may be useful for performing quick calculations, its transient nature makes it less useful for programming. • MATLAB allows you to assign values to variable names. This results in the storage of values to memory locations corresponding to the variable name. • MATLAB can store individual values as well as arrays; it can store numerical data and text (which is actually stored numerically as well). • MATLAB does not require that you pre-initialize a variable; if it does not exist, MATLAB will create it for you. Scalars • To assign a single value to a variable, simply type the variable name, the = sign, and the value: >> a = 4 a = 4 • Note that variable names must start with a letter, though they can contain letters, numbers, and the underscore (_) symbol Scalars (cont) • You can tell MATLAB not to report the result of a calculation by appending the semi-colon (;) to the end of a line. The calculation is still performed. • You can ask MATLAB to report the value stored in a variable by typing its name: >> a a = 4 Scalars (cont) • You can use the complex variable i (or j) to represent the unit imaginary number. • You can tell MATLAB to report the values back using several different formats using the format command. Note that the values are still stored the same way, they are just displayed on the screen differently. Some examples are: – short - scaled fixed-point format with 5 digits – long - scaled fixed-point format with 15 digits for double and 7 digits for single – short eng - engineering format with at least 5 digits and a power that is a multiple of 3 (useful for SI prefixes) Format Examples >> format short; pi ans = 3.1416 >> format long; pi ans = 3.14159265358979 >> format short eng; pi ans = 3.1416e+000 >> pi*10000 ans = 31.4159e+003 • Note - the format remains the same unless another format command is issued. Arrays, Vectors, and Matrices • MATLAB can automatically handle rectangular arrays of data - one-dimensional arrays are called vectors and twodimensional arrays are called matrices. • Arrays are set off using square brackets [ and ] in MATLAB • Entries within a row are separated by spaces or commas • Rows are separated by semicolons Array Examples • >> a = [1 2 3 4 5 ] a = 1 2 3 >> b = [2;4;6;8;10] b = 2 4 6 8 10 4 5 • Note 1 - MATLAB does not display the brackets • Note 2 - if you are using a monospaced font, such as Courier, the displayed values should line up properly Matrices • A 2-D array, or matrix, of data is entered row by row, with spaces (or commas) separating entries within the row and semicolons separating the rows: >> A = [1 2 3; 4 5 6; 7 8 9] A = 1 2 3 4 5 6 7 8 9 Useful Array Commands • The transpose operator (apostrophe) can be used to flip an array over its own diagonal. For example, if b is a row vector, b’ is a column vector containing the complex conjugate of b. • The command window will allow you to separate rows by hitting the Enter key - script files and functions will allow you to put rows on new lines as well. • The who command will report back used variable names; whos will also give you the size, memory, and data types for the arrays. Accessing Array Entries • Individual entries within a array can be both read and set using either the index of the location in the array or the row and column. • The index value starts with 1 for the entry in the top left corner of an array and increases down a column - the following shows the indices for a 4 row, 3 column matrix: 1 2 3 4 5 6 7 8 9 10 11 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 (unary operation) -8 = -8 * Multiplication and / Division 2*pi = 6.2832 pi/4 = 0.7854 \ Left Division 6\2 = 0.3333 + Addition and - Subtraction 3+5 = 8 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) >> x = 2+i*4; (or 2+4i, or 2+j*4, or 2+4j) >> y = 16; >> 3 * x ans = 6.0000 +12.0000i >> x+y ans = 18.0000 + 4.0000i >> x' ans = 2.0000 - 4.0000i Vector-Matrix Calculations • MATLAB can also perform operations on vectors and matrices. • The * operator for matrices is defined as the outer product or what is commonly called “matrix multiplication.” The number of columns of the first matrix must match the number of rows in the second matrix. The size of the result will have as many rows as the first matrix and as many columns as the second matrix. The exception to this is multiplication by a 1x1 matrix, which is actually an array operation. • The ^ operator for matrices results in the matrix being matrix-multiplied by itself a specified number of times. Note - in this case, the matrix must be square! Element-by-Element Calculations • At times, you will want to carry out calculations item by item in a matrix or vector. The MATLAB manual calls these array operations. They are also often referred to as element-byelement operations. • MATLAB defines .* and ./ (note the dots) as the array multiplication and array division operators. For array operations, both matrices must be the same size or one of the matrices must be 1x1 • Array exponentiation (raising each element to a corresponding power in another matrix) is performed with .^ Again, for array operations, both matrices must be the same size or one of the matrices must be 1x1 Built-In Functions • There are several built-in functions you can use to create and manipulate data. • The built-in help function can give you information about both what exists and how those functions are used: help elmat will list the elementary matrix creation and manipulation functions, including functions to get information about matrices. help elfun will list the elementary math functions, including trig, exponential, complex, rounding, and remainder functions. • The built-in look for command will search help files for occurrences of text and can be useful if you know a function’s purpose but not its name Graphics • MATLAB has a powerful suite of built-in graphics functions. • Two of the primary functions are plot (for plotting 2-D data) and plot3 (for plotting 3-D data). • In addition to the plotting commands, MATLAB allows you to label and annotate your graphs using the title, xlabel, ylabel, and legend commands. Plotting Example t = [0:2:20]’; g = 9.81; m = 68.1; cd = 0.25; v = sqrt(g*m/cd) * tanh(sqrt(g*cd/m)*t); plot(t, v) Plotting Annotation Example title('Plot of v versus t') xlabel('Values of t') ylabel('Values of v') grid Plotting Options • When plotting data, MATLAB can use several different colors, point styles, and line styles. These are specified at the end of the plot command using plot specifiers as found in Table 2.2. • The default case for a single data set is to create a blue line with no points. If a line style is specified with no point style, no point will be drawn at the individual points; similarly, if a point style is specified with no point style, no line will be drawn. • Examples of plot specifiers: – ‘ro:’ - red dotted line with circles at the points – ‘gd’ - green diamonds at the points with no line – ‘m--’ - magenta dashed line with no point symbols Plot line with open circle symbol Plot line with square green marker and green dashed line. Other Plotting Commands hold on and hold off hold on tells MATLAB to keep the current data plotted and add the results of any further plot commands to the graph. This continues until the hold off command, which tells MATLAB to clear the graph and start over if another plotting command is given. hold on should be used after the first plot in a series is made. subplot(m, n, p) subplot splits the figure window into an mxn array of small axes and makes the pth one active. Note - the first subplot is at the top left, then the numbering continues across the row. This is different from how elements are numbered within a matrix! subplot Three-dimensional plot Chapter 3 Programming with MATLAB Chapter Objectives • Learning how to create well-documented M-files in the edit window and invoke them from the command window. • Understanding how script and function files differ. • Understanding how to incorporate help comments in functions. • Knowing how to set up M-files so that they interactively prompt users for information and display results in the command window. • Understanding the role of subfunctions and how they are accessed. • Knowing how to create and retrieve data files. Objectives (cont) • Learning how to write clear and well-documented M-files by employing structured programming constructs to implement logic and repetition. • Recognizing the difference between if...elseif and switch constructs. • Recognizing the difference between for...end and while structures. • Understanding what is meant by vectorization and why it is beneficial. • Knowing how to animate MATLAB plots. 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 so named because the files are stored with a .m extension. • There are two main kinds of M-file Script files Function files Script Files • A script file is merely a set of MATLAB commands that are saved on a file - when MATLAB runs a script file, it is as if you typed the characters stored in the file on the command window. • Scripts can be executed either by typing their name (without the .m) in the command window, by selecting the Debug, Run (or Save and Run) command in the editing window, or by hitting the F5 key while in the editing window. Note that the latter two options will save any edits you have made, while the former will run the file as it exists on the drive. Function Files • Function files serve an entirely different purpose from script files. Function files can accept input arguments from and return outputs to the command window, but variables created and manipulated within the function do not impact the command window. Function File Syntax • The general syntax for a function is: function outvar = funcname(arglist) % helpcomments statements outvar = value; 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) will show the value on the screen. • If value is a string, enclose it in single quotes. Formatted Output • For formatted output, or for output generated by combining variable values with literal text, use the fprintf command: fprintf('format', x, y,...) where format is a string specifying how you want the value of the variables x, y, and more to be displayed - including literal text to be printed along with the values. • The values in the variables are formatted based on format codes. 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 - decimal format %g - the more compact of %e or %f • The following control codes produce special results within the format string: \n - start a new line \t - tab \\ - print the \ character Creating and Accessing Files • MATLAB has a built-in file format that may be used to save and load the values in variables. 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! ASCII Files • To create user-readable files, append the flag -ascii to the end of a save command. This will save the data to a text file in the same way that disp sends the data to a screen. • Note that in this case, MATLAB does not append anything to the file name so you may want to add an extension such as .txt or .dat. Structured Programming • Structured programming allows MATLAB to make decisions or selections based on conditions of the program. • Decisions in MATLAB are based on the result of logical and relational operations and are implemented with if, if…else, and if…elseif structures. • Selections in MATLAB are based on comparisons with a test expression and are implemented with switch structures. Relational Operators • From Table 3.2: Summary of relational operators in MATLAB: Example x == 0 unit ~= ‘m’ a < 0 s > t 3.9 <= a/3 r >= 0 Operator == ~= < > <= >= Relationship Equal Not equal Less than Greater than Less than or equal to Greater than or equal to 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) Decisions • Decisions are made in MATLAB using if structures, which may also include several elseif branches and possibly a catch-all else branch. • Deciding which branch runs is based on the result of conditions which are either true or false. If an if tree hits a true condition, that branch (and that branch only) runs, then the tree terminates. If an if tree gets to an else statement without running any prior branch, that branch will run. • Note - if the condition is a matrix, it is considered true if and only if all entries are true (or non-zero). Loops • Another programming structure involves loops, where the same lines of code are run several times. There are 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. 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. Vectorization • Sometimes, it is more efficient to have MATLAB 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); while Loops • A while loop is fundamentally different from a for loop since while loops 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. Early Termination • Sometimes it will be 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. Animation • Two ways to animate plots in MATLAB: Using looping with simple plotting functions This approach merely replots the graph over and over again. Important to use the axis command so that the plots scales are fixed. Using special function: getframe and movie This allows you to capture a sequence of plots (getframe) and then play them back (movie). Example • The (x, y) coordinates of a projectile can be generated as a function of time, t,with the following parametric equations x = v0 cos(0 t) y = v0 sin(0 t) 0.5 gt2 where v0 = initial velocity (m/s) 0 = initial angle (radians) g = gravitational constant (= 9.81 m/s2) Script • The following code illustrates both approaches: clc,clf,clear g=9.81; theta0=45*pi/180; v0=5; t(1)=0;x=0;y=0; plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8) axis([0 3 0 0.8]) M(1)=getframe; dt=1/128; for j = 2:1000 t(j)=t(j-1)+dt; x=v0*cos(theta0)*t(j); y=v0*sin(theta0)*t(j)-0.5*g*t(j)^2; plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8) axis([0 3 0 0.8]) M(j)=getframe; if y<=0, break, end end pause movie(M,1) Result movie Play recorded movie frames Syntax movie(M) movie(M,n) movie(M,n,fps) movie(h,...) movie(h,M,n,fps,loc) Description movie plays the movie defined by a matrix whose columns are movie frames (usually produced by getframe). movie(M) plays the movie in matrix M once. movie(M,n) plays the movie n times. If n is negative, each cycle is shown forward then backward. If n is a vector, the first element is the number of times to play the movie, and the remaining elements make up a list of frames to play in the movie. For example, if M has four frames then n = [10 4 4 2 1] plays the movie ten times, and the movie consists of frame 4 followed by frame 4 again, followed by frame 2 and finally frame 1. movie(M,n,fps) plays the movie at fps frames per second. The default is 12 frames per second. Computers that cannot achieve the specified speed play as fast as possible. movie(h,...) plays the movie centered in the figure or axes identified by the handle h. movie(h,M,n,fps,loc) specifies a four-element location vector, [x y 0 0], where the lower left corner of the movie frame is anchored (only the first two elements in the vector are used). The location is relative to the lower left corner of the figure or axes specified by handle h and in units of pixels, regardless of the object's Units property. 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. Chapter 4 Roundoff and Truncation Errors Chapter Objectives • Understanding the distinction between accuracy and precision. • Learning how to quantify error. • Learning how error estimates can be used to decide when to terminate an iterative calculation. • Understanding how roundoff errors occur because digital computers have a limited ability to represent numbers. • Understanding why floating-point numbers have limits on their range and precision. Objectives (cont) • Recognizing that truncation errors occur when exact mathematical formulations are represented by approximations. • Knowing how to use the Taylor series to estimate truncation errors. • Understanding how to write forward, backward, and centered finite-difference approximations of the first and second derivatives. • Recognizing that efforts to minimize truncation errors can sometimes increase roundoff errors. Accuracy and Precision • a) b) c) d) Accuracy refers to how closely a computed or measured value agrees with the true value, while precision refers to how closely individual computed or measured values agree with each other. inaccurate and imprecise accurate and imprecise inaccurate and precise accurate and precise Error Definitions • True error (Et): the difference between the true value and the approximation. • Absolute error (|Et|): the absolute difference between the true value and the approximation. • True fractional relative error: the true error divided by the true value. • Relative error (t): the true fractional relative error expressed as a percentage. • Note: the subscript t denotes as relative to true value Error Definitions (cont.) • The previous definitions of error relied on knowing a true value. If that is not the case, approximations can be made to the error. • The approximate percent relative error a can be given as the approximate error divided by the approximation, expressed as a percentage - though this presents the challenge of finding the approximate error! • For iterative processes, the error can be approximated as the difference in values between successive iterations. • Note: the subscript a denotes as relative to approximated value. Using Error Estimates • Often, when performing calculations, we may not be concerned with the sign of the error but are interested in whether the absolute value of the percent relative error is lower than a prespecified tolerance s. For such cases, the computation is repeated until |a|< s • This relationship is referred to as a stopping criterion. For stopping criterion s=0.05%, we can choose 6 terms to represent ex. Roundoff Errors • Roundoff errors arise because digital computers cannot represent some quantities exactly. There are two major facets of roundoff errors involved in numerical calculations: – Digital computers have size and precision limits on their ability to represent numbers. – Certain numerical manipulations are highly sensitive to roundoff errors. Computer Number Representation • By default, MATLAB has adopted the IEEE doubleprecision format in which eight bytes (64 bits) are used to represent floating-point numbers: n = ±(1+f) x 2e • The sign is determined by a sign bit • The mantissa f is determined by a 52-bit binary number • The exponent e is determined by an 11-bit binary number Floating Point Ranges • Values of 1023 and +1024 for e are reserved for special meanings, so the exponent range is 1022 to 1023. • The largest possible number MATLAB can store has f of all 1’s, giving a significand of 2 2-52, or approximately 2 e of 111111111102, giving an exponent of 2046 1023 = 1023 This yields approximately 21024 = 1.799710308 • The smallest possible number MATLAB can store with full precision has f of all 0’s, giving a significand of 1 e of 000000000012, giving an exponent of 11023 = 1022 This yields 21022 = 2.225110308 Floating Point Precision • The 52 bits for the mantissa f correspond to about 15 to 16 base-10 digits. The machine epsilon - the maximum relative error between a number and MATLAB’s representation of that number, is thus 252 = 2.22041016 Roundoff Errors with Arithmetic Manipulations • Roundoff error can happen in several circumstances other than just storing numbers - for example: – Large computations - if a process performs a large number of computations, roundoff errors may build up to become significant – Adding a Large and a Small Number - Since the small number’s mantissa is shifted to the right to be the same scale as the large number, digits are lost – Smearing - Smearing occurs whenever the individual terms in a summation are larger than the summation itself. • (x + 10-20) x = 10-20 mathematically, but x = 1; (x + 10-20) x gives a 0 in MATLAB! Truncation Errors • Truncation errors are those that result from using an approximation in place of an exact mathematical procedure. • Example 1: approximation to a derivative using a finite-difference equation: dv v v(t i1 ) v(t i ) dt t t i1 t i • Example 2: The Taylor Series 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 '' (3) (n ) f x f x f xi n ' i 2 i 3 f x i1 f x i f x i h h h 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: Given: f (x i1) f (x i ) f ' (x i )h O(h 2 ) Then: f (x i1 ) 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. 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. Chapter 5 Roots: Bracketing Methods Chapter Objectives • Understanding what roots problems are and where they occur in engineering and science. • Knowing how to determine a root graphically. • Understanding the incremental search method and its shortcomings. • Knowing how to solve a roots problem with the bisection method. • Knowing how to estimate the error of bisection and why it differs from error estimates for other types of root location algorithms. • Understanding false position and how it differs from bisection. Roots • “Roots” problems occur when some function f can be written in terms of one or more dependent variables x, where the solutions to f(x)=0 yields the solution to the problem. • These problems often occur when a design problem presents an implicit equation for a required parameter. 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 (odd number) c) Same sign, two roots (no or even number) d) Different sign, three roots (odd number) 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. 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 evenmultiplicity roots regardless of spacing. 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. • Generating a series 0 r 1 r 2 r n r x , x , x ......., x , x • • n 1 r ........ This is called an iteration process Convergent criterion a x n 1 r x x n 1 r n r 100% s s : stopping criterion, say,10 6 Programming Bisection 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: x E n 2 n a 0 The required number of iterations to obtain a particular absolute error can be calculated based on the initial guesses: x 0 n log 2 E a,d Ea ,d desired error x x x 0 0 u 0 l 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). False Position Illustration f (x u )(x l x u ) xr xu f (x l ) f (x u ) 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! 10 f (x) x 1 • Bad: