Jun-13-2017, 01:28 AM
I'm trying to model the solar system, with the gravitational effects of each planet interacting with every other planet (including the Sun). So far I have all of the planets orbiting the Sun, but I can't figure out how to do the same for Mercury. My plan was to do exactly what I did for the sun, but I kind of cheated with the sun by excluding it from the loop and using a separate variable for the sun when getting my r value. I had to do this because I get a 'division by zero' error when I leave it in.
Is there a way to include the sun in 'i' for the loop, exclude it while calculating r for the suns gravity, then include it again when calculating Mercuries gravity?
Thanks!
By the way, this is for Python v.2.7.9, and I don't have numpy. (I can't use it for this particular class)
Is there a way to include the sun in 'i' for the loop, exclude it while calculating r for the suns gravity, then include it again when calculating Mercuries gravity?
from visual import * myuniv = display(range=vector(7*10**12,7*10**12,7*10**12)) G = 6.674*10**-11 #Gravitational constant #Masses of each solar body in order starting with the Sun mass = [1.989*10**30,3.285*10**23, 4.867*10**24, 5.972*10**24, 6.39*10**23, 1.898*10**27, 5.68*10**26, 8.68*10**25, 1.024*10**26] #Positions of each solar body starting with the Sun posit = [vector(0,0,0),vector(5.791*10**10,0,0),vector(1.082*10**11,0,0), vector(1.496*10**11,0,0),vector(2.279*10**11,0,0),vector(7.785*10**11,0,0), vector(1.429*10**12,0,0),vector(2.871*10**12,0,0),vector(4.498*10**12,0,0)] #Velocities of each solar body starting with the Sun vel = [vector(0,0,0),vector(0,4.87*10**4,0),vector(0,3.502*10**4,0), vector(0,2.98*10**4,0),vector(0,2.401*10**4,0),vector(0,1.307*10**4,0), vector(0,9.69*10**3,0),vector(0,6.81*10**3,0),vector(0,5.43*10**3,0)] #Acceleration of each solar body starting with the Sun acc = [vector(0,0,0),vector(0,0,0),vector(0,0,0), vector(0,0,0),vector(0,0,0),vector(0,0,0),vector(0,0,0), vector(0,0,0),vector(0,0,0),vector(0,0,0),vector(0,0,0)] Msun = 1.989*10**30 Ssun = vector(0,0,0) t = 0.0 dt = 150000 sun = sphere(pos=posit[0],radius=9.9*10**10,color=color.yellow,make_trail=True) mercury = sphere(pos=posit[1],radius=2.44*10**6,color=color.red,make_trail=True) venus = sphere(pos=posit[2],radius=6.052*10**6,make_trail=True) earth = sphere(pos=posit[3],radius=6.371*10**6,color=color.green,make_trail=True) mars = sphere(pos=posit[4],radius=3.39*10**6,color=color.red,make_trail=True) jupiter = sphere(pos=posit[5],radius=6.99*10**7,color=color.blue,make_trail=True) saturn = sphere(pos=posit[6],radius=5.823*10**7,color=color.yellow,make_trail=True) uranus = sphere(pos=posit[7],radius=2.536*10**7,color=color.cyan,make_trail=True) neptune = sphere(pos=posit[8],radius=2.462*10**7,make_trail=True) while True: for i in arange(1,9,1): rate(1000000) t=t+dt r = Ssun-posit[i] Fnet= ((G*Msun*mass[i])/(mag(r)**2))* norm(r) #Calculating the force of gravity from the sun acc[i]=Fnet/mass[i] vel[i]=vel[i] + acc[i]*dt posit[i]=posit[i] + vel[i]*dt sun.pos=posit[0] mercury.pos=posit[1] venus.pos=posit[2] earth.pos=posit[3] mars.pos=posit[4] jupiter.pos=posit[5] saturn.pos=posit[6] uranus.pos=posit[7] neptune.pos=posit[8]Hopefully this made sense. For the most part I know what I'm doing, but I'm still very new to Python.
Thanks!

By the way, this is for Python v.2.7.9, and I don't have numpy. (I can't use it for this particular class)