I wonder how can I draw elliptical orbit by using the equation ay2 + bxy + cx + dy + e = x2 ?
I have first determined the a,b,c,d,e constants and now I assume by giving x values I will obtain y and this will give me the graph I want but I couldn't do it by using matplotlib.
I would really appreciate, if you could help me!
EDIT: I added the code here.
from numpy import linalg
from numpy import linspace
import numpy as np
from numpy import meshgrid
import random
import matplotlib.pyplot as plt
from scipy import optimize
x = [1.02, 0.95, 0.87, 0.77, 0.67, 0.56, 0.44, 0.30, 0.16, 0.01]
y = [0.39, 0.32, 0.27, 0.22, 0.18, 0.15, 0.13, 0.12, 0.12, 0.15]
my_list = [] #It is the main list.
b = [0] * len(x) # That is the list that contains the results that are given as x^2 from the equation.
def fxn(): # That is the function that solves the given equation to find each parameter.
global my_list
global b
for z in range(len(x)):
w = [0] * 5
w[0] = y[z] ** 2
w[1] = x[z] * y[z]
w[2] = x[z]
w[3] = y[z]
w[4] = 1
my_list.append(w)
b[z] = x[z] ** 2
t = linalg.lstsq(my_list, b)[0]
print 'List of list representation is', my_list
print 'x^2, the result of the given equation is', b
print '\nThe list that contains the parameters is', t
fxn()
t = linalg.lstsq(my_list, b)[0]
print '\nThe constant a is', t[0]
print 'The constant b is', t[1]
print 'The constant c is', t[2]
print 'The constant d is', t[3]
print 'The constant e is', t[4]
EDIT: Here are the constant values:
a = -4.10267300566
b = 1.10642410023
c = 0.39735696603
d = 3.05101004127
e = -0.370426134994
Intro
Easiest thing would be to parametrize this equation. As @Escualo suggests, you could introduce a variable
t
and parametrizex
andy
along that. Parametrizing means separating your equation into two separate equations forx
andy
individually in terms oft
. So you would havex = f(t)
andy = g(t)
for some values oft
. You could then plot thex, y
pairs that result for each value oft
.The catch here is that your ellipse is rotated (the
x*y
coupling term is an indication of that). To separate the equations, you have to first transform the equation to get rid of the coupling term. This is the same as finding a set of axes that are rotated by the same angle as the ellipse, parametrzing along those axes, then rotating the result back. Check out this forum post for a general overview.Math
You first need to find the angle of rotation of the ellipse's axes with respect to the x-y coordinate plane.
Your equation then transforms to
where
To find the (nearly) standard form of the ellipse, you can complete the squares for the and portions and rearrange the equation slightly:
where
Since you know , you can now parametrize the equations for and :
You would then rotate back into normal x-y space using the formulas
and
Code
The code to get the x- and y- arrays to pass to
plt.plot
is now relatively straightforward:To actually use the code:
To overplot your original points on the ellipse:
The result is the following image:
Note
Keep in mind that the forum post I linked to uses a different notation than the one you do. Their equation is Ax2 + Bxy + Cy2 + Dx + Ey + F = 0. This is a bit more standard than your form of ay2 + bxy - x2 + cx + dy + e = 0. All of the math is in terms of your notation.
The problem can be solved for y as a function of x
The catch is that there are 2 values of y for every valid x, and no (or imaginary) y solutions outside the range of x the ellipse spans
below is 3.5 code, sympy 1.0 should be fine but print, list comps may not be backwards compatable to 2.x
when the above is ran in Spyder:
 The generated functions for the y values aren't valid everywhere:
The domain error would require a try/execpt to "feel out" the valid x range or some more math
like the try/except below: (Edited to "close" drawing re comment )
Edit: added duplicate start point to the end of ellipse point lists to close the plot figure
The easiest thing to do is rewrite in parametric form so that you end up with the expressions
x = A cos(t)
;y = B sin(t)
. You then create a full ellipse by assigningt = [0, 2 * pi]
and calculating the correspondingx
andy
.Read this article which explains how to move from a general quadratic form into a parametric form.