Integrate a Discontinuous Piecewise Linear Function

10 ビュー (過去 30 日間)
Jacob
Jacob 2014 年 5 月 3 日
コメント済み: Jacob 2014 年 5 月 3 日
I have the following equation:
y={600 0<=x<15
1200 15<=x<30
2400 30<=x<45
1200 45<=x<60
600 60<=x<75
I need the cumulative integral as a function of x, both to plot the result and to perform an additional integration. I originally tried defining y as a vector and integrating with trapz:
y(1:15) =600
y(16:30)=1200
...
for j=1:length(y)
int_y=trapz(y(1:j))
end
This obviously gives the incorrect answer, because of the way I defined the intervals, but I'm pretty sure I can't integrate the discontinuities accurately with the trapezoidal rule.
I've switched to integrating each interval individually:
y(1,1:16)=600
y(2,1:16)=1200
...
I haven't worked out the details of a similar loop to integrate each row of the vector and deal with the endpoints yet, but I'm pretty sure I can get the right answer this way.
However, this is messy and awkward.
Am I missing a more straightforward way of performing this integration, either by some better way of defining my intervals or with a more suitable function?
  1 件のコメント
John D'Errico
John D'Errico 2014 年 5 月 3 日
Rectangle rule did not work?

サインインしてコメントする。

採用された回答

dpb
dpb 2014 年 5 月 3 日
編集済み: dpb 2014 年 5 月 3 日
...Am I missing a more straightforward way of performing this integration...
Since the intervals are constants, just define the breakpoints and y values...
>> y=[600 1200 2400 1200 600];
>> dx=16*ones(size(y));
>> cumsum(dx.*y)
ans =
9600 28800 67200 86400 96000
>>
  1 件のコメント
Jacob
Jacob 2014 年 5 月 3 日
I ended up defining y the same way I showed in my question (y(1:15)=600...), but cumsum did the trick!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumerical Integration and Differentiation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by