Python Forum
Using an Array in a While Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using an Array in a While Loop
#14
looking at this part of your code
while True:
    for i in arange(0,9,1):
        rate(1000000)
        t=t+dt
        rsun = posit[0]-posit[i]
         
        if mag(rsun) == 0:
            continue
        Fsun = ((G*mass[0]*mass[i])/(mag(rsun)**2))* norm(rsun) #Calculating the force of gravity from the sun
 
        rmerc = posit[1]-posit[i]
        if mag(rmerc)==0:
            continue
        Fmerc = ((G*mass[1]*mass[i])/(mag(rmerc)**2))* norm(rmerc)
 
        rven = posit[2]-posit[i]
        if mag(rven)==0:
            continue
        Fven = ((G*mass[2]*mass[i])/(mag(rven)**2))* norm(rven)
 
        rear = posit[3]-posit[i]
        if mag(rear)==0:
            continue
        Fear = ((G*mass[3]*mass[i])/(mag(rear)**2))* norm(rear)
 
        rmars = posit[4]-posit[i]
        if mag(rmars)==0:
            continue
        Fmars = ((G*mass[4]*mass[i])/(mag(rmars)**2))* norm(rmars)
 
        rjup = posit[5]-posit[i]
        if mag(rjup)==0:
            continue
        Fjup = ((G*mass[5]*mass[i])/(mag(rjup)**2))* norm(rjup)
 
        rsat = posit[6]-posit[i]
        if mag(rsat)==0:
            continue
        Fsat = ((G*mass[6]*mass[i])/(mag(rsat)**2))* norm(rsat)
 
        ruran = posit[7]-posit[i]
        if mag(ruran)==0:
            continue
        Furan = ((G*mass[7]*mass[i])/(mag(ruran)**2))* norm(ruran)
 
        rnep = posit[8]-posit[i]
        if mag(rnep)==0:
            continue
        Fnep = ((G*mass[8]*mass[i])/(mag(rnep)**2))* norm(rnep)
         
        Fnet = Fsun + Fmerc + Fven + Fear + Fmars + Fjup + Fsat + Furan + Fnep
        acc[i]=Fnet/mass[i]
        vel[i]=vel[i] + acc[i]*dt
        posit[i]=posit[i] + vel[i]*dt
Not 100% sure, but it looks like it will never calculate anything - you loop over all objects so at least one mag will be 0, so it will continue with next i, without even reaching Fnet = Fsun + Fmerc + Fven + Fear + Fmars + Fjup + Fsat + Furan + Fnep and next lines. Not to mention repeating code

I think what you want is

while True:
    for i in arange(0,9,1):
        Fnet = 0
        for j in range(i+1,9,1):
            rate(1000000)
            t=t+dt
            r = posit[j]-posit[i]
            F = ((G*mass[j]*mass[i])/(mag(r)**2))* norm(r) #Calculating the force of gravity from object j over object i
            Fnet += F

        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]



this way, when i=0, j will take values from 1 to 8
when i=1, j will take values from 2 to 8, etc.

PS. Not sure where the best place for rate(1000000) is...
Reply


Messages In This Thread
Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 01:28 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-13-2017, 04:47 AM
RE: Using an Array in a While Loop - by j.crater - Jun-13-2017, 06:01 AM
RE: Using an Array in a While Loop - by buran - Jun-13-2017, 08:48 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-13-2017, 10:24 PM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 04:12 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 04:45 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:06 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:18 AM
RE: Using an Array in a While Loop - by Skaperen - Jun-14-2017, 05:29 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 05:46 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 05:52 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 07:05 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:23 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 08:44 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:03 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:36 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 10:40 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 10:58 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:07 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 11:17 AM
RE: Using an Array in a While Loop - by buran - Jun-14-2017, 11:45 AM
RE: Using an Array in a While Loop - by JakeWitten - Jun-14-2017, 03:17 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020