We present a program to solve a nonlinear differential equation by the Runge-Kutta method (RK4). The differential equation is defined by a regular expression.
#include "OFELI.h" using namespace OFELI; int main(int argc, char *argv[]) { theFinalTime = 1.; theTimeStep = atof(argv[1]); |
ODESolver ode(RK4); |
y'(t) = F(t,y(t))It is important that the initial solution is to be given before defining the equation. We eventually run the time marching procedure.
ode.setInitial(0.); ode.setF("2*exp(t)-1-y"); ode.run(); |
cout << ode << endl; cout << "Solution: " << ode.get() << endl; cout << "Error: " << fabs(exp(theFinalTime)-1-ode.get()) < endl; return 0; } |