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

允我心安 提交于 2019-12-13 21:57:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!