We present the same problem as the previous example but the
differential equation is given as linear one by its coefficients.
We consider the linear differential equation
c_{1}y'(t) + c_{0}y(t) = f(t)
We test the code with the data:
f(t)=2*exp(t)-1,
c_{0}=c_{1}=1, y(0)=0.
With these data we obtain the solution
y(t)=exp(t)-1.
#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.
ODESolver ode(HEUN); ode.setInitial(0.); ode.setInitialRHS(1.); TimeLoop { ode.setCoef(1,1,0,2*exp(theTime)-1); ode.runOneTimeStep(); } cout << ode << endl; cout << "Error: " << fabs(exp(theFinalTime)-1-ode.get()) << endl; return 0; } |
cout << ode << endl; cout << "Solution: " << ode.get() << endl; cout << "Error: " << fabs(exp(theFinalTime)-1-ode.get()) < endl; return 0; } |