Area between a curve and a baseline

I want to create a base line (red line) from the plot and calculate area between the base line and curve (blue line).Curve is a vector. Baseline is drawn manually using code below. [x y]=ginput(2); line(x,y);
Any help/ suggestions is highly appreciated.
Thanks

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 11월 13일
편집: Andrei Bobrov 2012년 11월 13일

0 개 추천

data - your array < 2 x N >, data(1,:) - abscissa; data(2,:) - ordinate.
plot(data(1,:),data(2,:));
[x y]=ginput(2);
t = x(1) <= data(1,:) & x(2) >= data(1,:);
X = data(1,t);
out = trapz(X,diff([interp1(x,y,X);data(2,t);]))

댓글 수: 3

Teja Muppirala
Teja Muppirala 2012년 11월 13일
Ah, right. With linear interpolation and TRAPZ, I see you only really need to interpolate at the abscicca, not at a whole bunch of points like I did below.
Andrei Bobrov
Andrei Bobrov 2012년 11월 13일
corrected
Joe Abraham
Joe Abraham 2012년 11월 28일
Sorry for delayed reply. I got the following error when computed ur code.
??? Attempted to access xx(:,1); index out of bounds because size(xx)=[3,0].
Error in ==> specgraph.areaseries.refresh at 82 v = [xx(:,k) yy(:,k)];
Error in ==> graph2d.series.schema>LdoDirtyAction at 108 refresh(h);
Warning: Error occurred while evaluating listener callback. > In area at 100 In DSC at 27

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Teja Muppirala
Teja Muppirala 2012년 11월 13일

0 개 추천

You could try something along the lines of this:
% Just making some data
x0 = 0:0.001:5;
y0 = sin(x0);
plot(x0,y0);
grid on; set(gca,'layer','top');
% Get your two points
[x y]=ginput(2); line(x,y);
% Break up the line segment into a sufficiently fine grid
N = 10001;
x_fine = linspace(x(1),x(2),N);
dx = x_fine(2)-x_fine(1);
% Interpolate the values at the grid points
top = interp1(x0,y0,x_fine); % Top of the area
bottom = interp1(x,y,x_fine); % Bottom of the area
% Get the area using TRAPZ and show it
area = dx*trapz(top-bottom)
patch([x_fine fliplr(x_fine)], [top fliplr(bottom)],'r');
Just replace the sine with whatever data you are using (the example above uses row vectors, so if your data is in a column instead, you'll probably have to make some minor modifications)

댓글 수: 1

Joe Abraham
Joe Abraham 2012년 11월 28일
Hi. Thanks for the help. I get the following error when tried executing your code. Do you have any idea what went wrong?
??? Error using ==> interp1 at 259 The values of X should be distinct.
Error in ==> DSC at 28 top = interp1(T1,HbyC,x_fine); % Top of the area

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by