2017-02-16 211 views

回答

1

可以參數化在一些變量t表示橢圓。例如,您可以查看Wikipedia以瞭解如何完成此操作。

在下面的代碼中,我根據您提供的參數導出了參數形式所需的參數。

# Example focii and sum-distance 
a1 = 1 
b1 = 2 
a2 = 5 
b2 = 7 
c = 9 

# Compute ellipse parameters 
a = c/2        # Semimajor axis 
x0 = (a1 + a2)/2      # Center x-value 
y0 = (b1 + b2)/2      # Center y-value 
f = np.sqrt((a1 - x0)**2 + (b1 - y0)**2) # Distance from center to focus 
b = np.sqrt(a**2 - f**2)     # Semiminor axis 
phi = np.arctan2((b2 - b1), (a2 - a1)) # Angle betw major axis and x-axis 

# Parametric plot in t 
resolution = 1000 
t = np.linspace(0, 2*np.pi, resolution) 
x = x0 + a * np.cos(t) * np.cos(phi) - b * np.sin(t) * np.sin(phi) 
y = y0 + a * np.cos(t) * np.sin(phi) + b * np.sin(t) * np.cos(phi) 

# Plot ellipse 
plt.plot(x, y) 

# Show focii 
plt.plot(a1, b1, 'bo') 
plt.plot(a2, b2, 'bo') 

plt.axis('equal') 
plt.show() 

這給了你所需要的:

Ellipse plot

+0

作爲一名物理學家,如果對這個問題有任何理論上的簡化,我就這樣做。這就是爲什麼我喜歡這個答案。我想知道你給我看的這段代碼是否已經在某個模塊中實現了。它似乎不是。謝謝你的時間。 –

1

需要2所列出或X陣列,Y,使得元件滿足橢圓方程

通常的橢圓繪製方案參數由中央(或焦點)角的橢圓方程以使X,Y的功能單值在0角度2PI

我顯示在Drawing elliptical orbit in Python (using numpy, matplotlib) Y中一個黑客「感覺出」的x範圍,則片的溶液作爲X的函數一起雙重ÿ解決方案對於每個X

只是將代碼放入等式中的最小修改數將會失敗爲A1 = A2

符號溶液花費一分鐘左右運行時

import numpy as np 
import matplotlib.pyplot as plt 
from sympy import * 

# sqrt((x-a1)**2 + (y-b1)**2) + np.sqrt((x-a2)**2 + (y-b2)**2) = c 
coeffs = [1, 0, -1, 0, 4] 
xs = [coeffs[0], coeffs[2]] 

def ysolv(coeffs): 
    x,y,a1,b1,a2,b2,c = symbols('x y a1 b1 a2 b2 c', real = True) 
    ellipse = sqrt((x-a1)**2 + (y-b1)**2) + sqrt((x-a2)**2 + (y-b2)**2) - c 
    y_sols = solve(ellipse, y) 
    print(*y_sols, sep='\n') 

    num_coefs = [(a, f) for a, f in (zip([a1,b1,a2,b2,c], coeffs))] 
    y_solsf0 = y_sols[0].subs(num_coefs) 
    y_solsf1 = y_sols[1].subs(num_coefs) 
    print(y_solsf0, '\n', y_solsf1) 

    f0 = lambdify([x], y_solsf0) 
    f1 = lambdify([x], y_solsf1) 
    return f0, f1 

f0, f1 = ysolv(coeffs) 

y0 = [f0(x) for x in xs] 
y1 = [f1(x) for x in xs] 

def feeloutXrange(f, midx, endx): 
    fxs = [] 
    x = midx 
    while True: 
     try: f(x) 
     except: 
      break 
     fxs.append(x) 
     x += (endx - midx)/200 
    return fxs 

midx = (min(xs) + max(xs))/2  

xpos = feeloutXrange(f0, midx, max(xs)) 
xnegs = feeloutXrange(f0, midx, min(xs)) 
xs_ellipse = xnegs[::-1] + xpos[1:] 

y0s = [f0(x) for x in xs_ellipse] 
y1s = [f1(x) for x in xs_ellipse] 

ys_ellipse = y0s + y1s[::-1] + [y0s[0]] # add y start point to end to close drawing 

xs_ellipse = xs_ellipse + xs_ellipse[::-1] + [xs_ellipse[0]] # added x start point 

plt.plot(xs_ellipse, ys_ellipse) 
plt.show() 

