Page MenuHomec4science

IntegralSolver1D.cpp
No OneTemporary

File Metadata

Created
Wed, May 22, 16:38

IntegralSolver1D.cpp

#include "IntegralSolver1D.hpp"
#include "Function.hpp"
//constructor, deconstructor
IntegralSolver1D::IntegralSolver1D(Function function_, Domain domain, Method method) {
a = domain.boundaries[0][0];
b = domain.boundaries[0][1];
n = (double)domain.subdivision1D;
type = method.name;
function = function_;
if (type == "Midpoint") {
result = this->IntegrationMidpoint();
}
if (type == "Trapz") {
result = this->IntegrationTrapeze();
}
if (type == "Simpson") {
result = this->IntegrationSimpson();
}
}
IntegralSolver1D::~IntegralSolver1D() {}
//Midpoint method:
double IntegralSolver1D::IntegrationMidpoint() {
double result = 0;
double size = (b-a)/n;
for (int i = 0; i < n; i++){
vector<double> inputVector;
inputVector.assign(1,size/2+i*size);
result += size*function.GetResult(inputVector);
}
return result;
}
//Trapeze method:
double IntegralSolver1D::IntegrationTrapeze() {
double result = 0;
double size = (b-a)/n;
for (int i = 0; i < n; i++){
vector<double> arg1;
arg1.assign(1, size*i);
vector<double> arg2;
arg2.assign(1, size*(i+1));
result += size*( function.GetResult(arg1) + function.GetResult(arg2) )/2;
}
return result;
}
//Simpson Method:
double IntegralSolver1D::IntegrationSimpson() {
double result = 0;
double size = (b-a)/n;
for (int i = 0; i < n; i++){
vector<double> arg1;
vector<double> arg2;
vector<double> arg3;
arg1.assign(1, size*i);
arg2.assign(1, size/2+i*size);
arg3.assign(1, size*(i+1));
result += size/6 * (function.GetResult(arg1)
+ 4*function.GetResult(arg2)
+ function.GetResult(arg3) );
}
return result;
}
// Get result method
double IntegralSolver1D::GetResult() {
return result;
}

Event Timeline