First, some left-overs… Lazy languages Nesting 1.

Slides:



Advertisements
Similar presentations
Lecture 5 Newton-Raphson Method
Advertisements

ES 240: Scientific and Engineering Computation. Chapter 17: Numerical IntegrationIntegration  Definition –Total area within a region –In mathematical.
Roundoff and truncation errors
CSE 330: Numerical Methods
ROOTS OF EQUATIONS Student Notes ENGR 351 Numerical Methods for Engineers Southern Illinois University Carbondale College of Engineering Dr. L.R. Chevalier.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
The Proportional-Integral-Derivative Controller
Chapter 4 Roots of Equations
PHYS2020 NUMERICAL ALGORITHM NOTES ROOTS OF EQUATIONS.
Open Methods (Part 1) Fixed Point Iteration & Newton-Raphson Methods
Roots of Equations Bracketing Methods.
Notes, part 5. L’Hospital Another useful technique for computing limits is L'Hospital's rule: Basic version: If, then provided the latter exists. This.
NUMERICAL METHODS WITH C++ PROGRAMMING
Notes, part 4 Arclength, sequences, and improper integrals.
Roots of Equations Open Methods Second Term 05/06.
FP1: Chapter 2 Numerical Solutions of Equations
Fin500J: Mathematical Foundations in Finance Topic 3: Numerical Methods for Solving Non-linear Equations Philip H. Dybvig Reference: Numerical Methods.
Chapter 3 Root Finding.
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Roots of Equations Chapter 3. Roots of Equations Also called “zeroes” of the equation –A value x such that f(x) = 0 Extremely important in applications.
Simpson’s 1/3 rd Rule of Integration. What is Integration? Integration The process of measuring the area under a.
1 Simpson’s 1/3 rd Rule of Integration. 2 What is Integration? Integration The process of measuring the area under a curve. Where: f(x) is the integrand.
MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014
Solving Non-Linear Equations (Root Finding)
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Part 2 Roots of Equations Why? But.
Chapter 6 Finding the Roots of Equations
Simpson Rule For Integration.
Lecture Notes Dr. Rakhmad Arief Siregar Universiti Malaysia Perlis
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
Numerical Methods Part: Simpson Rule For Integration.
Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding.
Copyright © 2006 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 ~ Roots of Equations ~ Open Methods Chapter 6 Credit:
1 Nonlinear Equations Jyun-Ming Chen. 2 Contents Bisection False Position Newton Quasi-Newton Inverse Interpolation Method Comparison.
Lecture 6 Numerical Analysis. Solution of Non-Linear Equations Chapter 2.
Riemann Sums, Trapezoidal Rule, and Simpson’s Rule Haley Scruggs 1 st Period 3/7/11.
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.
CHAPTER 3 NUMERICAL METHODS
This is an example of an infinite series. 1 1 Start with a square one unit by one unit: This series converges (approaches a limiting value.) Many series.
Newton’s Method, Root Finding with MATLAB and Excel
Today’s class Roots of equation Finish up incremental search
MECN 3500 Inter - Bayamon Lecture 6 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
AUGUST 2. MATH 104 Calculus I Review of previous material…. …methods and applications of integration, differential equations ………..
Solving Non-Linear Equations (Root Finding)
Applications of Loops: The power of MATLAB Mathematics + Coding
Numerical Methods Solution of Equation.
Today’s class Numerical differentiation Roots of equation Bracketing methods Numerical Methods, Lecture 4 1 Prof. Jinbo Bi CSE, UConn.
4 Numerical Methods Root Finding Secant Method Modified Secant
Advanced Engineering Mathematics, 7 th Edition Peter V. O’Neil © 2012 Cengage Learning Engineering. All Rights Reserved. CHAPTER 4 Series Solutions.
Linearization, Newton’s Method
4 Numerical Methods Root Finding.
INTRO TO OPTIMIZATION MATH-415 Numerical Analysis 1.
The formulae for the roots of a 3rd degree polynomial are given below
Solving Quadratic Equations by Factoring
Project on Newton’s Iteration Method Presented by Dol Nath Khanal Project Advisor- Professor Dexuan Xie 05/11/2015.
CSE 330: Numerical Methods. What is true error? True error is the difference between the true value (also called the exact value) and the approximate.
NUMERICAL ANALYSIS I. Introduction Numerical analysis is concerned with the process by which mathematical problems are solved by the operations.
CSE 330: Numerical Methods. Introduction The bisection and false position method require bracketing of the root by two guesses Such methods are called.
CMPSC 200 Fall 2013 Lecture 37 November 18, 2013.
CHAPTER 3 NUMERICAL METHODS
Algorithms and Programming
NUMERICAL DIFFERENTIATION Forward Difference Formula
Read Chapters 5 and 6 of the textbook
MATH 2140 Numerical Methods
Solution of Equations by Iteration
SOLUTION OF NONLINEAR EQUATIONS
SKTN 2393 Numerical Methods for Nuclear Engineers
Presentation transcript:

