I have written the following code for the Newton Raphson method, which takes as input two function pointers, one that points to the function f(x), the other points to its derivative f'(x). Once I dereference f(x) to a specific function, and f'(x) to its derivative (example: f(x) = x3 - 10, f'(x) = 3x^2), I can call the Newton routine on that function. So far, everything works smoothly:
Now, I want to extend my code in the following way: suppose I have a very complex expression for f(x), so that f'(x) cannot be evaluated with a simple algebraic expression as in the case above, but can be only approximated, for instance by f(x+0.001) - f(x-0.001)/0.002.
My question is: how should I modify my code in order to allow for this more general situation? I would like to be able to write at some point something like:
are you asking how to replace the function for your equation with a function pointer? There are tons of explains online on how to do this, though its generally used more in C than C++.
Also, you can just use the derivative formula once generically .. ( (f(x)- (fx2))/stuff ) .. if you get tired of coding up both the function and its dx.
another approach would be to just parse an equation format from a string.
I've turned down your tolerance (a lot). Also, you probably need to put in some check that it doesn't just go on looping endlessly: N-R isn't guaranteed to converge.
EDITED: edited to avoid multiple deriv() name clashes.
@lastchance
thank you. But are you using function pointers in your code?? Your 'deriv' is a function, a pointer to a function, a composite of two functions.... what is exactly?
As far as its effect goes, it's the function being passed as an argument. I simply find it easier to write down like this. It does exactly the same as with the (*f) notation. Feel free to put the *'s back in your own code if you prefer.
And yes, the C++ book that I learnt from told me to write them your way in the function declaration ... but then admitted it wasn't necessary.
EDITED: As you can see from the deriv() declaration in my code on line 21, it takes two arguments: a function and a value of x. This needs to be reflected in the Newton declaration on line 5. (I've edited the name here as I realised I was over-using the deriv() name.
Code compiles nicely, but when I run it, instead of giving me an approximate value for sqrt(2), all I get is 4.0... there should be something wrong, but I don't see exactly what and why. Can you spot the problem??
Code compiles nicely, but when I run it, instead of giving me an approximate value for sqrt(2), all I get is 4.0... there should be something wrong, but I don't see exactly what and why. Can you spot the problem??
1 2 3 4
double funct(double x)
{
return sqrt(x) - 2.0; //Want: approximate the square root of 2 using Newton method.
}
That's the incorrect function to be using. Likewise with your derivative.
x2 = 2
f(x) = x2 - 2
f'(x) = 2x