Modifying a function for the trapezoidal rule to work with a data set instead of an equation?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sarah Peterson
am 25 Apr. 2020
Kommentiert: Rafael Hernandez-Walls
am 25 Apr. 2020
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
1 Kommentar
Rafael Hernandez-Walls
am 25 Apr. 2020
z = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ];
start = 0;
stop = 3;
N = 20; %steps
h=(stop-start)/(N-1);
I=h/2*(z(1)+z(N)+2*sum(z(2:N-1)))
Akzeptierte Antwort
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)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Numerical Integration and Differentiation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!