# Coding: orbiting planets using vpython

Announcements
#1
Im stuck on animating the orbit of a planet around a star. No specific values are given and i was asked to write a general code. Ive checked that the first two functions linked below are indeed correct but im having trouble with the last one(part 3) which is animating.
Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.
Last edited by poop yakuza; 1 year ago
0
1 year ago
#2
(Original post by poop yakuza)
Im stuck on animating the orbit of a planet around a star. No specific values are given and i was asked to write a general code. Ive checked that the first two functions linked below are indeed correct but im having trouble with the last one(part 3) which is animating.
Any help is appreciated, if you would like the theory, please say so and ill post it but its just newton's law of gravitation.
What is the trouble that you are having? Or at least post what the VPython is complaining about your code.
0
1 year ago
#3
Code:
```// time = (+dt)
time =+ dt```

It should be:
Code:
```//time = time + dt
time += dt```
0
#4
(Original post by 0le)
Code:
```// time = (+dt)
time =+ dt```

It should be:
Code:
```//time = time + dt
time += dt```
thanks ive changed that. Im still getting the same animation as before. The orbit should be elliptical.
Last edited by poop yakuza; 1 year ago
0
#5
(Original post by Eimmanuel)
What is the trouble that you are having? Or at least post what the VPython is complaining about your code.
the last function utillises vpython and a while loop and it is the part that is incorrect. i know it is incorrect because when animating it, the orbit is meant be ellipitical for a specific set of values(see figure below)
Last edited by poop yakuza; 1 year ago
0
#6
here is the code:

from vpython import vector, mag, sphere, rate, color, canvas
G = 1. # gravitational constant [using units where gravitational constant is 1.0]

def force(pos1, pos2, m1, m2):
"""
Returns the gravitational force exerted by object 2 on object 1.
Input:
- pos1 = position vector of first object
- pos2 = position vector of second object
- m1 = mass of first object
- m2 = mass of second object
Depends on:
- G = gravitational constant (global variable)
"""
##################
#the position vector from object 2 to object 1
pos = pos2 - pos1
f = G*m1*m2*pos/(mag(pos))**3 #newton's gravitational force equation in vector form

return f
##################

def move_planet(position, velocity, m_star, dt):
"""
Calculate motion of planet in the gravitational field of a star with given mass
at the origin, using Euler's method.

Input:
- position: position vector of planet at start of time step
- velocity: velocity vector of planet at start of time step
- m_star: mass of star
- dt: time step

Output: (position_new, velocity_new)
- position_new: position vector of planet at end of time step
- velocity_new: velocity vector of planet at end of time step

Depends on:
- force = function to calculate the gravitational force between two objects
"""
##################
#the star is at the origin
star_pos = vector(0,0,0)

#arbritary number, there is no real reason other than defining it for calculating the acceleration in the line below
m_planet = 1
acc = (force(position, star_pos, m_planet, m_star))/m_planet

position_new = position + (velocity)*dt #the new position
velocity_new = velocity + acc*dt #the new velocity

return position_new, velocity_new
##################

def animate_planet(position, velocity, m_star, dt):
"""
Animate planetary orbit from given starting position, with given time step.
"""
##################

dt = 0.05
time = 0

while time<= 10:
rate(1/dt)
planet.pos, velocity = move_planet(position, velocity, m_star, dt)
time += dt
##################

# Initialize canvas, and set parameters of star and planet.
canvas()
pos_planet = vector(0,2,0) # initial position of planet
v_planet = vector(-22,0,0) # initial velocity of planet
m_star = 1000. # mass of star (units where G=1)
sphere(pos=vector(0,0,0), color=color.yellow, radius=0.1) # draw star

# Animate orbit of planet
animate_planet(pos_planet, v_planet, m_star, 1e-4)
Last edited by poop yakuza; 1 year ago
0
#7
update: it works now,

all i had to do was change

"planet.pos, velocity = move_planet(position, velocity, m_star, dt)" to "planet.pos, velocity = move_planet(planet.pos, velocity, m_star, dt)"
1
#8
Ive another question on this topic.

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the
planets as well as between each planet and the star? specifically how would the equations of motion change?
Last edited by poop yakuza; 1 year ago
0
1 year ago
#9
(Original post by poop yakuza)
Ive another question on this topic.

How would you go about adapting the code for two planets and taking into account the gravitational attraction between the
planets as well as between each planet and the star? specifically how would the equations of motion change?
It might be the three body problem(?):
https://en.wikipedia.org/wiki/Three-body_problem
0
#10
(Original post by 0le)
It might be the three body problem(?):
https://en.wikipedia.org/wiki/Three-body_problem
Im not sure ill get away with stating the equations for acceleration in the wiki page since i don't know how they are derived and also it might be a two-body problem since the star, the planets are orbiting around, is at the origin, not completely sure tho.
0
#11
currently, I have no idea how to adapt the move_planet function (2nd pic) for this case. the picture below shows what i need to do without giving specific instructions. id appreciate any help on this

part A refers to the code from the first post
0
1 year ago
#12
(Original post by poop yakuza)
currently, I have no idea how to adapt the move_planet function (2nd pic) for this case. the picture below shows what i need to do without giving specific instructions. id appreciate any help on this

part A refers to the code from the first post
Warning, I may not correctly read the detail as I am typing using a phone.

The modifications is replicating the code in the move_planet by adding the new interaction.
The N2L will be
For m1,
Force between m1 and m2 plus force between m1 and star equal m1×acceleration.
Do the same for m2.

Then apply Euler algorithm to move the planets.

Add the trailing accordingly in the main program.
0
#13
(Original post by Eimmanuel)
Warning, I may not correctly read the detail as I am typing using a phone.

The modifications is replicating the code in the move_planet by adding the new interaction.
The N2L will be
For m1,
Force between m1 and m2 plus force between m1 and star equal m1×acceleration.
Do the same for m2.

Then apply Euler algorithm to move the planets.

Add the trailing accordingly in the main program.
i was on the toilet when i figured this out lol, seems like I overcomplicated it, thanks for your help anyways!
the code works now too!
0
X

new posts Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### How did your Edexcel A-level Economics Paper 1 go?

Loved the paper - Feeling positive (66)
16.14%
The paper was reasonable (200)
48.9%
Not feeling great about that exam... (101)
24.69%
It was TERRIBLE (42)
10.27%