Difference between integral and cumtrapz.
Ältere Kommentare anzeigen
I have a tempral data series, which I need to integrate. In order to understand how Matlab does that, I start with a parabola as the data series. Could you guys please explain me why I get completely different results using the integral and cumtrapz funtions? The cumtrapz is giving me a cubic function, but I have no idea why it is only positive and I also do not understand the limits. My understanding is that the answer should be simple: intfx = x^3/3...
x = [-10:0.01:10]';
fx = x.^2;
% intfx = integral(@(x) fx, x(1), x(end), 'ArrayValued', true);
intfx = cumtrapz(fx,x);
plot(x,fx,'b')
hold on
plot(x,intfx,'r')
Akzeptierte Antwort
Weitere Antworten (2)
Walter Roberson
am 27 Jul. 2025
1 Stimme
integral() takes a function handle and does an adaptive quadrature numeric integration of the given function. The adaptive quadrature is exact for polynomials up to roughly degree 32767 if I read the source code correctly. For non-polynomials, the accuracy will depend on how close the approximating polynomial gets to the actual function.
cumtrapz() uses trapezoid numeric approximation, which is only accurate for polynomials up to (I think) degree 4 that are sampled often enough.
So, cumtrapz() will be less precise than integral() for all but very low degree polynomials.
The sample function you are integrating is a very low degree polynomial so the results should be the same to within numeric round-off.
Your input arguments to "cumtrapz" are reversed: you have to use
intfx = cumtrapz(x,fx);
instead of
intfx = cumtrapz(fx,x);
And the answer with "cumtrapz" as well as with "integral" will be x^3/3 - (-10)^3/3 because both start with 0 at x = -10.
2 Kommentare
Star Strider
am 27 Jul. 2025
I reversed them o be correct inmy answer. I did not mention the change.
John D'Errico
am 27 Jul. 2025
Bearbeitet: John D'Errico
am 27 Jul. 2025
This has always bothered me about trapz and cumtrapz. That is, if you call them with one argument, it is assumed to be y, and the stride in x is assumed to be 1. So we can do this:
x = 1:10;
y = x.^3 - x + 1;;
cumtrapz(y)
Now, if I add additional information such as a non-unit stride between the x values, then logically it should be with a second argument, which if not supplied, has a default value. So I would expect this to be the case, if I want to tell cumtrapz both the vectors x and y.
cumtrapz(y,x)
cumtrapz(x,y)
And of course, the first argument must be x, not f(x), when called with two arguments. So the first call I madethere failed. And if we just want to tell MATLAB the stride alone, again you need to use this form
cumtrapz(1,y)
which works perfectly.
While I know and remember this behavior of cumtrapz and trapz, these tools run counter to our expectations of how the code SHOULD have been written. Well, at least to MY expectations. And given the number of times new and even highly experiencedd users have tripped over this quirk, it is more than just me.
And, yes, I know this is something that could never be fixed, due to the amount of legacy code that relies on tools like trapz and cumtrapz. But it still frustrates me every time I need to use them, because it forces me to waste brain sweat. :)
set(0,'RantMode','off')
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







