This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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.

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.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

details follow.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialdouble (*FunPtrTyp)(double x); // Function pointer.

double (*DerPtrTyp)(double x); // Derivative pointer.

Note the two pointer types are equavalent, so just one could be used.

Then define your newton's method function to take pointers to the user defined function, like

double Newton(FunPtrTyp FunPtr, // -> function evaluation procedure.

DerPtrTyp DerPtr, // -> derivative evlautation procedure.

double Guess) // Initial guess.

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

class FunBas

{

public:

virtual double Evaluate(double x) const = 0;

}

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

double Newton(const FunBas &Fun, // function evaluation functor.

const FunBas &Der, // derivative evlautation functor.

double Guess)

Does that help?

double (*FunPtrTyp)(double x);

double (*DerPtrTyp)(double x);

We are defining function pointers, right? What happens if we want to define function objects instead?

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.

Let me know if you have any questions.

This is helping out quite a bit..

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

C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.