x = -2:.01:2;
intg = trapz(x,myfun(x));
Jan beat me to it! But CUMTRAPZ is the cumulative integral on your data, not a function. So it is like doing this (explicitly with a loop):
x = 0:.1:2;
cum_int = zeros(size(x));
cnt = 1;
for ii = 0:.1:2
cum_int(cnt) = quadl(@sin,0,ii);
cnt = cnt+1;
end
cum_int2 = cumtrapz(x,sin(x));
plot(x,cum_int,'b',x,cum_int2,'r')
%
%
%
EDIT In response to your further questions.
You need to think a little bit about what you are doing! You were given examples of what TRAPZ does. TRAPS doesn't work on functions, but data... Look again at the examples. You get different numbers because you are doing different things. When using quad you are integrating from:
>> b = 0.000000000258;LH = -250*b
LH =
-6.45e-008
LH to abs(LH). When using TRAPZ you are integrating from -2 to 2 (as you showed in your first question) with a much larger spacing than used from LH to abs(LH). Remember TRAPZ operates on data (look again at the examples given to you), not functions. QUAD operates on functions so it can evaluate a function on arbitrary (well, not quite, but for all intents and purposes...) spacing while determining the integral - TRAPZ is stuck with the spacing in the data. Thus if the spacing in the data misses some important features of the underlying function, TRAPZ won't see it.
Since you have a function, you should stick with QUAD or QUADL.
Best Answer