class, simple ODE with EE method

This is a simple code which illustrated the use of euler explicit method for solving an ordinary differential equation.

Main


//============================================================================
// Name        : ode.cpp
// Author      : chd
// Version     :
// Copyright   : Your copyright notice
// Description : Simple ordinary differential
//               equation class.Euler explicit method, C++ Ansi-style
//============================================================================

#include <iostream>
#include "ode.h"
#include "srcFunction.h"
using namespace std;

int main() {
	ode odef(0,2,3,f);
	double* sol=odef.euler(100);
	for (int i = 0; i < 100; ++i) {
		cout<<"|"<<sol[i]<<"|";
	}
}

ode.h


/*
 * ode.h
 *
 *  Created on: Mar 14, 2016
 *      Author: christos
 */

#ifndef ODE_H_
#define ODE_H_

class ode{

private:

	double tstart;
	double tend;
	double x0;
	double (*sfn)(double t, double x);

public:
	ode(double t0,double tn,double xinit, double (*f)(double t,double x)){

		tstart=t0;
		tend=tn;
		x0=xinit;
		sfn=f;

	}


	double* euler(int n) const;

};




#endif /* ODE_H_ */

ode.cpp


/*
 * ode.cpp
 *
 *  Created on: Mar 14, 2016
 *      Author: christos
 */
#include "ode.h"

double* ode::euler(int n) const{

	double* x=new double[n+1];
	double h=(tend-tstart)/n;
	x[0]=x0;
	for (int i = 0; i < n; ++i) {
		x[i+1]=x[i]+ h*sfn(tstart+h*i,x[i]);
	}
	return x;
}






srcFunction.h

/*
 * srcFunction.h
 *
 *  Created on: Mar 14, 2016
 *      Author: christos
 */

#include <math.h>

#ifndef SRCFUNCTION_H_
#define SRCFUNCTION_H_

double f(double t, double x);

#endif /* SRCFUNCTION_H_ */








srcFunction.cpp


/*
 * srcFunction.cpp
 *
 *  Created on: Mar 14, 2016
 *      Author: christos
 */

#include "srcFunction.h"


double f(double t, double x){


	return x*(1-exp(t))/(1+exp(t));


}