|||
Very often, you may need to find the intersection pointsbetween curves, even complex ones. To illustrate your emphasis, you may need toarea some parts between the curves of the intersection part. This is onesituation occurred to me recently. I found some of the Maltab functions to be usefuland post here.
1. Example 01
Find the intersection points of two simple sinusoids andarea the intersection part as diagramed.
Here is the code:
x = 0:0.001:2*pi;
y1 = sin(x);y2 = cos(x);
%// Find point ofintersection
% idx = find(y1 -y2 < eps, 1);
idx2 =find(diff(sign(y2-y1)),2);
px = x(idx2); py =y1(idx2);
y1C =y1(idx2(1):idx2(2));
y2C =y2(idx2(1):idx2(2));
X = [x(idx2(1):idx2(2)),fliplr(x(idx2(1):idx2(2)))];
Y = [y1C, fliplr(y2C)];
figure
plot(x, y1,x, y2, px,py, 'ro', 'MarkerSize', 10)
set(gca,'xlim',[min(x)max(x)],'ylim',[-1.1 1.1]);
hold on; fill(X,Y,'r');hold off
Here, the two section points was able to be found using thesimple command of
idx2 = find(diff(sign(y2-y1)),2);
The reason is that we used the fact the relative magnitude changedon the two sides of the intersection points. This is one very simple yet usefulcode realization of this thinking.
In order the area the parts between these two points, weused the fill command, here, the way of calculating and manipulating X,Y arethe key points and need to be noticed.
2. Example 02
For more complex cases of curve intersections, however, thefind command may not give us very useful and accurate results. I found this “Fastand robust curve intersections” function to be extremely useful and easy touse.
http://www.mathworks.us/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections
Here is one illustration of finding the intersections of twocomplex curves using this function, very nice!
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-18 17:15
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社