[英]Solving for intersection points between parametric curves
兩條曲線的參數方程如下:
Curve1: r(t) = (2(t-sin(t)),2(1 -cos(t)))
Curve2: s(t) = (2t - sin(t),2 - cos(t))
我需要找到[0,4π]
區域中的交點。
我能夠 plot 上述區域的圖表並觀察到 4 個交點。 但我無法確定確切的交點。
對於非參數方程,可以使用來自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)
對於給定的x
,您可以找到每條曲線的t
並查看相應的y
是否相同。 您可以使用一些網格跨過 x 范圍,尋找曲線撞擊的位置,並在更精確的x
上使用二分法歸零。 由於您無法求解 t 的參數x(t) - x
t
因此必須使用nsolve
來找到近似值t
。 在將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
我不知道在 SymPy 中有更自動化的方法來做到這一點。
以下可能是 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))
但不幸的是,這會返回NotImplementedError
兩條曲線不會同時相交(這將是 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 = 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.