Apply trapezoidal rule to list of data points.

8 visualizaciones (últimos 30 días)
Mohammed
Mohammed el 11 de Feb. de 2023
Editada: Askic V el 11 de Feb. de 2023
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];

Respuestas (1)

Askic V
Askic V el 11 de Feb. de 2023
Editada: Askic V el 11 de Feb. de 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
sum = 0.7012
% test using built in function
Z = cumtrapz(x,y);
Z(end)
ans = 0.7012
  3 comentarios
John D'Errico
John D'Errico el 11 de Feb. de 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
Askic V el 11 de Feb. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Numerical Integration and Differentiation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by