Note that trapz allows you to provide any spacing your heart desires, because you can give it a vector of values for x and y. So why are you writing your own code here? Never write code to solve a problem when professionally written code already exists.
If you feel you need a more accurate result, could you write a Simpson's rule integral that uses a variable step? Well, yes, in theory, you could do something like that. In practice, it is just a bad idea, because there are better tools out there. What tools?
- Build a cubic interpolating spline. (A pchip interpolant might be a better choice some of the time.)
- Integrate the interpolant from step 1. That would be done most easily using fnint (as long as you have the curve fitting toolbox.)
x = [0,logspace(-1,0,5)]
0 0.1 0.17783 0.31623 0.56234 1
y = exp(x);
Can we compute the integral of this function from 0 to 1 from the data? Analytically, the integral is exp(1)-1 == 1.71828...
format long g
exp(1) - 1
Trapz is trivial to use:
Better is to use a spline integral, as I suggested.
spl = spline(x,y);
Pchip would not have been as accurate for this particular function. But for SOME problems, pchip would have been a better choice.