Have a question regarding Cumtrapz
Show older comments
Hello,
I am not sure what cumtrapz does. So I have a plot. 

There are 3 plots. First is power which is Kilo Watts. Second and Third are energy. But Energy calculated just as kilowatt into hour and second cumtrapz(power) * time which is hour. I was expecting both of those plots to be same. Which means I dont understand cumtrapz correctly. What does cumtrapz do? Thank you.
Accepted Answer
More Answers (1)
cumtrapz() is used to approximate the cumulative trapezoidal numerical integration.
For example,
.
.x = linspace(-2, 2, 401);
y = cumtrapz(x(201:401), 2*x(201:401));
hold on
plot(x, x.^2, '--') % blue dashed curve
plot(x(201:401), y), grid on % red solid curve
legend('x^{2}', 'cumtrapz', 'location', 'north')
hold off
2 Comments
Here is a minor update. I believe this is the method you intended to implement in the 3rd plot.

x = linspace(-2, 2, 401);
A = zeros(1, 201); % initialization
dt = x(2) - x(1); % step length
for i = 202:401
% y(i-200) = trapz(x(201:i), 2*x(201:i));
% y(i-200) = trapz(2*x(201:i))*dt;
A(i-200) = trapz(2*x(i-1:i));
end
hold on
plot(x, x.^2, '--') % blue dashed curve
plot(x(201:401), cumsum(A)*dt), grid on % red solid curve
legend('x^{2}', 'cumsum(A)·dt', 'location', 'north')
hold off
xlabel('x')
ylabel('y')
Govind Sankar Madhavan Pillai Ramachandran Nair
on 19 Sep 2025
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




