Modifying a function for the trapezoidal rule to work with a data set instead of an equation?

9 Ansichten (letzte 30 Tage)
I have a formula for the Trapeziodal Rule but it only works with an equation. How would a modify it to work with a given data set?
Say my data set is:
z = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ]; %This is just an example of what a data set could be
start = 0;
stop = 3;
N = 20; %steps
trapeziodal = trapezodial(z,start,stop,N);
function I = trapezodial(Fun,a,b,N)
h =(b-a)/N;
x = a:h:b;
F =Fun(x);
I = h*(F(1)+F(N+1))/2+h*sum(F(2:N));
end

Akzeptierte Antwort

Sindar
Sindar am 25 Apr. 2020
My suggestion is to check out the built-in trapz function. There are a few formulas there that should help.
Another note: your dataset should include the x-points as well as the y (or z, as you call it). At the least, you need to know the spacing.
Assuming your dataset is convenient, it isn't that tricky. By convenient, I mean:
  • uniform spacing
  • endpoints included in dataset
  • no singularities (which you'd also like from the original function)

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by