Applications of Loops: The power of MATLAB Mathematics + Coding

Slides:



Advertisements
Similar presentations
Numerical Computation Lecture 4: Root Finding Methods - II United International College.
Advertisements

Lecture 5 Newton-Raphson Method
CSE 330: Numerical Methods
Newton’s Method finds Zeros Efficiently finds Zeros of an equation: –Solves f(x)=0 Why do we care?
Numeriska beräkningar i Naturvetenskap och Teknik 1.Solving equations.
Chapter 4 Roots of Equations
PHYS2020 NUMERICAL ALGORITHM NOTES ROOTS OF EQUATIONS.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 6 Roots of Equations Bracketing Methods.
APPLICATIONS OF DIFFERENTIATION Newton’s Method In this section, we will learn: How to solve high-degree equations using Newton’s method. APPLICATIONS.
The Derivative. Objectives Students will be able to Use the “Newton’s Quotient and limits” process to calculate the derivative of a function. Determine.
Lecture 1: Numerical Solution of Non-linear equations.
NUMERICAL METHODS WITH C++ PROGRAMMING
Notes, part 4 Arclength, sequences, and improper integrals.
FP1: Chapter 2 Numerical Solutions of Equations
Chapter 3 Root Finding.
MATH 117 Pre-calculus Outline Part 1: Review of basic algebra and graphs, and Straight lines (constant slope) Part 2: Functions, quadratic functions (slope.
Secant Method Another Recursive Method. Secant Method The secant method is a recursive method used to find the solution to an equation like Newton’s Method.
First, some left-overs… Lazy languages Nesting 1.
MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014
Solving Non-Linear Equations (Root Finding)
Inverse Kinematics.
Introduction This chapter gives you several methods which can be used to solve complicated equations to given levels of accuracy These are similar to.
Simulation and Animation
Numerical Methods Applications of Loops: The power of MATLAB Mathematics + Coding 1.
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:
Lecture 6 Numerical Analysis. Solution of Non-Linear Equations Chapter 2.
Graphics Graphics Korea University kucg.korea.ac.kr 2. Solving Equations of One Variable Korea University Computer Graphics Lab. Lee Seung Ho / Shin.
4.5: Linear Approximations, Differentials and Newton’s Method.
Numerical Methods for Engineering MECN 3500
Numerical Methods.
CHAPTER 3 NUMERICAL METHODS
TI-83 An Introduction to Graphing Mathematics Staff Development Lincoln Public Schools August 25, 2005 © Jerel L. Welker
Newton’s Method, Root Finding with MATLAB and Excel
Dr. Jie Zou PHY Chapter 2 Solution of Nonlinear Equations: Lecture (II)
Linearization and Newton’s Method. I. Linearization A.) Def. – If f is differentiable at x = a, then the approximating function is the LINEARIZATION of.
Lecture 5 - Single Variable Problems CVEN 302 June 12, 2002.
AUGUST 2. MATH 104 Calculus I Review of previous material…. …methods and applications of integration, differential equations ………..
Area Under the Curve We want to approximate the area between a curve (y=x 2 +1) and the x-axis from x=0 to x=7 We want to approximate the area between.
Solving Non-Linear Equations (Root Finding)
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.
Iteration Methods “Mini-Lecture” on a method to solve problems by iteration Ch. 4: (Nonlinear Oscillations & Chaos). Some nonlinear problems are solved.
4 Numerical Methods Root Finding Secant Method Modified Secant
4.8 Newton’s Method Mon Nov 9 Do Now Find the equation of a tangent line to f(x) = x^5 – x – 1 at x = 1.
Linearization, Newton’s Method
Solving Absolute Value Equations The absolute value of x is defined as Example 1.
Warm Up Write an equation of the tangent line to the curve at the given point. 1)f(x)= x 3 – x + 1 where x = -1 2)g(x) = 3sin(x/2) where x = π/2 3)h(x)
The formulae for the roots of a 3rd degree polynomial are given below
Newton’s Method Problem: need to solve an equation of the form f(x)=0. Graphically, the solutions correspond to the points of intersection of the.
Project on Newton’s Iteration Method Presented by Dol Nath Khanal Project Advisor- Professor Dexuan Xie 05/11/2015.
4.5: Linear Approximations, Differentials and Newton’s Method Greg Kelly, Hanford High School, Richland, Washington.
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.
CSE 330: Numerical Methods. Introduction The bisection and false position method require bracketing of the root by two guesses Such methods are called.
Modeling of geochemical processes Numeric Mathematics Refreshment
The formulae for the roots of a 3rd degree polynomial are given below
Numerical Methods.
Ch. 5 – Applications of Derivatives
Numerical Methods.
2-4: Tangent Line Review &
Solution of Equations by Iteration
SOLUTION OF NONLINEAR EQUATIONS
Section 4.8: Newton’s Method
3.8: Newton’s Method Greg Kelly, Hanford High School, Richland, Washington.
4.5: Linear Approximations, Differentials and Newton’s Method
FP1: Chapter 2 Numerical Solutions of Equations
3.8: Newton’s Method Greg Kelly, Hanford High School, Richland, Washington.
Copyright © Cengage Learning. All rights reserved.
MATH 1910 Chapter 3 Section 8 Newton’s Method.
Presentation transcript:

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

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 0.00001% change.

