[英]Solving for intersection points between parametric curves
The parametric equations of the two curves are as follows:两条曲线的参数方程如下:
Curve1: r(t) = (2(t-sin(t)),2(1 -cos(t)))
Curve2: s(t) = (2t - sin(t),2 - cos(t))
I need to find the points of intersection in the region [0,4π]
.我需要找到[0,4π]
区域中的交点。
I was able to plot the graph for the mentioned region and observed 4 points of intersection.我能够 plot 上述区域的图表并观察到 4 个交点。 But I am not able to determine the exact points of intersection.但我无法确定确切的交点。
For non-parametric equations, fsolve
from sympy
can be used, but the curves which are given in their parametric forms, I am not able to find a workaround.对于非参数方程,可以使用来自sympy
的fsolve
,但是在其参数 forms 中给出的曲线,我无法找到解决方法。
t = np.arange(-0.25*np.pi,4.25*np.pi,0.01)
rx = np.zeros(len(t))
ry = np.zeros(len(t))
for i in t:
rx = 2*(t - np.sin(t))
ry = 2*(1 - np.cos(t))
sx = np.zeros(len(t))
sy = np.zeros(len(t))
for i in t:
sx = 2*t - np.sin(t)
sy = 2 - np.cos(t)
plt.plot(rx,ry)
plt.plot(sx,sy)
For a given x
you can find t
for each curve and see if the corresponding y
are the same.对于给定的x
,您可以找到每条曲线的t
并查看相应的y
是否相同。 You can step over the x range with some grid looking for such locations where the thee curves hit and use bisection to zero in on a more precise x
.您可以使用一些网格跨过 x 范围,寻找曲线撞击的位置,并在更精确的x
上使用二分法归零。 Since you can't solve the parametrix x(t) - x
for t
, nsolve
will have to be used to find an approximate t
.由于您无法求解 t 的参数x(t) - x
t
因此必须使用nsolve
来找到近似值t
。 Something like this finds values for your 4 roots (confirmed graphically) after correcting your OP equation for Curve1
to be the same as in the code you wrote afterwards.在将Curve1
的 OP 方程更正为与您之后编写的代码中的相同之后,这样的事情会找到您的 4 个根的值(以图形方式确认)。
f = lambda xx: a[1].subs(t, tt)-b[1].subs(t,nsolve(b[0]-xx,tlast))
tlast = 0 # guess for t for a given xx to be updated as we go
tol = 1e-9 # how tight the bounds on x must be for a solution
dx = 0.1
for ix in range(300):
xx = ix*dx
tt=nsolve(a[0]-xx,tlast)
y2 = f(xx)
if ix != 0 and yold*y2 < 0 and tt<4*pi:
tlast = tt # updating guess for t
# bisect for better xx now that bounding xx are found
x1 = xx-dx
x2 = xx
y1 = yold
while x2 - x1 > tol:
xm = (x1 + x2)/2
ym = f(xm)
if ym*y1 < 0:
y2 = ym
x2 = xm
elif ym != 0:
y1 = ym
x1 = xm
else:
break
print(xm) # a solution
yold = y2
I don't know of a more automated way to do this in SymPy.我不知道在 SymPy 中有更自动化的方法来做到这一点。
Probably the following would be a SymPy version:以下可能是 SymPy 版本:
from sympy import *
from sympy.geometry import Curve
t = Symbol("t", real=True)
r = Curve((2*(t-sin(t)), 2*(-cos(t))), (t, 0, 4*pi))
s = Curve((2*t - sin(t), 2 - cos(t)), (t, 0, 4*pi))
print(intersection(r, s))
But unfortunately this returns a NotImplementedError
但不幸的是,这会返回NotImplementedError
The two curves don't intersect at the same time (that would be points where sin(t) = cos(t) = 0, which has no solutions).两条曲线不会同时相交(这将是 sin(t) = cos(t) = 0 的点,没有解)。 So you really want to know when所以你真的想知道什么时候
R = (2*t1 - 2*sin(t1), 2 - 2*cos(t1))
S = (2*t2 - sin(t2), 2 - cos(t2))
intersect.相交。
This is two equations with two unknowns, so it is straightforward to solve with sympy.nsolve
.这是两个具有两个未知数的方程,因此可以直接使用sympy.nsolve
。 You have to fiddle with the starting values a bit to find the ones that converge to different solutions.您必须稍微摆弄起始值才能找到收敛到不同解决方案的值。 If you know what they are roughly from the graph that is the best place to start.如果您从图中大致知道它们是什么,那是最好的起点。
>>> t1, t2 = symbols('t1 t2')
>>> R = (2*t1 - 2*sin(t1), 2 - 2*cos(t1))
>>> S = (2*t2 - sin(t2), 2 - cos(t2))
>>> nsolve([R[0] - S[0], R[1] - S[1]], [t1, t2], [1, 1])
Matrix([
[ 1.09182358380672],
[0.398264297579454]])
>>> nsolve([R[0] - S[0], R[1] - S[1]], [t1, t2], [5, 5])
Matrix([
[5.19136172337286],
[5.88492100960013]])
>>> nsolve([R[0] - S[0], R[1] - S[1]], [t1, t2], [7, 7])
Matrix([
[7.37500889098631],
[6.68144960475904]])
>>> nsolve([R[0] - S[0], R[1] - S[1]], [t1, t2], [10, 10])
Matrix([
[11.4745470305524],
[12.1681063167797]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.