Calculate the area between two curves
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rhynchophorus
am 9 Feb. 2021
Kommentiert: Star Strider
am 10 Feb. 2021
I'm trying to calculate the area between two curves with different x-coordinate values.
I have tried using the trapz function found on other posts but recieve this error: "Point spacing must be a scalar specifying uniform spacing or a vector of x-coordinates for each data point" because of the different x-coordinate.
Is there an easy work around to this or a function that can handle non-uniform x-coordinates?
Data is attached.
Thanks.
0 Kommentare
Akzeptierte Antwort
Star Strider
am 9 Feb. 2021
Try this:
x1 = [0;1.55580000000001;2.85504000000000;3.82563000000000;4.82445000000000;5.79351000000000;6.79326000000000;7.80289000000001;8.82673000000000;9.85429000000001;10.8973400000000;11.9484900000000;13.0101000000000;14.0731500000000;15.1988100000000;16.3739900000000;17.6388500000000;19.0389500000000;20.6390600000000;22.4391900000000;24.0878500000000;25.6427000000000];
y1 = [0;0.401599999999998;1.07665000000000;2.11669000000000;3.11591000000000;4.15207000000000;5.15369000000000;6.14584000000000;7.12498000000000;8.08660000000000;9.04624000000000;9.99980000000000;10.9459000000000;11.8641100000000;12.7639100000000;13.5865100000000;14.3062400000000;14.8419500000000;15.3002500000000;15.4422700000000;15.1964700000000;14.7840700000000];
y2 = [0;0.384671293121388;1.57627140917836;3.16779178071786;4.85765709764627;6.55544840317967;8.24716485349601;9.92055454136918;11.5625117501394;13.1566591697723;14.6801931597464;16.0987653282826;17.3581033361660;18.3741093925497;19.0633321412604];
x2 = [0;2.00874340241544;3.67099954803351;4.95555059430456;6.10766783485649;7.24807408674708;8.39747273978297;9.57339210601188;10.7928140953118;12.0741007560704;13.4385870493858;14.9118791988194;16.5233808961725;18.2983453681205;20.2239606108829];
xv2 = linspace(min(x2), max(x2), 25);
yv2 = interp1(x2, y2, xv2);
xv1 = linspace(min(x1), max(x2), 25);
yv1 = interp1(x1, y1, xv2);
AUC1 = cumtrapz(xv1, yv1);
AUC2 = cumtrapz(xv2, yv2);
dAUC = AUC2 - AUC1;
figure
plot(x2, y2)
hold on
plot (x1, y1)
hold off
figure
plot(xv1, yv1)
hold on
plot(xv2,yv2)
plot(xv2, dAUC, 'LineWidth',2)
hold off
grid
xlabel('X (Units)')
ylabel('Y (Units)')
legend('Curve 1', 'Curve 2', 'Area Difference', 'Location','NW')
text(xv2(end), dAUC(end), sprintf('Area Between = %7.3f \\rightarrow',dAUC(end)), 'HorizontalAlignment','right', 'VerticalAlignment','middle')
The cumulative area between the curves is about 39.416.
2 Kommentare
Weitere Antworten (1)
the cyclist
am 9 Feb. 2021
Because your curves are smooth, I think using interpolated curves will be useful:
xx = 0 : 0.01 : min(max(x1),max(x2));
yy1 = interp1(x1,y1,xx);
yy2 = interp1(x2,y2,xx);
Use the yy1 and yy2 variables, along with the uniformly spaced xx variable (same for both curves) to calculate areas.
Siehe auch
Kategorien
Mehr zu Interpolation 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!