I'm working on a function that performs some numerical methods - let's say for now, I want to write a function that performs Newton's Method and it takes three parameters:
1. double f(x) - some function
2. double df(x) - the derivative of that function
3. int initial_guess
f(x) and df(x) should be written/defined by the user
Newton's method takes these parameters and iteratively finds a root for f(x) closest to the initial guess.
Is it possible for me to define such a function Newton()? If so, how do I go about doing it? And what if I want parameter 1 to be f(x,y,z) instead of f(x) - in the case I want to perform something other than Newton's Method?
Currently, I have to write a new program each time I want to perform such a numerical method - I'm trying to find a way to generalize it.
Then you would write the "evaluation" functions like
double XSquared(double X)
{
return X*X;
};
double TwoX(double X)
{
return 2*X;
};
Tnen call the newton function like
double Root = Newton(XSquared,TwoX,10);
0
With monday.comâ€™s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
If you wanted, you could use functors instead of function pointers. These are classes that contain virtual functions and usually no data and are used in place of function pointers. Breifly, what you would do is to define an abstract base class for the functors that has a pure virtual function used to evaluate the function ("function" meaining f()). like
Then derive classes from this for particular functions (meaning f()). like
class XSquared : public FunBas
{
public:
virtual double Evaluate(double x) const
{
return x*x;
}
};
Then the newton function would take references (or pointers) to the base class functor object. (Don't bass by value or it becomes the base class, right?)
Now you probably can broaden this approach to deal with f(x,y,z) too, (In terms of C++ you definitely can, the probably part has to do with what you need to do in particular for your case.) The problem is how does newton's method handle the extra variables? (I don't remember newton's method anymore) I know that it tries a bunch of points along an axis (like x) until it narrows in on a root. but how does it handle it with 2 or 3 dimensions?
If you know how to do it for 2 or 3 dimensions, then I think you will find that the bulk of the code that goes into newton() itself will change depending on the number of dimensions. If that is the case, you will then find that you have to pass a 4th parameter to newton() This will be a function pointer or functor to the algorithm to be used by newton(). So this parameter will change depending on how many dimensions you are working with.
I don't think newton's method works for multiple dimensions, but I was wondering about multivariable functions for other numerical methods, like the runge-kutta method for differential equations.
Then you would probably need to do what I said, use another functor, an algorithm functor.
Like for example, in a 1D case the main function might call this algorithm functor with integer values from 1 to 10. This algorithm functor would then call another functor (the function (f()) functor) using the value passed to the algorithm functor.) In a 2D case, when the algorithm functor is called, it might then call the function functor a bunch of times (for different Y values) for each x falue that it is called with. Make sense?
To make this work smoothly, the algorithm functor would probably store the function functor in itself as a data member. Thus the function functor could have different numbers of parameters. (i.e. the function functors would be of different types, because they have different parameters to their evaluetion function, but that is okay because they are not passed directly to the main function, instead they are stored in the derived algorithm functor classes.)