问题
I need to integrate the area under the curve between two known X values. The index values do not correspond to the actual x values (e.g. a data point at 3 seconds is not at position 3 in the array).
I realised this when attempting:
time=[0.1,1.5,2.1,3.2,4.5,6];
traceVect=[0,0.1,1,2,3.0,2.9];
hold on
plot(time,traceVect,'k');
t0=1;
td=5;
time = time(1,[t0:td]);
traceVect = traceVect(1,[t0:td]);
area(time,traceVect,'FaceColor','g');
a = trapz(time,traceVect);
Which produces the plot:
For clarity, what I need is:
回答1:
My solution:
time=[0.1,1.5,2.1,3.2,4.5,6];
traceVect=[0,0.1,1,2,3.0,2.9];
hold on
plot(time,traceVect,'k');
%integration interval limits
t0=1;
td=5;
%select data points within the limits
ind = (time > t0) & (time < td);
xw = time(ind);
yw = traceVect(ind);
%then complete them by interpolation values at the edges
ya = interp1(time, traceVect, t0);
yb = interp1(time, traceVect, td);
xw = [t0, xw, td];
yw = [ya, yw, yb];
trapz(xw,yw)
area(xw,yw,'FaceColor','g');
来源:https://stackoverflow.com/questions/37305609/matlab-integrate-and-shade-area-under-curve-no-function