y(x) = x3 + 2x2 – 300/x Ex 1. Newton’s Method Newton’s Method is used to find the roots of an equation. “When the curve crosses the x-axis.” “When f(x)=0.” Prepare a MATLAB program that uses Newton’s Method to find the roots of the equation: y(x) = x3 + 2x2 – 300/x

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

Newton’s Method Closer look… Actual Root!

Newton’s Method X is the root we’re trying to find – the value where the function becomes 0 Xn is the initial guess (hopefully, a reasonably-close guess!) Xn+1 is the next approximation using the tangent line Credit to: http://en.wikipedia.org/wiki/Newton’s_method

The Big Picture (1/4) 1st: guess! xn Guess an Xn Hit the actual curve, find the slope of the tangent at that point (using f’(x)) Using this tangent, find the X intercept: Xn+1 1st: guess! xn

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

The Big Picture (1/4) 3rd: Using the slope m, the coordinates (Xn , Yn), calculate Xn+1 : Remember: m was obtained from the derivative – only Xn+1 is unknown. What is Yn+1? Guess an Xn Hit the actual curve, find the slope of the tangent at that point (using f’(x)) Using this tangent, find the X intercept: Xn+1

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

The Big Picture (2/4) Iterate!

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

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

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

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)

Xn+1 Xn Xn+1 Xn % 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) > 0.0000001) % 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^2 - 300/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 Xn Xn+1 ? Xn Xn+1

Newton’s Method Add fprintf’s and see the results: x y ------------------- ------------------- 5.000000000000000 115.000000000000000 3.925233644859813 14.864224007996924 3.742613907949879 0.279824373263295 3.739045154311932 0.000095471294827 3.739043935883257 0.000000000011084 Root of f(x) = x3 + 2x2 – 300/x is approximately 3.7390439

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.

Secant Method: Find the Derivative Numerical Methods Secant Method: Find the Derivative

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.

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

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

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

Derivatives (3/5) Slope 1 x loX hiX Find new secant line.

Derivatives (3/5) Slope 1 Slope 2 hiY loY x loX hiX Calculate new slope

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

Derivatives (4/5) Slope 1 Slope 2 Slope3 x loX hiX Zoom in again..

Derivatives (4/5) Is Slope2 very different from Slope3? Iterate while yes! x loX hiX Zoom in again..

(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 0.000001% difference?

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)

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<3 || (abs((m-oldm)/oldm) > 0.00000001)) % 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 Code (or have just started)

Derivatives - the end Add some fprintf’s and… LOW x HIGH x Slope Difference in slope 1: 0.000000000000000, 10.000000000000000 -Inf -Inf 2: 2.500000000000000, 7.500000000000000 -0.092478729683983 Inf 3: 3.750000000000000, 6.250000000000000 0.075675505484728 0.168154235168711 4: 4.375000000000000, 5.625000000000000 0.130061340134248 0.054385834649520 5: 4.687500000000000, 5.312500000000000 0.144575140383541 0.014513800249293 6: 4.843750000000000, 5.156250000000000 0.148263340290110 0.003688199906569 7: 4.921875000000000, 5.078125000000000 0.149189163013407 0.000925822723297 8: 4.960937500000000, 5.039062500000000 0.149420855093148 0.000231692079740 9: 4.980468750000000, 5.019531250000000 0.149478792897432 0.000057937804284 10: 4.990234375000000, 5.009765625000000 0.149493278272689 0.000014485375257 11: 4.995117187500000, 5.004882812500000 0.149496899674250 0.000003621401561 12: 4.997558593750000, 5.002441406250000 0.149497805028204 0.000000905353954 13: 4.998779296875000, 5.001220703125000 0.149498031366966 0.000000226338761 14: 4.999389648437500, 5.000610351562500 0.149498087951815 0.000000056584850 15: 4.999694824218750, 5.000305175781250 0.149498102098005 0.000000014146190 16: 4.999847412109375, 5.000152587890625 0.149498105634484 0.000000003536479 17: 4.999923706054688, 5.000076293945313 0.149498106518877 0.000000000884393 Slope of f(x) = sin(x) + 3/sqrt(x) at x=5 is approximately 0.1494981

Numerical Methods Bisection Method A general purpose mathematical solution finder

More Examples ES206 Fluid Dynamics Solve for f The Colebrook Equation

ES206 – Fluid Dynamics “WOW – that’s hard to solve!” How about this instead: “By hand, find the square root of 127.3” Both problems use the same technique to solve: the Bisection Method.

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.

Bisection Method Typical human method: Find the square root of 127.3 First guess: 11 (since we know that 112 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 = 132.25  too high Adjust: 11.25 11.25 * 11.25 = 126.5625  too low etc.

Bisection Method Let’s use the programmer’s method – an algorithm: Define highest possible answer (HI) and lowest possible answer (LO) NEW is the most recent estimate: halfway between HI and LO. OLD is defined to be very far from NEW. As long as NEW is far enough from OLD: If NEW is too high, HI gets the value of NEW; if it’s too low, LO gets the value of NEW. OLD gets the value that was in NEW NEW gets the value that lies halfway between HI and LO.

Bisection Method For the Colebrook Equation, we know that this equation must hold: Re-arrange 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 127.3 = X2 . We made it X2 -127.3 = 0 and found the roots.

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.

Wrapping Up We learned about three numerical methods 1. Newton’s Method for finding the roots of an equation Requires the symbolic derivative 2. Secant line approximation of a tangent point Finds the numerical value of the derivative at a point 3. Bisection Method Finds numerical solutions to complex mathematical problems Very general – essentially “Guess, Test, Adjust”