2016-05-18 67 views
0

2つの既知のX値の間の曲線の下の面積を積分する必要があります。インデックス値は実際のx値に対応していません(たとえば、3秒のデータポイントは配列の位置3にありません)。MATLAB:曲線下の面積を積分して陰影付けする(関数なし)

しようとしたときに私はこれを実現:プロット生成

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

答えて

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'); 
関連する問題