Docsity

Newton's Method: A Numerical Technique for Finding Roots of Functions - Prof. Bryon Ehlman, Study notes of Computer Science

Toggle sidebar
bg1
Notes—Chap. 10a Introduction to Numerical Methods
(pp. 405 – 413)
- Chapter presents program solutions to some mathematics and statistics
problems.
- We will cover first two sections
- Mathematics of Newton’s Method for finding solution to f(x) = 0:
- studied Bisection Method in section 5.6.
- Newton’s method usually converges to a solution faster
- Steps:
1. Start with a guess (or approximation), x0.
2. Compute new approximation from previous one using formula
f (xn)
xn+1 = xn - ───── (mathematically derived on p. 407)
f ’ (xn)
3. Continue computing new approximations until
a. | xn+1 - xn | < acceptable tolerance or
b. n = maximum number of iterations
- Show graphically (see Fig. 10.1, p. 407):
pf3
pf4

Partial preview of the text

Download Newton's Method: A Numerical Technique for Finding Roots of Functions - Prof. Bryon Ehlman and more Study notes Computer Science in PDF only on Docsity!

Notes—Chap. 10a Introduction to Numerical Methods

(pp. 405 – 413)

- Chapter presents program solutions to some mathematics and statistics

problems.

- We will cover first two sections

- Mathematics of Newton’s Method for finding solution to f(x) = 0:

- studied Bisection Method in section 5.6.

- Newton’s method usually converges to a solution faster

- Steps:

1. Start with a guess (or approximation), x 0.

2. Compute new approximation from previous one using formula

f ( xn )

xn +1 = xn - ───── (mathematically derived on p. 407)

f ’ ( xn )

3. Continue computing new approximations until

a. | xn +1 - xn | < acceptable tolerance or

b. n = maximum number of iterations

- Show graphically (see Fig. 10.1, p. 407):

  • C++ program that implements and tests Newton’s Method (Figs. 10.

and 10. 3 combined and slightly revised):

// Program that tests Function Newton. #include #include #include using namespace std; double f(double x); // Return f(x), where f is the function whose root is sought. double fPrime( double ); // Return f'(x), where f' is the derivative of f. double newton(double xGuess, double okError, int maxIterations, bool& converges); // Return approximate root of a function f given an initial estimate, // xGuess, and set converges to true if convergence is obtain within a // given tolerance, okError, and within a given maximum number of // iterations, maxIterations; otherwise, set converges to false and // return original guess. // NOTE: Uses Newton's Method and assumes function f and fPrime // have been previously declared. Pointers to functions can be passed as parameters in C++. int main() { const double MAX_ERROR = 0.00001; const int MAX_ITER = 25; made global to newton() and passed as parameter. Why? double rootGuess; // guess for root as entered by user bool rootFound; // set to true iff root was found double rootApprox; // approximate value of root cout << "Enter an initial guess for a root of f => "; cin >> rootGuess; rootApprox = newton(rootGuess, MAX_ERROR, MAX_ITER, rootFound); cout << setiosflags( ios::fixed ) << setprecision( 5 ); if (rootFound) { cout << "Starting with an initial guess of " << rootGuess << ", Newton's method " << endl << "approximates a root of f at " << rootApprox << endl; cout << "f( " << rootApprox << " ) = " << f(rootApprox) << endl; } else took this out of newton. Why? cout << "Newton's method did not converge to a root of f " << "in " << MAX_ITER << endl << "iterations using an initial" << " guess of " << rootGuess << endl; return 0;

Starting with an initial guess of 1.50000, Newton's method

approximates a root of f at 2.

f( 2.00000 ) = 0.

Enter an initial guess for a root of f =>.

Newton's method did not converge to a root of f in 25

iterations using an initial guess of 0.

Enter an initial guess for a root of f => 0

Starting with an initial guess of 0.00000, Newton's method

approximates a root of f at -1.

f( -1.00000 ) = 0.

Enter an initial guess for a root of f => -

Starting with an initial guess of -10.00000, Newton's method

approximates a root of f at -4.

f( -4.00000 ) = 0.

  • Questions?