Is this a correct way to write Simpson's rule in C++?

thebolivianrock

New member
I've got this code to calculate Simpson's rule from a friend and I need to use the formula to calculate the area under a set of points that is going to be input from a text file so I don't know exactly what the points are going to be yet, Here is what I have:

template <class ContainerA, class ContainerB>
double trapezoid_integrate(const ContainerA &x, const ContainerB &y) {
if (x.size() != y.size()) {
throw std::logic_error("x and y must be the same size");
}
double sum = 0.0;
for (int i = 1; i < x.size(); i++) {
sum += (x - x[i-1]) * (y + y[i-1]);
}
return sum * 0.5;
}
 
Back
Top