i need help with a complete source code for c++ program for newton raphson method .the program should have these attributes;one double data member,class,and also have a constructor to initialize the data member as it cannot be negative. The function should return the square root of the data member. The square root should be accurate to atleast four decimal places.

What have you got so far?

Do you know the algorithm for the Newton-Raphson Method?
Have you written pseudo-code for this program?
Have you got any C++ code written?

Edited 3 Years Ago by DavidB

please nope,this project work ends in 24 hours,no one knows it in my class,i really need help with this,and a little explanation

Being the evil little Schemer that I am, I would like to mention a bit of a diversion in the form of a lecture on Netwon's Method as written in a completely different language, using methods which would be difficult (though perhaps not impossible) in C++. You can find the part on Newton's Method at minute 43.30 of the lecture video; howver, you would probably need to back up a bit in the lecture series to make sense out of it, as the description depends on the previous description of the square root method (which is a specific case of Newton's Method), and specifically on the computation of fixed points.

To make a long story short, their version of Newton's Method, written in Scheme (the language in question), comes out to be:

(define tolerance 0.0001)

(define (fixed-point f first-guess)
   (define (close-enough? v1 v2)
     (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

(define dx 0.00001)

(define (deriv g)
 (lambda (x)
    (/ (- (g (+ x dx)) (g x)) dx)))

(define (newton-transform g)
  (lambda (x)
     (- x (/ (g x) ((deriv g) x)))))

(define (newtons-method g guess)
   (fixed-point (newton-transform g) guess))

(This specific approach won't work in C++, as it relies on the ability to generate new functions programmatically. However, it, combined with the pseudo-code in the Wikipedia article linked earlier, should shed some light on how Newton's Method works.)

using methods which would be difficult (though perhaps not impossible) in C++. ....
This specific approach won't work in C++, as it relies on the ability to generate new functions programmatically.

Is that some kind of a joke? Or maybe you're taunting me?

The code you posted is trivial to translate to C++. In fact, I can make almost a literal line-by-line translation of it, here's how it turns out:

bool fp_close_enough(double v1, double v2) { 
  const double tolerance = 0.0001;
  return std::fabs(v2 - v1) < tolerance;
};

template <typename Func>
double fixed_point(Func f, double guess) {
  double next = f(guess);
  if( fp_close_enough(guess, next) )
    return next;
  return fixed_point(f, next);
};

template <typename Func>
std::function<double(double)> deriv(Func g) {
  const double dx = 0.00001;
  return [dx,g](double x) {
    return (g(x + dx) - g(x)) / dx;
  };
};

template <typename Func>
std::function<double(double)> newton_transform(Func g) {
  return [g](double x) {
    return x - (g(x) / deriv(g)(x));
  };
};

template <typename Func>
double newtons_method(Func g, double guess) {
  return fixed_point(newton_transform(g), guess);
};

With a test for it:

int main() {

  std::cout << "The square-root of 2 is " 
            << newtons_method([](double x) { 
                 return 2 - x * x; 
               }, 1.1) << std::endl;

  return 0;
};

If I couldn't have used lambda expressions, the code would just have been a bit more verbose, lambdas are mostly just syntactic sugar.

This kind of code is nothing more than just your day-to-day generic programming stuff, and an example of the use of tail-recursion to implement what normal programmers would do with a loop. Academics like to wrap all that in mysticism and paint it as some sort paradigm-shifting "New-Age" type of programming. And they resort to purist and dogmatic tools (e.g., Scheme) to elevate it even further. Don't buy the hype. It's just code.

@reverend818: If you submit the above code as a solution to your homework, you will certainly not get away with it, as this kind of code is far beyond your class' level.

And for the rest, we just cannot help you if you don't help yourself, you have to show that you are at least trying to solve the problem, not just looking to be handed out a (submittable) solution.

This article has been dead for over six months. Start a new discussion instead.