1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Newton's method in c++

  1. Sep 18, 2012 #1
    1. The problem statement, all variables and given/known data

    why the loop not looping ?

    2. Relevant equations


    3. The attempt at a solution

    Code (Text):

    #include "stdafx.h"
    using namespace std;
    float fun1(float);
    float fun2(float);
    void main()
    float a;

    if (fun2(a)>0)



    float fun1( float a)
        float y;
        return y;
    float fun2( float b)
        float y;
        return y;

  2. jcsd
  3. Sep 18, 2012 #2


    User Avatar

    Staff: Mentor

    Try to trace your program watching values of variable a and values returned by fun1 and fun2. Is it doing what you hoped for, or is it doing something else?
  4. Sep 18, 2012 #3
    Code (Text):
    Are you sure about this?
  5. Sep 18, 2012 #4
    Keep two variables for iterations.
    If the difference between them is < 0.001 then stop loop.

    Something like this :

    x1 = a - (fun1(a) / fun2(a));

    x0 = x1;
    x1 = x0 - (fun1(x0) / fun2(x0));
    d = x0 - x1;
    }while(d > 0.001);
  6. Sep 19, 2012 #5
    Also, check your (math in) function FUN2.
  7. Sep 19, 2012 #6


    Staff: Mentor

    I don't see anything wrong with it.
  8. Sep 19, 2012 #7
    You're CORRECT - I misread OP's function F(x)! fun2 code is correct for fun1 code.
  9. Sep 19, 2012 #8


    Staff: Mentor


    Maybe you're referring to this line in the OP, in the Relevant equations section:
    There's a missing x. 4^2 should be 4*x^2.
    The code has the right formula, though.
  10. Oct 7, 2012 #9
    thank u all
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook