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

Page 1 of 1

2 Replies - 6224 Views - Last Post: 12 July 2009 - 09:18 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=114337&amp;s=9faf35b3fd904969cd7faa687db0a9c3&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 andriedit

Reputation: 0
• 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

• using System.Linq;

Reputation: 781
• Posts: 5,101
• 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.

Thanks.

#3 NickDMax

Reputation: 2254
• 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.