フィルターのクリア

Difference between cumsum and cumtrapz to find area under a curve?

150 ビュー (過去 30 日間)
friet
friet 2017 年 8 月 31 日
編集済み: Chad Greene 2019 年 11 月 19 日
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)

採用された回答

Matt J
Matt J 2017 年 8 月 31 日
編集済み: Matt J 2017 年 8 月 31 日
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));

その他の回答 (1 件)

John D'Errico
John D'Errico 2017 年 8 月 31 日
編集済み: Chad Greene 2019 年 11 月 19 日
Why should they be the same? They do different things, solving different problems.
Is a sum the same thing as an integral? Of course not. At least, not in general.
cumsum computes the cumulative sum of elements in a vector or array.
cumtrapz computes the integral of a function that we assume is represented by the values in that vector (or array). It uses a cumulative trapezoidal rule, ergo cumtrapz. Thus each element of the result is the integral from zero to that point in the vector.
A cumulative sum CAN be just another approximation to an integral though. Rectangle rule is an an approximation to an integral, if you multiply the height of each rect by the width, then you compute an area. So as the rectangle rule would see it, if the width of each rectangle is 1, then cumsum would be a cumulative rectangle rule.
So, should the two give the SAME result? Even so, OF COURSE NOT!!!!!!
A rectangle rule is NOT the same approximation to an integration as a trapezoidal rule. Both are just approximation to an integral, but they are DIFFERENT approximations.

カテゴリ

Help Center および File ExchangeState-Space Control Design and Estimation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by