MATLAB: Difference between cumsum and cumtrapz to find area under a curve

areacumsumcumtrapz

Hello
I want to find the area under the curve plotted below. I am trying to figure out why the cumsum and sumtrapz are not giving me the same answer. I read some of the help in matlab, However i am still confussed why i cant get the same answer using both the keywords.
clc
clear all
close all
x=4.5:0.1:9;
y=-2*x + 20;
plot(x,y)
Area1=cumtrapz(x,y);
Area2=cumsum(y);
figure(1)
plot(x,Area1)
figure(2)
plot(x,Area2)

Best Answer

While bearing in mind what John said, Area2 should come into much closer agreement with Area1 if cumsum is weighted by the sample spacing:
Area2=cumsum(y)*(x(2)-x(1));