First, some left-overs… Lazy languages Nesting 1

Lazy Languages Lazy Languages (“Short-circuit evaluation”) Conditions on IF statements and WHILE loops are only evaluated as far as is needed. For example: y = 3; if 4 9 4<y is False, so x<9 is not evaluated Advantages: 1. Performance improvement 2. Avoid run-time errors (“exceptions”) 2

Lazy Languages Avoiding run-time errors (“exceptions”) if x~=0 && y/x>5 Without lazy languages, the right-hand Boolean expression could result in a “Divide by 0” error. Because the left-hand expression is evaluated, if x is 0, the && is False and there is no need to evaluate the right-hand expression. (Obviously this could be re-written – this is just to provide an example for discussion) 3

Lazy Languages Avoiding run-time errors (“exceptions”) if exist(filename) && fh=fopen(filename) Without lazy languages, the right-hand Boolean expression could result in an error. Because the left-hand expression is evaluated, if the file does not exist there is no need to evaluate the right-hand expression and so there will not be an error from opening a non-existent file. 4

Nesting “Nesting” means to embed a flow control construct within another flow control construct. “Flow control constructs”: IF, SWITCH, WHILE, FOR “The IF is nested within a WHILE loop” “We use nested loops to traverse the matrix.” Any flow control construct can be nested in any other flow control construct – and as deep as you need. 5

Nesting while x<3 x = input(‘An integer: ‘); switch x case 1 fprintf('Option 1\n'); case 2 fprintf('Option 2\n'); case 3 fprintf('Option 2\n'); otherwise end 6

Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 7

Numerical Methods Numerical methods are techniques used to find quantitative solutions to mathematical problems. Many numerical methods are iterative solutions – this means that the technique is applied over and over, gradually getting closer (i.e. converging) on the final answer. Iterative solutions are complete when the answer from the current iteration is not significantly different from the answer of the previous iteration. “Significantly different” is determined by the program or user – for example, it might mean that there is less than 1% change or it might mean less than % change. 8

Root Finding If we can find where f(x) = 0, then we can find where f(x) equals any value. To find where f(x) = -2, find the roots of f(x)+2 Essentially, we can shift any function up or down and solve for the roots, so all value finding problems can essentially be adjusted to be root finding problems. We can turn any equation into a root finding problem. 9

Bracketing Methods There are 2 basic “bracketing methods” (only 1 of which we’ll discuss). They both rely on the same basic concept If you know that f(x 1 ) > 0 and f(x 2 ) < 0, then a root must occur between x 1 and x 2. 10

Examples ES206 Fluid Dynamics 11 Solve for f : The Colebrook Equation

ES206 – Fluid Dynamics 12 “WOW – that’s hard to solve!”

Square Root 13 How about this instead: “By hand, find the square root of 127.3”

Bisection Method The Bisection Method is a very common technique for solving mathematical problems numerically. Most people have done this even if they didn’t know the name. The technique involves taking a guess for the desired variable, then applying that value to see how close the result is to a known value. Based upon the result of the previous step, a better “guess” is made and the test repeated. 14

Bisection Method Typical human method: Find the square root of First guess: 11 (since we know that 11 2 is 121) 11*11 = 121  too low (looking for 127.3) Adjust: 12 is the new guess 12*12 = 144  too high Adjust: 11.5 (“bisect” the difference) 11.5*11.5 =  too high Adjust: * =  too low etc. 15

Bisection Method Let’s use the programmer’s method – an algorithm for finding roots: Define x 1 so that f(x 1 ) is greater than 0. Define x 2 so that f(x 2 ) is less than 0. Evaluate f((x 1 +x 2 )/2) If it is positive, set x 1 to (x 1 +x 2 )/2 If is it negative, set x 2 to (x 1 +x 2 )/2 Repeat until you’re happy with either x 1 -x 2 or change in f() 16