(-c*sqrt((a1**2 - 2*a1*a2 + a2**2 + b1**2 - 2*b1*b2 + b2**2 - c**2)*(a1**2 + 2*a1*a2 - 4*a1*x + a2**2 - 4*a2*x + b1**2 - 2*b1*b2 + b2**2 - c**2 + 4*x**2))*(-b1 + b2 + c)*(b1 - b2 + c) + (b1**2 - 2*b1*b2 + b2**2 - c**2)*(-a1**2*b1 + a1**2*b2 + 2*a1*b1*x - 2*a1*b2*x + a2**2*b1 - a2**2*b2 - 2*a2*b1*x + 2*a2*b2*x - b1**3 + b1**2*b2 + b1*b2**2 + b1*c**2 - b2**3 + b2*c**2))/(2*(-b1 + b2 + c)*(b1 - b2 + c)*(b1**2 - 2*b1*b2 + b2**2 - c**2)) 
(c*sqrt((a1**2 - 2*a1*a2 + a2**2 + b1**2 - 2*b1*b2 + b2**2 - c**2)*(a1**2 + 2*a1*a2 - 4*a1*x + a2**2 - 4*a2*x + b1**2 - 2*b1*b2 + b2**2 - c**2 + 4*x**2))*(-b1 + b2 + c)*(b1 - b2 + c) + (b1**2 - 2*b1*b2 + b2**2 - c**2)*(-a1**2*b1 + a1**2*b2 + 2*a1*b1*x - 2*a1*b2*x + a2**2*b1 - a2**2*b2 - 2*a2*b1*x + 2*a2*b2*x - b1**3 + b1**2*b2 + b1*b2**2 + b1*c**2 - b2**3 + b2*c**2))/(2*(-b1 + b2 + c)*(b1 - b2 + c)*(b1**2 - 2*b1*b2 + b2**2 - c**2)) 
sqrt(-48*x**2 + 192)/8 
-sqrt(-48*x**2 + 192)/8 

enter image description here

其他的答案所使用的參量的變換方法

我特別喜歡錶示sympy求解所述方程你而不是有人解決它

符號表達式只需要找到一次f或特定的橢圓參數化,則該符號表達式可以簡單地硬編碼:

""" 
for Ellipse equation: 
sqrt((x-a1)**2 + (y-b1)**2) + sqrt((x-a2)**2 + (y-b2)**2) = c 

sympy solution to Ellipse equation, only have to run once to get y_sols 
symbolic expression to paste into ysolv below 

#def symEllipse(): 
# x,y,a1,b1,a2,b2,c = symbols('x y a1 b1 a2 b2 c', real = True) 
# ellipse = sqrt((x-a1)**2 + (y-b1)**2) + sqrt((x-a2)**2 + (y-b2)**2) - c 
# y_sols = solve(ellipse, y) 
# print(*y_sols, sep='\n') 

""" 

coeffs = [1, 1, -1, -1, 3] 
xs = [coeffs[0], coeffs[2]] 

def ysolv(coeffs): 

    x,y,a1,b1,a2,b2,c = symbols('x y a1 b1 a2 b2 c', real = True) 

    y_sols = [ 
     (-c*sqrt((a1**2 - 2*a1*a2 + a2**2 + b1**2 - 2*b1*b2 + b2**2 - c**2)* 
      (a1**2 + 2*a1*a2 - 4*a1*x + a2**2 - 4*a2*x + b1**2 - 2*b1*b2 + b2**2 
      - c**2 + 4*x**2))*(-b1 + b2 + c)*(b1 - b2 + c) + (b1**2 - 2*b1*b2 + 
      b2**2 - c**2)*(-a1**2*b1 + a1**2*b2 + 2*a1*b1*x - 2*a1*b2*x + 
      a2**2*b1 - a2**2*b2 - 2*a2*b1*x + 2*a2*b2*x - b1**3 + b1**2*b2 + 
      b1*b2**2 + b1*c**2 - b2**3 + b2*c**2))/(2*(-b1 + b2 + c)* 
      (b1 - b2 + c)*(b1**2 - 2*b1*b2 + b2**2 - c**2)), 
      (c*sqrt((a1**2 - 2*a1*a2 + a2**2 + b1**2 - 2*b1*b2 + b2**2 - c**2)* 
      (a1**2 + 2*a1*a2 - 4*a1*x + a2**2 - 4*a2*x + b1**2 - 2*b1*b2 + b2**2 
      - c**2 + 4*x**2))*(-b1 + b2 + c)*(b1 - b2 + c) + (b1**2 - 2*b1*b2 + 
      b2**2 - c**2)*(-a1**2*b1 + a1**2*b2 + 2*a1*b1*x - 2*a1*b2*x + 
      a2**2*b1 - a2**2*b2 - 2*a2*b1*x + 2*a2*b2*x - b1**3 + b1**2*b2 + 
      b1*b2**2 + b1*c**2 - b2**3 + b2*c**2))/(2*(-b1 + b2 + c)* 
      (b1 - b2 + c)*(b1**2 - 2*b1*b2 + b2**2 - c**2)) 
      ] 
    num_coefs = [(a, f) for a, f in (zip([a1,b1,a2,b2,c], coeffs))] 
    y_solsf0 = y_sols[0].subs(num_coefs) 
    y_solsf1 = y_sols[1].subs(num_coefs) 
    print(y_solsf0, '\n', y_solsf1) 

    f0 = lambdify([x], y_solsf0) 
    f1 = lambdify([x], y_solsf1) 
    return f0, f1 
+0

你能幫我明白這是爲什麼比參數方法更好?它似乎過於複雜... – Praveen

+0

不一定「更好」,我提出一個觀點,純x,y座標解決方案不是「不可能」 – f5r5e5d

+0

@ f5r5e5d感謝您的答案。事實上,這顯示了sympy的巨大力量,並提供了一個非常好的解決方案。 –