显示两条曲线的交点 [英] Show the intersection of two curves
问题描述
如果我有两个由两个不同方程定义的图:
x = 0:0.01:30;
y1 = x。^ 2 + 2;
y2 = x。^ 3;
我将它们绘制为
plot(x,y1,x,y2);
如何以编程方式在交叉点周围形成一个小环(如下图所示)?
您必须找到交点(p x ,p y ):
idx = find(y1 - y2 px = x(idx);
py = y1(idx);请记住,我们正在比较浮点数表示法中的两个数字,所以不是
>
> > y1 == y2
我们必须设置一个容差。我选择它作为 eps
,但这取决于您决定。
要围绕此画一个圆点,你可以计算它的点,然后绘制它们,但更好的方法是用一个圆圈标记来绘制一个点(归功于乔纳斯对此建议):
$ b $ pre $ plot(px,py,'ro','MarkerSize',18)
通过这种方式,圆的尺寸不受坐标轴和纵横比的影响。
示例
x = 0:0.01:30;
y1 = x。^ 2 + 2;
y2 = x。^ 3;
%//找到交点
idx = find(y1 - y2 px = x(idx);
py = y1(idx);
figure
plot(x,y1,x,y2,px,py,'ro','MarkerSize',18)
axis([0 10 0 10])
这应该会产生以下图表:
If I have two plots defined by two different equations:
x = 0:0.01:30;
y1 = x .^2 + 2;
y2 = x .^3 ;
and I plot them as
plot(x, y1, x, y2);
How do I get a small ring around the point of intersection programatically (as in the following plot)?
You'll have to find the point of intersection (px, py) manually:
idx = find(y1 - y2 < eps, 1); %// Index of coordinate in array
px = x(idx);
py = y1(idx);
Remember that we're comparing two numbers in floating point representation, so instead of y1 == y2
we must set a tolerance. I've chosen it as eps
, but it's up to you to decide.
To draw a circle around this point, you can compute its points and then plot them, but a better approach would be to plot one point with a blown-up circle marker (credit to Jonas for this suggestion):
plot(px, py, 'ro', 'MarkerSize', 18)
This way the dimensions of the circle are not affected by the axes and the aspect ratio of the plot.
Example
x = 0:0.01:30;
y1 = x .^ 2 + 2;
y2 = x .^ 3;
%// Find point of intersection
idx = find(y1 - y2 < eps, 1);
px = x(idx);
py = y1(idx);
figure
plot(x, y1, x, y2, px, py, 'ro', 'MarkerSize', 18)
axis([0 10 0 10])
This should produce the following plot:
这篇关于显示两条曲线的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!