code C++ to find function f(x)=0 "with newton method and secan method"

# C++ HW: Find function f(x)=0 with Newton method

## code C++ to find function f(x)=0 "with newton method and secan me

Page 1 of 1## 2 Replies - 7023 Views - Last Post: 12 July 2009 - 09:18 AM

##
**Replies To:** C++ HW: Find function f(x)=0 with Newton method

### #2

## Re: C++ HW: Find function f(x)=0 with Newton method

Posted 12 July 2009 - 08:30 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this:

Thanks.

Post your code like this:

Thanks.

### #3

## Re: C++ HW: Find function f(x)=0 with Newton method

Posted 12 July 2009 - 09:18 AM

Well first you need to know a little more about the Newton-Raphson method which is basically a simple iterative algorithm for finding a root of a function given a "best guess".

To use Newton's method you need to know the derivative of the function -- while for basic polynomials it is pretty easy to compute the derivative - in general this is not an easy task - I would imagine that your assignment does not mean you to program that part -- UNLESS you are taking a numerical method's class - in which case you may need to calculate the approximate value of f'(x).

so all you need to do is make write x = x - f(x)/f'(x) into a loop, initialize x with a "best guess" and then let it go, when you find that x does not change (or changes less than some degree of error) then you know that you have found a root (or an approximate value of a root).

You may also want to set a maximum number of iterations, since in instances Newton's Method will not converge.

The secant method does not require the derivative, but its convergence will be slower than Newton's method -- but here we need to have two guesses. x and ox

nx = x - f(x)*(x - ox)/(f(x)-f(ox)); ox = x; x = nx;

(here ox stood for "old x" and tx stood for "new x").

Note that the secant method is just Newton's method with a trick to approximate the derivative.

To use Newton's method you need to know the derivative of the function -- while for basic polynomials it is pretty easy to compute the derivative - in general this is not an easy task - I would imagine that your assignment does not mean you to program that part -- UNLESS you are taking a numerical method's class - in which case you may need to calculate the approximate value of f'(x).

so all you need to do is make write x = x - f(x)/f'(x) into a loop, initialize x with a "best guess" and then let it go, when you find that x does not change (or changes less than some degree of error) then you know that you have found a root (or an approximate value of a root).

You may also want to set a maximum number of iterations, since in instances Newton's Method will not converge.

The secant method does not require the derivative, but its convergence will be slower than Newton's method -- but here we need to have two guesses. x and ox

nx = x - f(x)*(x - ox)/(f(x)-f(ox)); ox = x; x = nx;

(here ox stood for "old x" and tx stood for "new x").

Note that the secant method is just Newton's method with a trick to approximate the derivative.

Page 1 of 1