Coding: orbiting planets using vpython

Watch this thread
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#1
Report Thread starter 1 year ago
#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.Name:  2020-12-27 (4).png
Views: 137
Size:  87.1 KBName:  2020-12-27 (5).png
Views: 157
Size:  128.1 KBName:  2020-12-27.png
Views: 139
Size:  67.0 KB
Last edited by poop yakuza; 1 year ago
0
reply
Eimmanuel
Badges: 13
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#2
Report 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.Name:  2020-12-27 (4).png
Views: 137
Size:  87.1 KBName:  2020-12-27 (5).png
Views: 157
Size:  128.1 KBName:  2020-12-27.png
Views: 139
Size:  67.0 KB
What is the trouble that you are having? Or at least post what the VPython is complaining about your code.
0
reply
0le
Badges: 21
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#3
Report 1 year ago
#3
Your last line is incorrect.
Code:
// time = (+dt)
time =+ dt

It should be:
Code:
//time = time + dt
time += dt
0
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#4
Report Thread starter 1 year ago
#4
(Original post by 0le)
Your last line is incorrect.
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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#5
Report Thread starter 1 year ago
#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)
Name:  2020-12-28.png
Views: 77
Size:  23.4 KB
Last edited by poop yakuza; 1 year ago
0
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#6
Report Thread starter 1 year ago
#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.
"""
##################
planet = sphere(pos=position,radius=0.2,make_trail=True, color=color.red)

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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#7
Report Thread starter 1 year ago
#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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#8
Report Thread starter 1 year ago
#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
reply
0le
Badges: 21
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#9
Report 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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#10
Report Thread starter 1 year ago
#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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#11
Report Thread starter 1 year ago
#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
Name:  2020-12-28 (1).png
Views: 58
Size:  42.7 KB
part A refers to the code from the first post
0
reply
Eimmanuel
Badges: 13
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#12
Report 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
Name:  2020-12-28 (1).png
Views: 58
Size:  42.7 KB
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
reply
poop yakuza
Badges: 9
Rep:
? You'll earn badges for being active around the site. Rep gems come when your posts are rated by other community members.
#13
Report Thread starter 1 year ago
#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
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

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

Personalise

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%

Watched Threads

View All
Latest
My Feed