Can anyone tell me whats going wrong with this program? Trying to implement the trapezoidal rule:
template <class ContainerA, class ContainerB> (!EXPECTED EXPRESSION!)
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[i] - x[i-1]) * (y[i] + y[i-1]);
}
return sum * 0.5;
Can you give the exact error message? My guess is that you have some problems in the lines above (like a missing ;) or that the error has something to do with the template arguments you use.
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[i] - x[i-1]) * (y[i] + y[i-1]);
}
return sum * 0.5;
}