1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include "IntegrateLib.h"
#include <cmath>
#include <vector>
#include <numeric>
using namespace std;
// where function to be integrated goes
double calc(double x, double phi, double k, double A, double d){
return A*sin(k*x + phi) + d;
}
double Integration::integrate(int noIntervals, double A, double k, double phi, double d, double a, double b) {
vector<double> vecCoeffs(noIntervals+1, 2);
vecCoeffs[0] = 1;
vecCoeffs[vecCoeffs.size()-1] = 1;
vector<double> functionValues(noIntervals+1);
double intSize = (b - a)/(double)noIntervals;
for (int i=0;i<=noIntervals;i++){
double x_i = a + i*intSize;
functionValues[i] = calc(x_i, phi, k, A, d);
}
double multValue = inner_product(vecCoeffs.begin(), vecCoeffs.end(),functionValues.begin(), 0.0);
return 0.5*intSize*multValue;
}
double Integration::integrate(double A, double k, double b, double phi, double d, double a){
return ((-1*A*k*cos(k*b + phi) + d) - (-1*A*k*cos(k*a + phi) + d));
}
|