Apply trapezoidal rule to list of data points.

4 ビュー (過去 30 日間)
Mohammed
Mohammed 2023 年 2 月 11 日
編集済み: Askic V 2023 年 2 月 11 日
I have two lists of data points and need to estimate an integral using the trapezoidal rule, ideally using a for loop instead of the trapz function. Any help would be appreciated as i'm very new to matlab.
These are the two data sets:
x=[0,0.1,0.3,0.5,0.7,0.95,1.2];
y=[1,0.9048,0.7408,0.6065,0.4966,0.3867,0.3012];

回答 (1 件)

Askic V
Askic V 2023 年 2 月 11 日
編集済み: Askic V 2023 年 2 月 11 日
This is probably what you need:
x = [0,0.1,0.3,0.5,0.7,0.95,1.2];
y = [1,0.9048,0.7408,0.6065,0.4966,0.3867,0.3012];
% manually implement trapez. integration
sum = 0;
for i = 1:numel(x)-1
sum = sum + (x(i+1)-x(i))*(y(i+1)+y(i))/2;
end
sum
sum = 0.7012
% test using built in function
Z = cumtrapz(x,y);
Z(end)
ans = 0.7012
  3 件のコメント
John D'Errico
John D'Errico 2023 年 2 月 11 日
Please don't answer what are obvious questions for a student homework. This does not help the student. It does not help Answrs, since then it only convinces the student it is ok to post their homework with no effort made.
If the student has shown some effort, then you can try to help them, depending on how much effort they have shown.
Lacking any effort shown, at most you should offer some general giuidance. Push the student in the right direction.
Askic V
Askic V 2023 年 2 月 11 日
@John D'Errico you're right, I'll try not to repeat that in future. I understood he wanted loop alternative for the built in function.

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

カテゴリ

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