0

really struggling with this problem.

I'm writing a piece of code to solve a general system of linear ODE's using the forward Euler method.

I believe my code is working fine inside the classes but I'm having a problem initializing it.

Here is the constructor for my ForwardEulerSolver class:

ForwardEulerSolver(ODEInterface& anODESystem, const Vector& initialState, 
const double initialTime, const double finalTime, const double stepSize, 
Vector (*pFunction)(double time, Vector u), const std::string outputFileName 
= "output.dat", const int saveGap = 1, const int printGap = 1);

I believe the problem is with Vector (*pFunction)(double time, Vector u). The function in question is supposed to be a function that takes a time "t", and a vector u, and returns the vector u^{dot} (ie: du/dx).

Then in my driver I define:

Vector FunctionToSolve(double time, Vector U)
{
    Vector Output(1);
    Output[0] = 1/(1 + pow(time,2));

    return Output;
}

as a separate function (I know the vector U is unused here, and the problem is 1D but I want the option to perform this with more than 1 dimension and with a more complex f). I then have:

Vector  (*pfunction)(double, Vector);
pfunction = &FunctionToSolve;

in int main().

I then try to initialize ForwardEulerClass in the following way:

ForwardEulerSolver* EulerSystem(*pODESystem, *initialVector, 0, 10, 0.01, *pfunction);

and get the following error on that line:

error: cannot convert 'Vector (*)(double, Vector)' to 'ForwardEulerSolver*' in initialization

im also getting a ton of warnings saying (all on the same line):

warning: right operand of comma operator has no effect [-Wunused-value]

warning: value computed is not used [-Wunused-value]

and

error: expression list treated as compound expression in initializer [-fpermissive]

Any help would be greatly appreciated! Thanks!

twoface
  • 51
  • 7

1 Answers1

1

First, you are trying to initialize an instance of ForwardEulerSolver and assign it to a pointer-type variable. This is syntactically incorrect. You probably wanted to do something like this:

ForwardEulerSolver eulerSystem(*pODESystem, *initialVector, 0, 10, 0.01, pfunction);

Or maybe this:

ForwardEulerSolver* eulerSystem = new ForwardEulerSolver(*pODESystem, *initialVector, 0, 10, 0.01, pfunction);

I changed the letter case of EulerSystem just wanting to make sure you meant a variable name, not some derived class name.

Also note that you do not want to derefenrece the function pointer. You actually wanted to pass the pointer, not to evaluate function on the spot. So I removed the * before pfunction.

  • Damn, thats it, builds perfectly. Silly mistake to not use " = new ForwardEulerSystem" since that's how i initiated the ODESystem and the initialVector. Now just to fix the "run" errors... haha Thanks a bunch! – twoface Jan 04 '18 at 17:20
  • @twoface • don't forget to accept Pigpag's answer as the accepted answer. – Eljay Jan 04 '18 at 17:24