35
Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Embed Size (px)

Citation preview

Page 1: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Chapter 6Finding the Roots of

Equations

Newton’s Method,Root Finding with MATLAB and Excel

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 2: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Review: Classification of Equations

• Linear: independent variable appears to the first power only, either alone or multiplied by a constant

• Nonlinear:– Polynomial: independent variable appears

raised to powers of positive integers only– General non-linear: all other equations

Engineering Computation: An Introduction Using MATLAB and Excel

Page 3: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Review: Solution Methods

• Linear: Easily solved analytically• Polynomials: Some can be solved analytically (such

as by quadratic formula), but most will require numerical solution

• General non-linear: unless very simple, will require numerical solution

Engineering Computation: An Introduction Using MATLAB and Excel

Page 4: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Review: The Bisection Method

• In the bisection method, we start with an interval (initial low and high guesses) and halve its width until the interval is sufficiently small

• As long as the initial guesses are such that the function has opposite signs at the two ends of the interval, this method will converge to a solution

Engineering Computation: An Introduction Using MATLAB and Excel

Page 5: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method

• Newton’s Method (also know as the Newton-Rapshon Method) is another widely-used algorithm for finding roots of equations

• In this method, the slope (derivative) of the function is calculated at the initial guess value and projected to the x-axis

• The corresponding x-value becomes the new guess value

• The steps are repeated until the answer is obtained to a specified tolerance

Engineering Computation: An Introduction Using MATLAB and Excel

Page 6: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method

Engineering Computation: An Introduction Using MATLAB and Excel

Initial guess xi

y(xi)Tangent Line: Slope = y’(xi)

Page 7: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method

Engineering Computation: An Introduction Using MATLAB and Excel

xi

y(xi)

y(xi)/y’(xi)

New guess for x:xn = xi - y(xi)/y’(xi)

Page 8: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

• Find a root of this equation:

• The first derivative is:

• Initial guess value: x = 10

Engineering Computation: An Introduction Using MATLAB and Excel

Page 9: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

• For x = 10:

• This is the new value of x

Engineering Computation: An Introduction Using MATLAB and Excel

Page 10: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

Engineering Computation: An Introduction Using MATLAB and Excel

Initial guess =

10

y = 1350y’ = 580

New x-value = 7.6724

Page 11: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

• For x = 7.6724:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 12: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

Engineering Computation: An Introduction Using MATLAB and Excel

Initial value = 7.6724

y = 368.494y’ = 279.621

New x-value = 6.3546

Page 13: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

• Continue iterations:

Engineering Computation: An Introduction Using MATLAB and Excel

Method quickly converges to this root

Page 14: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

• To find the other roots, use different initial guess values

Engineering Computation: An Introduction Using MATLAB and Excel

Page 15: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method Example

Engineering Computation: An Introduction Using MATLAB and Excel

• Three roots found:

x = -2.0764 x = 1.4187

x = 5.6577

Page 16: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Newton’s Method - Comments

• Usually converges to a root much faster than the bisection method

• In some cases, the method will not converge (discontinuous derivative, initial slope = 0, etc.)

• In most cases, however, if the initial guess is relatively close to the actual root, the method will converge

• Don’t necessarily need to calculate the derivative: can approximate the slope from two points close to the x-value. This is called the Secant Method

Engineering Computation: An Introduction Using MATLAB and Excel

Page 17: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

In-Class Exercise

• Draw a flow chart of Newton’s Method• Write the MATLAB code to apply Newton’s Method

to the previous example:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 18: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Engineering Computation: An Introduction Using MATLAB and Excel

Define converge tolerance tol

while abs(f(x)) > tol

Input initial guess xCalculate f(x)

Calculate slope fpr(x)x = x – f(x)/fpr(x)

Calculate f(x)

Output root x

Page 19: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

MATLAB Code

• MATLAB functions defining the function and its derivative:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 20: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

MATLAB Code

