You are on page 1of 35

Newtons Method, Root Finding with MATLAB and Excel

TOPIC 1 Finding the Roots of Equations

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

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

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

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

Newtons Method
Newtons Method (also know as the NewtonRapshon 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

Newtons Method

Tangent Line: Slope = y(xi)

y(xi)

Initial guess xi
Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method

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

y(xi)

xi

y(xi)/y(xi)
Engineering Computation: An Introduction Using MATLAB and Excel

Newtons 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

Newtons Method Example


For x = 10:

This is the new value of x


Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example

y = 1350 y = 580 New x-value = 7.6724

Initial guess = 10

Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example


For x = 7.6724:

Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example


y = 368.494 y = 279.621 Initial value = 7.6724

New x-value = 6.3546

Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example


Continue iterations:

Method quickly converges to this root


Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example


To find the other roots, use different initial guess values

Engineering Computation: An Introduction Using MATLAB and Excel

Newtons Method Example


Three roots found:

x = -2.0764

x = 1.4187

x = 5.6577
Engineering Computation: An Introduction Using MATLAB and Excel

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

In-Class Exercise
Draw a flow chart of Newtons Method Write the MATLAB code to apply Newtons Method to the previous example:

Engineering Computation: An Introduction Using MATLAB and Excel

Define converge tolerance tol Input initial guess x Calculate f(x)

while abs(f(x)) > tol


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

Output root x
Engineering Computation: An Introduction Using MATLAB and Excel

MATLAB Code
MATLAB functions defining the function and its derivative:

Engineering Computation: An Introduction Using MATLAB and Excel

MATLAB Code

Engineering Computation: An Introduction Using MATLAB and Excel

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

Excel and MATLAB Tools


Bisection method or Newtons 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

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

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

Goal Seek Example

Engineering Computation: An Introduction Using MATLAB and Excel

Goal Seek Example

Engineering Computation: An Introduction Using MATLAB and Excel

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

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

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

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

Graphing to Help Find Roots


Equation 1: Equation 2:

3 real roots

1 real root

Engineering Computation: An Introduction Using MATLAB and Excel

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

roots Example
>> A = [3, -15, -20, 50]; >> roots(A) ans = 5.6577 -2.0764 1.4187

Engineering Computation: An Introduction Using MATLAB and Excel

roots Example
Now find roots of

>> B = [3, -5, -20, 50]; >> roots(B) ans = -2.8120 2.2393 + 0.9553i Two complex roots 2.2393 - 0.9553i
Engineering Computation: An Introduction Using MATLAB and Excel

Summary
The bisection method and Newtons 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

You might also like