Apply trapezoidal rule to list of data points.
5 views (last 30 days)
Show older comments
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];
0 Comments
Answers (1)
Askic V
on 11 Feb 2023
Edited: Askic V
on 11 Feb 2023
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
% test using built in function
Z = cumtrapz(x,y);
Z(end)
3 Comments
John D'Errico
on 11 Feb 2023
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
on 11 Feb 2023
@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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!