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 - 3715 Views - Last Post: 12 July 2009 - 09:18 AM Rate Topic: -----

#1 andriedit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-July 09

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

Posted 12 July 2009 - 08:22 AM

code C++ to find function f(x)=0 "with newton method and secan method"
Is This A Good Question/Topic? 0
  • +

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

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

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: :code:

Thanks.
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1