views:

336

answers:

3

I need to write a class that includes a bisection and newton function. I understand how each method works, I just need to know how to write a class that passes the parameters of each function correctly.

Parameters:

  • Bisection

    • The function you're finding the root of
    • the starting interval (a,b)
    • the absolute tolerance
    • the relative tolerance
  • Newton

    • the function
    • derivative of the function
    • initial value
    • absolute tolerance
    • relative tolerance
+1  A: 

Or would you really rather have a 1D function root-finding interface that allowed you to swap implementations in and out? That way you could write both the bisection and Newton implementations of that interface and see how they compared.

I'd start with a function that had methods that returned both the value and the derivative that you could pass into the root-finding interface:

class Function
{
    public:
        virtual ~Function() {} = 0;
        virtual double value(double x) = 0;
        virtual double deriv(double x) = 0;
};

Then create another for your root finder:

class RootFinder
{
    public: 
        virtual ~RootFinder() {} = 0;
        virtual double calculateRoot(double a, double b, double xstart, double abstol, double reltol, Function f) = 0;
};
duffymo
+1  A: 

To expand on Marks's comment. We would like to help, but we're not going to do your work for you.

Some specific questions that might help you get started:

  • What (if anything) should your class be derived from? Why?
  • Do you know how you are going to represent the functions to be minimized? Class? Function pointers? Mini language? (Also, do you know how you're going to find or represent the derivative for the Newton's Method case?)
  • What data members should your class have? Why?
  • What might the constructor(s) of the class (or at least its(their) argument list(s)) look like?
  • Do you need any explicit destructors (and why or why not)?
  • What methods must you support?

...

BTW duffymo answer provides a nice framework for answering several of these questions.

dmckee
Not saying that this person is asking for homework answers, but for those who are... what do they think they're going to do when they get that programming job, and they've managed to bluff their way to a degree? It's like flying - it doesn't get easier once you get of the ground; it gets HARDER.
Scott Smith
@Steve: Some really think they're going to be able to skate indefinitely, and with the advent of the web they've been able to fool themselves for longer. I've no pity for them. Others seem to believe in some kind of magic osmotic process where, after being near able people for long enough, they will wake up one day with skillz. Those folks I'd like to help, but I'm not sure how...
dmckee
@Steve: they copy teh open-source codez and attempt to modify it to solve the business problem that they don't understand.
shoover
Heh. I copied the @Steve from the previous comment without looking. Should have been @ScottSmith.
shoover
::boggle:: Where did I get @Steve from?!?
dmckee
@dmckee: American Dad?
shoover
A: 

The ancient method is to put parameters into a structure and pass the structure to a function when there are many parameters. The definition of many is a style issue.

So for example:

typedef double (*Pointer_To_Evaluation_Function)(double x, double y, double z);
typedef double (*Pointer_To_Derivative_Function)(double x, double y);

struct Newtons_Parameters
{
    Pointer_To_Evaluation_Function p_eval_function;
    Pointer_To_Derivative_Function p_derivative_function;
    double initial_value;
    double absolute_tolerance;
    double relative_tolerance;
};

double Newtons_Method(const Newtons_Parameters& parameters);

With C++, you could declare a base function object for the evaluation function and the derivative function and replace the pointers above with pointers to the function objects.

Hope this helps.

Thomas Matthews