Difference between integral and cumtrapz.
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Aram Zeytunyan
am 27 Jul. 2025
Kommentiert: Star Strider
am 29 Jul. 2025
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')
0 Kommentare
Akzeptierte Antwort
Star Strider
am 27 Jul. 2025
They produce the same result if you let them.
Try something like this --
x = [-10:0.01:10]';
fx = @(x) x.^2;
for k = 1:numel(x)
intfx_int(k) = integral(fx, x(1), x(k), 'ArrayValued', true);
end
intfx_ctz = cumtrapz(x,fx(x));
figure
tiledlayout(2,1)
nexttile
plot(x,fx(x),'b')
hold on
plot(x,intfx_ctz,'r')
hold off
nexttile
plot(x,fx(x),'b')
hold on
plot(x,intfx_int,'r')
hold off
Your original code will produce a single value for 'intfx' (that I use as 'intfx_int') since you give it two integration limits, so it will integrate between them to produce a single result that is the integral of the function between those limits:
intfx = integral(@(x)x.^2, x(1), x(end), 'ArrayValued', true)
Here, I gave it varying limits of integration in the loop.
.
8 Kommentare
Star Strider
am 29 Jul. 2025
As always, my pleasure!
It doesn't look like a constant to me since it integrates over ν (and it multiplies as the inverse, so essentially the first integral is divided by it), however I don't understand either the physics or the variable definitions, so I'll defer to you.
Weitere Antworten (2)
Walter Roberson
am 27 Jul. 2025
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.
0 Kommentare
Torsten
am 27 Jul. 2025
Bearbeitet: Torsten
am 27 Jul. 2025
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')
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