Bisection Method For the Colebrook Equation, we know that this equation must hold: Re-arrange so that you have 0 on one side, and you have a roots problem. Guess for f and if the equation is not satisfied, adjust f and try again. This is the same as solving for = X 2. We made it X = 0 and found the roots. 17

Pros/Cons with Bisection Cons Requires f(x1) 0. CAN be hard to find Can require A LOT of iterations. Pros GUARANTEED to work 18

Newton’s Method X is the root we’re trying to find – the value where the function becomes 0 X n is the initial guess (hopefully, a reasonably-close guess!) X n+1 is the next approximation using the tangent line Credit to: 19

The Big Picture (1/4) 1 st : guess! x n 20

The Big Picture (1/4) 2 nd : Go hit the actual curve At this (x n, y n ), calculate the slope of the curve using the derivatives: m = f’(x n ) 21

The Big Picture (1/4) 3 rd : Using the slope m, the coordinates (X n, Y n ), calculate X n+1 : 22 Remember: m was obtained from the derivative – only X n+1 is unknown. What is Y n+1 ?

The Big Picture (2/4) Guess again! TOO FAR 23

The Big Picture (2/4) Iterate! 24

The Big Picture (2/4) Guess again! TOO FAR 25

The Big Picture (3/4) – Eventually… Stop the loop, when there is not much difference between the guess and the new value! CLOSE ENOUGH! Old estimate New estimate 26

Newton’s Method Prepare a MATLAB program that uses Newton’s Method to find the roots of the equation: y(x) = x 3 + 2x 2 – 300/x 27

Newton’s Method f(x) = x 3 + 2x 2 – 300/x What does it seem the answer is? 28

Newton’s Method Closer look… Actual Root! 29

Ex 1: algorithm, skeleton % Provide an initial guess for the x-intercept % Repeat until close enough: compare most recent x-intercept to previous x-intercept % Find the slope of the tangent line at the point (requires derivative) % Using the slope formula, find the x intercept of the tangent line 30

Newton’s Method % Define initial difference as very large (force loop to start) % Repeat as long as change in x is large enough % Make the “new x” act as the current x % Find slope of tangent line using f’(x) % Compute y-value % Use tangent slope to find the next value: %two points on line are the current point (x, y) % and the x-intercept (x_new, 0) 31

% Define initial difference as very large % (force loop to start) x_n = 1000; x_np1 = 5; % initial guess % Repeat as long as change in x is large enough while (abs(x_np1 – x_n) > ) % x_np1 now becomes the new guess x_n = x_np1; % Find slope of tangent line f'(x) at x_n m = 3*x_n^2 + 4*x_n + 300/(x_n^2); % Compute y-value y = x_n^3 + 2*x_n^ /x_n; % Use tangent slope to find the next value: %two points on line: the current point (x_n, y_n) % and the x-intercept (x_np1, 0) x_np1 = ((0 - y) / m) + x_n; end TOO FAR XnXn X n+1 ? XnXn 32

Newton’s Method Add fprintf ’s and see the results: x y Root of f(x) = x 3 + 2x 2 – 300/x is approximately

Real-World Numerical Methods are used in CFD, where convergence can take weeks/months to complete (get “close enough”)... while loops are generally used, since the number of iteration depends on how close the result should be, i.e. the precision. Practice! Code this, and use a scientific calculator to see how close the results are. 34

Cons Divide by 0 Error What happens if x is a maximum or minimum – the slope of the tangent line is 0. Divergence What if our tangent line leads us away from our root. Cycling f(x)=x 3 -x-3 f’(x)=3x 2 -1 x0=0 35 xixi x i

Secant Method: Find the Derivative Numerical Methods 36

Derivatives, intro. Another numerical method involves finding the value of the derivative at a point on a curve. Basically, this method uses secant lines as approximations to the tangent line. This method is used when the actual symbolic derivative is hard to find, the formula is simply not needed, or the function is prone to change as the main program does its job. 37

Derivatives, big picture What is the slope at this point? 38

Derivatives (1/5) Choose a and b to be equidistant from that point, calculate slope 1 Slope 1 X 39 range range/2

Derivatives (2/5) Zoom in on x Slope 1 x loXhiX 40 Reduce the range for the second iteration.

Derivatives (3/5) Find new secant line. Slope 1 x loXhiX 41

Derivatives (3/5) Calculate new slope Slope 1 x loXhiX loY hiY Slope 2 42

Derivatives (3/5) Slope 1 x loXhiX Slope 2 Is slope 2 “significantly different” from slope 1? Assume they are different… let’s iterate!... 43

