planet.py

Created by mobluse

Created on September 14, 2024

1.08 KB

The script shows a planet orbit about the origin, where the Sun is. The initial velocity v0=1.0 results in a circular orbit. Units are not SI units. Press Back/Esc to enter a new v0. The output, comments, and function names are in Swedish.

Skriptet visar en planetbana runt origo, där solen är. Ursprungshastigheten v0=1.0 ger en cirkulär bana. Enheterna är inte SI-enheter. Tryck på Back/Esc för att mata in ett nytt v0. Utskrifterna, kommentarerna och funktionsnamnen är på svenska.


# Planetbana
from math import *
import math
from time import *
from turtle import *

# Konstanter
G=1.0
M=1.0
m=1.0
dt=0.01

def omvandla(x,y):
  xpl=101*x
  ypl=101*y-7
  goto(xpl,ypl)

def koordaxlar():
  penup()
  omvandla(-1.0,0.0)
  pendown()
  omvandla(1.0,0.0)
  penup()
  omvandla(0.0,1.0)
  pendown()
  omvandla(0.0,-1.0)

def planet(vx=1.0):
  # Initialtillstand
  t=0.0
  vy=0.0
  x=0.0;y=1.0
  r=sqrt(x*x+y*y)
  # Grafinit
  hideturtle()
  speed(0)
  pensize(1)
  color("green")
  koordaxlar()
  penup()
  setheading(0)
  goto(-162,91)
  color("blue")
  write("Planetbana v0=%.3f"%vx)
  omvandla(x,y)
  color("black")
  showturtle()
  speed(1)
  pendown()
  try:
   # Stegning
    while True:
      omvandla(x,y)
      Fx=-G*M*m*x/(r**3)
      Fy=-G*M*m*y/(r**3)
      ax=Fx/m
      ay=Fy/m
      vx=vx+ax*dt
      vy=vy+ay*dt
      x=x+vx*dt
      y=y+vy*dt
      r=sqrt(x*x+y*y)
      t=t+dt
      setheading(math.degrees(atan2(-y,-x)))
      sleep(0.02)
  except:
    print("r=%.3f, t=%.3f."%(r,t))
    vx=float(input("v0? "))
    planet(vx)

vx=float(input("v0? "))
planet(vx)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.