从绘制的图获取x,y坐标并获取与其他图的交点 [英] getting x,y coordinates from a plotted graph and get the intersection with an other graph
问题描述
我是matlab的新手,所以这个问题可能很简单.我尝试在Matlab中获取绘制图上点的[x,y]
坐标.
I'm some kind of new to matlab,so the question may be elementary. I try to get the [x,y]
coordinates of the points on a plotted graph in matlab.
该图很简单.我已经有了某些点(xb,yb)
的坐标,并使用line(xb, yb)
绘制了图形.
The graph is simple. I already had the coordinates of some points (xb,yb)
, and plotted the graph using line(xb, yb)
.
现在,我需要找到绘制线上所有点的坐标,以便获得这些线与另一个已定义图形的交点.
Now I need to find the coordinates of all the points on the plotted lines in order to get the intersection points of these lines with another defined graph.
我使用的代码是:
line(Xb,Yb)
hold on
X=min(Xb):.001:max(Xb);
y=0.03;
plot(X,y);
Xb
和Yb
是1 * 38数组,它们在我正在研究的实际问题中塑造区域的边界.但是我没有边界上所有的x
和Y
点.我需要用plot(X,y)
绘制的水平线与边界的交点坐标.
The Xb
and Yb
are 1*38 arrays, and shape the boundary of a region in real problem which I'm studying. But I don't have all the x
, Y
of the points on the boundary. I need to the intersection coordinates of the boundary with horizontal line which is plotted by plot(X,y)
.
Xb
和Yb
是:
Xb = [-0.0400 -0.0550 -0.0700 -0.0850 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.1000 -0.0800 -0.0600 -0.0400 -0.0200 0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.0850 0.0700 0.0550 0.0400 0.0380 0.0324 0.0235 0.0124 0 -0.0124 -0.0235 -0.0324 -0.0380 -0.0400 ];'
Yb = [0 0 0 0 0 0.0200 0.0400 0.0600 0.0800 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.1000 0.0800 0.0600 0.0400 0.0200 0 0 0 0 0 0.0124 0.0235 0.0324 0.0380 0.0400 0.0380 0.0324 0.0235 0.0124 0];'
非常感谢您的帮助.
推荐答案
如果将函数指定为数组x1,y1
和x2,y2
,则可以使用它们的线性内插器找到它们的交点:
If your functions are given as arrays x1,y1
and x2,y2
, then you can find their crossing using their linear interpolators:
%dummy input
x1=[0 1 2 3];
y1=[1 4 2 0];
x2=[-1 3 4 5];
y2=[-1 2 5 3];
x0 = (max(min(x1),min(x2))+min(max(x1),max(x2)))/2;
fun1 = @(x) interp1(x1,y1,x,'linear');
fun2 = @(x) interp1(x2,y2,x,'linear');
difffun = @(x) fun1(x)-fun2(x);
crossing = fzero(difffun,x0);
您的x0
应该非常接近实际的交叉点,我选择了两个区间内的一个点.匿名函数fun1
和fun2
使用interp1
创建一个可调用函数,该函数只能告诉您特定x
坐标处的插值.如果具有曲线拟合工具箱",则可以使用fit
一步获得一个函数.那么difffun
是由两个输入函数之差给出的函数,其零将为您提供两个函数的交点. fzero
会做到这一点:从给定的起点找到一个零的函数.
where your x0
should be fairly close to the actual crossing point, I chose a point inside both of your intervals. The anonymous functions fun1
and fun2
create a callable function using interp1
, which can only tell you the interpolant at specific x
coordinates. If you have the Curve Fitting Toolbox, you can get a function in a single step using fit
. Then difffun
is a function given by the difference of the two input functions, the zero(s) of which will give you the crossing of the two functions. fzero
will do just that: find a zero of a function from a given starting point.
如果起始x0
离得很远,您可能会遇到麻烦;如果有多个零,则一次只能找到一个.在您的情况下,插值函数的行为不太好,因此选择正确的x0
(可能是手动设置值)是最重要的.
You can run into troubles if the starting x0
is way off, and in case of multiple zeros you will only find one at a time. In your case, the interpolating functions are not too well-behaved, so choosing a right x0
(probably manually setting a value) is paramount.
使用上面的虚拟输入的结果:
Results with the dummy input above:
输出:
crossing =
2.272727272727273
这篇关于从绘制的图获取x,y坐标并获取与其他图的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!