Derivatives (4/5) Zoom in again.. x loXhiX Slope3 Slope 2 Slope 1 44

Derivatives (4/5) Zoom in again.. x loXhiX Slope3 Is Slope2 very different from Slope3? Iterate while yes! Slope 2 Slope 1 45

(5/5) Eventually… Stop the loop when the slopes are no longer “significantly different” Remember: “significantly different” means maybe a 1% difference? maybe a 0.1% difference? maybe a % difference? 46

Derivatives - Algorithm % Define initial difference as very large % (force loop to start) % Specify starting range % Repeat as long as slope has changed “a lot” % Cut range in half % Compute the new low x value % Compute the new high x value % Compute the new low y value % Compute the new high y value % Compute the slope of the secant line % end of loop Let’s do this for f(x) = sin(x) + 3/sqrt(x) 47

Code % Define initial difference as very large % (force loop to start) m = 1; oldm = 2; % Specify starting range and desired point range = 10; x=5; ctr=0; % Repeat as long as slope has changed a lot while (ctr )) % Cut range in half range = range / 2; % Compute the new low x value lox = x – range ; % Compute the new high x value hix = x + range ; % Compute the new low y value loy = sin(lox) + 3/sqrt(lox); % Compute the new high y value hiy = sin(hix) + 3/sqrt(hix); % Save the old slope oldm = m; % Compute the slope of the secant line m = (hiy – loy) / (hix – lox); % increment counter ctr = ctr + 1; end % end of loop (or have just started) 48

Derivatives - the end Add some fprintf’s and… LOW x HIGH x Slope Difference in slope 1: , Inf -Inf 2: , Inf 3: , : , : , : , : , : , : , : , : , : , : , : , : , : , : , Slope of f(x) = sin(x) + 3/sqrt(x) at x=5 is approximately

Integration 50

Integration Some functions can’t be integrated symbolically Other functions are just not much fun to do

1 st Effort The area under a curve can be obtained using rectangles. Pro: Simple – no real math Con: Slow to converge on an answer 52

Trapezoidal Rule Approximate the curve with an easy to integrate first order polynomial - This ends up being a trapezoid.

Trapezoidal Rule Trapezoidal Rule works out as:

Composite Trapezoidal Can we just combine multiple trapezoids? Yes - Instead of 1 large trapezoid, we’ll use multiple smaller trapezoids and add up their areas (and their errors) So we make a and b change. Instead of a=0 and b=5, we’ll move them from 0 and 1 then to 1 and 2, then to 2 and 3, then to 3 and 4, then to 4 and 5

xyArea Sum:4.8669

Can We Go Smaller? You can decrease the size of your trapezoids, but they are still always going to be approximating a curved line using a linear formula.

With a width of 1, the value is Error = With a width of 0.5, the value is Error =

Simpson’s 1/3 Rule Obviously, there will always be some error. Can we do a better job than a straight line at approximating the curve? Trapezoidal Method approximated two consecutive points with a line (i.e. a 1st order polynomial). What if we approximate over three points using a 2nd order polynomial?

Error =

What about 3rd & 4th Order? Simpson’s 3/8th Rule - 3rd Order Requires an EVEN number of points Boole’s Rule - 4th Order

MethodhEstimate Absolute Error Evaluations of f(x) Math Operations Trapezoidal Trapezoidal Simpson’s 1/ Simpson’s 1/ Simpson’s 3/ Simpson’s 3/ Boole’s Math Operations = Number of +,-,*,/ to calculate segment + Number of Additions to add up all segments

Wrapping Up Very small amount of code to solve a mathematical problem MATLAB can iterate 5 times, or 1,000 times, or a million! The more precision needed, the more iterations, the more time! Try to code it. See it you can find the slope of any equation. 66

Wrapping Up We learned about four numerical methods 1. Bisection Method Finds numerical solutions to complex mathematical problems Essentially a “binary search” Very general – essentially “Guess, Test, Adjust” 2. Newton’s Method for finding the roots of an equation Requires the symbolic derivative 3. Secant line approximation of a tangent point Finds the numerical value of the derivative at a point 4. Integration using rectangles, trapezoids, Simpson’s, and Boole’s 67

More… Numerical Methods is often a separate class at some universities. Other Common Topics are: Solving large systems of equations Polynomial Approximation Curve Fitting Optimization ODE/PDE Chapra and Canale’s “Numerical Methods for Engineers” is considered “the resource” Also: 68