Chapter 1 Computing Tools

Download Report

Transcript Chapter 1 Computing Tools

The Bisection Method

Chapter 6 Finding the Roots of Equations

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

Finding Roots of Equations

• • • • In this chapter, we are examining equations with one independent variable.

These equations may be linear or non-linear Non-linear equations may be polynomials or generally non-linear equations A root of the equation is simply a value of the independent variable that satisfies the equation

Engineering Computation: An Introduction Using MATLAB and Excel

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

Finding Roots of Equations

• • As with our method for solving simultaneous non linear equations, we often set the equation to be equal to zero when the equation is satisfied Example: • If we say that then when f(y) =0, the equation is satisfied

Engineering Computation: An Introduction Using MATLAB and Excel

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

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 Example: Consider the function

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• Consider an initial interval of y lower 10 = -10 to y upper = • • Since the signs are opposite, we know that the method will converge to a root of the equation The value of the function at the midpoint of the interval is:

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• The method can be better understood by looking at a graph of the function: Interval

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• • Now we eliminate half of the interval, keeping the half where the sign of f(midpoint) is opposite the sign of f(endpoint) In this case, since f(y mid ) = -6 and f(y function crosses zero in this interval upper ) = 64, we keep the upper half of the interval, since the

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• • Now we eliminate half of the interval, keeping the half where the sign of f(midpoint) is opposite the sign of f(endpoint) In this case, since f(y mid ) = -6 and f(y function crosses zero in this interval upper ) = 64, we keep the upper half of the interval, since the

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• The interval has now been bisected, or halved: New Interval

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• • New interval: y lower Function values: = 0, y upper = 10, y mid = 5 • Since f(y lower ) and f(y mid ) have opposite signs, the lower half of the interval is kept

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• • • • At each step, the difference between the high and low values of y is compared to 2*(allowable error) If the difference is greater, than the procedure continues Suppose we set the allowable error at 0.0005. As long as the width of the interval is greater than 0.001, we will continue to halve the interval When the width is less than 0.001, then the midpoint of the range becomes our answer

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• Excel solution: Initial Guesses Evaluate function at lower and mid values.

Is interval width narrow enough to stop?

If signs are same (+ product), eliminate lower half of interval.

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• Next iteration: New Interval (if statements based on product at the end of previous row) Evaluate function at lower and mid values.

Is interval width narrow enough to stop?

If signs are different (- product), eliminate upper half of interval.

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• Continue until interval width < 2*error (16 iterations) Answer: y = 0.857

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example

• • • • Or course, we know that the exact answer is 6/7 (0.857143) If we wanted our answer accurate to 5 decimal places, we could set the allowable error to 0.000005

This increases the number of iterations only from 16 to 22 – the halving process quickly reduces the interval to very small values Even if the initial guesses are set to -10,000 and 10000, only 32 iterations are required to get a solution accurate to 5 decimal places

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• Now consider this example: • Use the bisection method, with allowed error of 0.0001

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• If limits of -10 to 0 are selected, the solution converges to x = -2

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• If limits of 0 to 10 are selected, the solution converges to x = 4

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• • If limits of -10 to 10 are selected, which root is found?

In this case f(-10) and f(10) are both positive, and f(0) is negative

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• • Which half of the interval is kept?

Depends on the algorithm used – in our example, if the function values for the lower limit and midpoint are of opposite signs, we keep the lower half of the interval

Engineering Computation: An Introduction Using MATLAB and Excel

Bisection Method Example - Polynomial

• Therefore, we converge to the negative root

Engineering Computation: An Introduction Using MATLAB and Excel

In-Class Exercise

• • Draw a flow chart of the algorithm used to find a root of an equation using the bisection method Write the MATLAB code to determine a root of within the interval x = 0 to 10

Engineering Computation: An Introduction Using MATLAB and Excel

Define tolerance tol Keep lower half of range: high = mid NO Input lower and upper limits low and high while high-low > 2*tol mid = (high+low)/2 Evaluate function at lower limit and midpoint: fl = f(low), fm = f(mid) fl*fm > 0? YES Keep upper half of range: low = mid Display root (mid)

MATLAB Solution

• Consider defining the function • as a MATLAB function “fun1” This will allow our bisection program to be used on other functions without editing the program – only the MATLAB function needs to be modified

Engineering Computation: An Introduction Using MATLAB and Excel

MATLAB Function

function y = fun1(x) y = exp(x) - 15*x -10; Check values at x = 0 and x = 10: >> fun1(0) ans = -9 >> fun1(10) ans = 2.1866e+004 Different signs, so a root exists within this range

Engineering Computation: An Introduction Using MATLAB and Excel

Set tolerance to 0.00001; answer will be accurate to 5 decimal places

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Engineering Computation: An Introduction Using MATLAB and Excel

Find Root

>> bisect Enter the lower limit 0 Enter the upper limit 10 Root found: 4.3135

Engineering Computation: An Introduction Using MATLAB and Excel

What if No Root Exists?

• Try interval of 0 to 3: >> bisect Enter the lower limit 0 Enter the upper limit 3 • Root found: 3.0000

This value is not a root – we might want to add a check to see if the converged value is a root

Engineering Computation: An Introduction Using MATLAB and Excel

Modified Code

• Add “solution tolerance” (usually looser than convergence tolerance): • Add check at end of program:

Engineering Computation: An Introduction Using MATLAB and Excel

Check Revised Code

>> bisect Enter the lower limit 0 Enter the upper limit 3 No root found

Engineering Computation: An Introduction Using MATLAB and Excel

Numerical Tools

• • Of course, Excel and MATLAB have built-in tools for finding roots of equations However, the examples we have considered illustrate an important concept about non-linear solutions:

Remember that there may be many roots to a non-linear equation. Even when specifying an interval to be searched, keep in mind that there may be multiple solutions (or no solution) within the interval.

Engineering Computation: An Introduction Using MATLAB and Excel