Python 参数曲线交点的求解

Python 参数曲线交点的求解,python,numpy,sympy,Python,Numpy,Sympy,两条曲线的参数方程如下: Curve1:r(t)=(2(t-sin(t)),2(1-cos(t))) Curve2:s(t)=(2t-sin(t),2-cos(t)) 我需要找到该区域的交点[0,4π] 我能够绘制上述区域的图形,并观察到4个交点。但我无法确定确切的交点 对于非参数方程,可以使用sympy中的fsolve,但对于以参数形式给出的曲线,我无法找到解决方法 t = np.arange(-0.25*np.pi,4.25*np.pi,0.01) rx = np.zeros(len(t))

两条曲线的参数方程如下:

Curve1:r(t)=(2(t-sin(t)),2(1-cos(t)))

Curve2:s(t)=(2t-sin(t),2-cos(t))

我需要找到该区域的交点
[0,4π]

我能够绘制上述区域的图形,并观察到4个交点。但我无法确定确切的交点

对于非参数方程,可以使用
sympy
中的
fsolve
,但对于以参数形式给出的曲线,我无法找到解决方法

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)

对于给定的
x
,您可以找到每条曲线的
t
,并查看相应的
y
是否相同。您可以使用一些网格跨过x范围,查找E曲线击中的位置,并使用二分法在更精确的
x
上归零。由于无法求解
t
的参数x
x(t)-x
,因此必须使用
nsolve
来查找近似值
t
。类似这样的东西会在将
Curve1
的运算公式更正为与之后编写的代码中的值相同后,找到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
f=lambda xx:a[1]。subs(t,tt)-b[1]。subs(t,nsolve(b[0]-xx,tlast))
tlast=0#猜测给定xx的t将在我们进行时更新
tol=1e-9#对于一个解,x上的边界必须有多紧
dx=0.1
对于范围(300)内的ix:
xx=ix*dx
tt=nsolve(a[0]-xx,tlast)
y2=f(xx)
如果ix!=0和yold*y2<0和tt-tol:
xm=(x1+x2)/2
ym=f(xm)
如果ym*y1<0:
y2=ym
x2=xm
伊里夫·伊姆!=0:
y1=ym
x1=xm
其他:
打破
打印(xm)#解决方案
yold=y2

在Symphy中,我不知道有哪种更自动化的方法可以做到这一点。

两条曲线不同时相交(即sin(t)=cos(t)=0的点,没有解)。你真的想知道什么时候

R = (2*t1 - 2*sin(t1), 2 - 2*cos(t1))
S = (2*t2 - sin(t2), 2 - cos(t2))
相交

这是两个含有两个未知数的方程,因此使用
sympy.nsolve
求解非常简单。您必须稍微修改起始值,以找到收敛到不同解决方案的值。如果你从图表中大致知道它们是什么,那么这是最好的开始

>>t1,t2=符号('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])
母体([
[ 1.09182358380672],
[0.398264297579454]])
>>>nsolve([R[0]-S[0],R[1]-S[1]],[t1,t2],[5,5])
母体([
[5.19136172337286],
[5.88492100960013]])
>>>nsolve([R[0]-S[0],R[1]-S[1]],[t1,t2],[7,7])
母体([
[7.37500889098631],
[6.68144960475904]])
>>>nsolve([R[0]-S[0],R[1]-S[1]],[t1,t2],[10,10])
母体([
[11.4745470305524],
[12.1681063167797]])

您在描述中是否混淆了numpy和sympy?可能与有关,但这些曲线是一维的。这里我们有两个维度。如果你说的是代码和描述。我用颠簸来策划。但是为了找到实际的解决方案,即交叉点,我尝试使用sympy提供的fsolve,但没有成功。您共享的链接没有回复,您是说scipy提供的
fsolve
?我认为sympy没有一个
fsolve