0

Not Yet Answered # programming of newton rapsons root method

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.

DavidB 44 Discussion Starter reverend818 Schol-R-LEA 1,003 mike_2000_17 2,669 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

0

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*

0

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

0

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.)

0

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.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```