I'm trying to implement a function that calculates the integral of a function f'(x) using the midpoint method.
Here's what I came up with:
float CMATH::midPointIntegrate(float steps,float x1, float(*F)(float x) ){
float stepSize = x1/steps;
float integral = 0.0f;
float xn = 0;
float y0 = 0.0f;
float ymid = 0.0f;
float y1 = 0.0f;
for(int I = 0; I < steps; I++){
y0 = F(xn);
ymid = F(xn+stepSize*0.5f);
y1 = F(xn+stepSize);
integral+= y0*stepSize*0.5f + (ymid - y0)*stepSize*0.25f + ymid*stepSize*0.5f + (y1-ymid)*stepSize*0.25f;
xn+=stepSize;
}
return integral;
}
It appears to work, however, I was under the impression that the midpoint method requires less steps in order to calculate the integral.
Can anyone confirm that this is the correct implementation of the midpoint method because ,at the moment, i'm not really sure.