only size-1 arrays can be converted to Python scalars: Trying to plot circular orbit with equation of. circle

69 Views Asked by At

I'm relatively new to python so forgive me for any nonsense in my code. I am trying to program a circular orbit of a planet (I just used the mass of Uranus and the Sun) in python using equations from my Classical Mechanics textbook (John R. Taylor's Classical Mechanics). I figured I could just use the equations and graph a function, y, that equals the equations of a circle with c_squared being the radius and x being an array of values being used to plot the circle. Let me know how I can improve the code or I am even going in the right direction.

...

import matplotlib.pyplot as plt 
import numpy as np  
import matplotlib
import math
fig = plt.figure()
ax = fig.add_subplot()

m_uranus = 8.681 * 10**(25)
m_sun = 1.989 * 10 **(30) 
G = 6.67430 * 10**(-11)
mu = (m_uranus * m_sun)/(m_uranus + m_sun)
l = (1.7 * 10**(42)) * 1000 * 24 * 60 * 60 
ang_squared = l ** 2
c = (ang_squared)/(G * m_uranus * m_sun * mu)
c_squared = c**2 
print(m_sun, mu,  m_uranus,  ang_squared, c)

x = np.arange(-100, 100, 1)

y = math.sqrt(c_squared - x) 

plt.plot(x, y)
plt.show()

...

1

There are 1 best solutions below

0
On

As mentioned by @JohanC, use numpy np.sqrt() instead of math.sqrt() will fix your error, here the fix with (unnecessary libraries removed):

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot()

m_uranus = 8.681 * 10 ** 25
m_sun = 1.989 * 10 ** 30
G = 6.67430 * 10 ** (-11)
mu = (m_uranus * m_sun) / (m_uranus + m_sun)
l = (1.7 * 10 ** 42) * 1000 * 24 * 60 * 60
ang_squared = l ** 2
c = ang_squared / (G * m_uranus * m_sun * mu)
c_squared = c ** 2
print(m_sun, mu, m_uranus, ang_squared, c)

x = np.arange(-100, 100, 1)

y = np.sqrt(c_squared - x)

plt.plot(x, y)
plt.show()

Hope, this will help you by!