sunoval的个人博客分享 http://blog.sciencenet.cn/u/sunoval

博文

Curve intersection

已有 2689 次阅读 2014-5-12 05:37 |个人分类:学习笔记|系统分类:科研笔记| MATLAB, curve, intersection

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!





https://blog.sciencenet.cn/blog-813575-793550.html

上一篇:Live with it
下一篇:英语退出高考
收藏 IP: 128.123.233.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-11-18 17:15

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部