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!

C++ programming, Newtons Method.

  1. Feb 9, 2012 #1
    Having a lot of trouble the my C++ course, loops especially. This is the assignment giving me trouble

    The function you are to implement finds a root of the given function eff(x) using Newton's method. The given version of eff(x) implements f(x) = x2e-x-2 (where e is the base of the natual logarithm) and effPrime(x) implements its derrivative, but your code could be used to find a root of other functions by substituting different implementations of eff and effPrime. Your program should use both of the above techniques to stop iteration (i.e., it should stop when either condition is satisfied).

    The function you are to implement is as follows:

    double newton(double x, double tol, int maxIt)
    Finds a root of eff using Newton's method starting at x and stoping when successive approximations are within tol of each other or maxIt iterations have occured.

    These files were given;
    eff(double x)
    return x*x * exp(-x) - 2;

    effPrime(double x)
    return 2.0 * x * exp(-x) - x * x * exp(-x);

    This is all I have so far, not even sure how to begin

    #include "assign4.h"
    #include <cmath>

    double newton(double x, double tol, int maxIt); {
    return x_1 = x_o - (f(x_o) / f(x_o));

  2. jcsd
  3. Feb 9, 2012 #2
    You did not use the return statement properly, instead you would write

    Code (Text):
    return x_o - (f(x_o) / f(x_o));
    In other words, you shouldn't be returning x1 = something
    just return something

    I do not see the variable "x_o" defined.

    Even if it was defined, this is not the correct equation for Newton's method.
  4. Feb 9, 2012 #3


    Staff: Mentor

    First step, can you describe newton's method of successive approximation?

    You can't write a program if you don't know the algorithm.
  5. Feb 9, 2012 #4


    Staff: Mentor

    Actually, you can do it this way. In C and C++, the value of an assignment expression is the value that is stored in the variable on the left side of the assignment. I wouldn't do it this way, but it's not incorrect.

  6. Feb 9, 2012 #5
    Yes, but the OP shouldn't do it this way. The only way it would make any sense would be if one needed to set a (non-local) variable to the return value of a function every time the function was called.
  7. Feb 9, 2012 #6


    Staff: Mentor

    I agree.
  8. Feb 9, 2012 #7
    I think this is what they suggested I use in the assignment "y = x - f(x)/f'(x),"
  9. Feb 9, 2012 #8


    Staff: Mentor

    This won't work. The compiler will complain about the symbol ' after f. Your header file defines two functions. Use them.
  10. Feb 9, 2012 #9


    Staff: Mentor

    Try solving the problem with pencil and paper and maybe you'll see how to program it.
    Do one step at a time like a computer on a separate line.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: C++ programming, Newtons Method.
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)