Engineering Computation: An Introduction Using MATLAB and Excel

Page 21: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

MATLAB Results

>> Newton

Enter initial guess

10

Root found: 5.6577

>> Newton

Enter initial guess

0

Root found: 1.4187

>> Newton

Enter initial guess

-10

Root found: -2.0764

Engineering Computation: An Introduction Using MATLAB and Excel

Page 22: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Excel and MATLAB Tools

• Bisection method or Newton’s method can be easily programmed

• However, Excel and MATLAB have built-in tools for finding roots of equations

Engineering Computation: An Introduction Using MATLAB and Excel

Page 23: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Excel and MATLAB Tools

• General non-linear equations:– Excel: Goal Seek, Solver– MATLAB: fzero

• Polynomials:– MATLAB: roots

• Graphing tools are also important to locate roots approximately

Engineering Computation: An Introduction Using MATLAB and Excel

Page 24: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Excel Tools

• We have seen how to use Solver for a system of equations; we can easily apply it to find roots of a single equation

• There is a tool in Excel even easier to use for a single equation: Goal Seek

• With Goal Seek, we instruct Excel to change the value of a cell containing the independent variable to cause the value of the cell containing the function to equal a given value

Engineering Computation: An Introduction Using MATLAB and Excel

Page 25: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Goal Seek Example

Engineering Computation: An Introduction Using MATLAB and Excel

Page 26: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Goal Seek Example

Engineering Computation: An Introduction Using MATLAB and Excel

Page 27: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

fzero Example

• The MATLAB function fzero finds the root of a function, starting with a guess value

• Example: function fun1 defines this equation:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 28: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

fzero Example

• The function fzero has arguments of the name of the function to be evaluated and a guess value:

>> fzero('fun1',10)

ans =

5.6577

• Or the name of function to be evaluated and a range of values to be considered:

>> fzero('fun1',[4 10])

ans =

5.6577

Engineering Computation: An Introduction Using MATLAB and Excel

Page 29: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

fzero Example

• If a range is specified, then the function must have opposite signs at the endpoints

> fzero('fun1',[0 10])

??? Error using ==> fzero at 292

The function values at the interval endpoints must differ in sign.

Engineering Computation: An Introduction Using MATLAB and Excel

Page 30: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Graphing to Help Find Roots

• Making a graph of the function is helpful to determine how many real roots exist, and the approximate locations of the roots

• Example: Consider these two equations:

• How many roots does each have?

Engineering Computation: An Introduction Using MATLAB and Excel

Page 31: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Graphing to Help Find Roots

• Equation 1: Equation 2:

Engineering Computation: An Introduction Using MATLAB and Excel

3 real roots 1 real root

Page 32: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

roots Example

• For polynomials, the MATLAB function roots finds all of the roots, including complex roots

• The argument of roots is an array containing the coefficients of the equation

• For example, for the equation

the coefficient array is [3, -15, -20, 50]

Engineering Computation: An Introduction Using MATLAB and Excel

Page 33: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

roots Example

>> A = [3, -15, -20, 50];

>> roots(A)

ans =

5.6577

-2.0764

1.4187

Engineering Computation: An Introduction Using MATLAB and Excel

Page 34: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

roots Example

• Now find roots of

>> B = [3, -5, -20, 50];

>> roots(B)

ans =

-2.8120

2.2393 + 0.9553i

2.2393 - 0.9553i

Engineering Computation: An Introduction Using MATLAB and Excel

Two complex roots

Page 35: Chapter 6 Finding the Roots of Equations Newton’s Method, Root Finding with MATLAB and Excel Copyright © The McGraw-Hill Companies, Inc. Permission required

Summary

• The bisection method and Newton’s method (or secant method) are widely-used algorithms for finding the roots of equations

• When using any tool to find the roots of non-linear equations, remember that multiple roots may exist

• The initial guess value will affect which root is found

Engineering Computation: An Introduction Using MATLAB and Excel