MATLAB:积分和遮蔽曲线下的区域(无功能)

MATLAB: integrate and shade area under curve (no function)

我需要对两个已知 X 值之间的曲线下面积进行积分。索引值与实际的 x 值不对应(例如,3 秒的数据点不在数组中的位置 3)。

我在尝试时意识到了这一点:

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);

产生情节:

为清楚起见,我需要的是:

我的解决方案:

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');