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

时间:2016-05-18 16:44:04

标签: arrays matlab indexing plot area

我需要将曲线下的区域整合到两个已知的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);

产生情节:

incorrect shaded area produced by referencing index value as index

为清楚起见,我需要的是: shaded area created by referencing range values

1 个答案:

答案 0 :(得分:0)

我的解决方案:

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