我希望计算matlab中具有不同x范围的两条曲线之间的区域。有两种情况。当一条曲线(图中的红色)高于另一条时,我希望该区域为正,当该曲线低于蓝色曲线时,我希望该区域为负。我意识到这通常是一个简单的问题,通常会找到两条曲线之间的所有交点,但在我的情况下,交叉点的数量可能非常大。我的困难在于,根据所讨论的数据集,两条曲线将交叉任意次数或根本不相交。
示例:
n=4
x = n*pi*rand(100,1);
x=[0;x;pi];
x=sort(x);
y=.5+exp(-.5*x).*sin(x);
x2 = n*pi*rand(1000,1);
x2=[0;x2;pi];
x2=sort(x2);
y2=.5+exp(-.5*x2).*0.6.*sin(x2/1.2);
figure,hold on
plot(x,y,'r-')
plot(x2,y2,'b-')
Area1 = trapz(x, y);
Area2 = trapz(x2, y2);
答案 0 :(得分:1)
您计算交叉口之间区域的方法是合理的。您可以使用this library来计算交叉点。 Direct Link来压缩文件。
假设你有交叉点,你可以循环使用trapz
函数来减去两个区间之间的区域。如果曲线1高于曲线2,唯一棘手的部分仍然是区域的符号。为此,您可以在epsilon
处设置阈值y1
并计算y2
和x2 - epsilon
其中(x1, x2)
是X
交点。 y1 > y2
意味着您必须Area 1 - Area2
。
综上所述,代码如下所示:
%Find all intersections
[xIntersect,yIntersect] = intersections(x,y,x2,y2,1);
%Number of intersections
numIntersections = size(xIntersect, 1);
%Threshold to calculate sign of area
epsilon = 0.1;
curveArea = 0; %Initial value
lastX1Index = 1; % End X1 Index processes in last iteration
lastX2Index = 1; % End X2 Index processes in last iteration
%Loop over intersections processing pair of points hence until
%numIntersections - 1
for i = 1:numIntersections-1
% startX = xIntersect(i); %Start of interval
% startY = yIntersect(i);
endX = xIntersect(i+1); % Next Intersection point
% endY = yIntersect(i+1);
xMinus = endX - epsilon; % get a X value before intersection
%Sign of area is positive if y1Minus > y2Minus
y1Minus = .5+exp(-.5*xMinus).*sin(xMinus); %Convert this to function1
y2Minus = .5+exp(-.5*xMinus).*0.6.*sin(xMinus/1.2); %Convert this to function 2
x1Index = find(x < xIntersect(i+1), 1, 'last' ); % Find last X1 element before intersection
x2Index = find(x2 < xIntersect(i+1), 1, 'last' ); % Find last X2 element before intersection
%Calculate area for interval
Area1 = trapz(x(lastX1Index:x1Index), y(lastX1Index:x1Index));
Area2 = trapz(x2(lastX2Index:x2Index), y2(lastX2Index:x2Index));
%Store last X1, X2 Index for next run
lastX1Index = x1Index+1;
lastX2Index = x2Index+1;
%Save curveArea
if y1Minus > y2Minus
curveArea = curveArea + (Area1 - Area2);
else
curveArea = curveArea + (Area2 - Area1);
end
end
fprintf('curveArea= %f \n',curveArea);