On 12 Dec 2004 16:52:00 GMT,
(Honestmath) wrote:
>I'm using a root finding algorithm in a function that only takes as arguments a
>function pointer and two variables that represent guesses at the roots of the
>function.
>
>int zbrac(float (*func)(float), float *x1, float *x2)
>
>Problem is, I'm not sure how to pass parameters to the root finding function.
I had the analogous problem with the simplex function minimization
method, and I found that the following sort of object-based solution
worked perfectly. (Pardon my fractured syntax--I'm really a
Pascal programmer at heart.)
First, define an abstract "RootFinder" class something like this:
class RootFinder {
int zbrac(float x1, float x2);
virtual float myfunc;
}
int RootFinder.zbrac(float x1, float x2)
{
the actual zbrac code goes here.
zbrac calls myfunc as necessary
}
float RootFinder.myfunc{}; // This is just a dummy
Now, you can descend any desired specific type of
rootfinder from this abstract class, for example:
class PolyRootFinder : RootFinder {
int Degree;
float *Coefficients;
virtual float myfunc;
}
zbrac need not be redefined--it is inherited from
the abstract class. Instead, you just define
the exact function that you want for this
particular rootfinder:
float PolyRootFinder.myfunc{
// Computations relevant to polynomial go here
// Note that this function has access to Degree
// and Coefficients as part of the class, so you
// don't need to pass these values "through" zbrac.
// That's what I really like about object-oriented
// numerical programming.
}
Similarly, you descend another new class for any type of function with
which you want to use zbrac.
HTH,