MATLAB: Is dynamic interpolation of input of function for ‘quad’ integration possible

arraydynamicfunctioninputinterpolationiterativeMATLABquadsimpsons ruletolerance

My task is to integrate a function given limited data, e.g.
max_energy = 1000;
quad(@(x) a.*x.*exp(-b.*x),0,max_energy)
where a, b are vectors, functions of energy. My problem is that it seems quad uses Simpson's rule iteratively until some tolerance is met, so the array size of a, b would need to change dynamically to match. Is it possible to interpolate between the data points I have to match quad? Must I do it in advance or use a different integration method?

Best Answer

Given that your data are irregularly spaced, you may have to use interpolation after all. If your points are (xdata,ydata), you could integrate as below:
pp = interp1(xdata,ydata,'pchip','pp');
f = @(x) ppval(pp,x);
max_energy = 1000;
quad(f,0,max_energy)
Just don't assume the integral is as accurate as the default tolerance.