Python Syntax

From Physics Book
Jump to navigation Jump to search

The Main Idea

This page discusses basic vPython functions and how they can be used to produce a model. vPython uses the same syntax as regular Python; however, vPython also allows you to produce a 3D model simulating the equations and computations your code is producing.

Mathematical Model

Vpython can be used with any equation. However, you may find some of the following useful:

Momentum Update: pf = pi + Fnet*deltat

Position Update: objectf.pos = objecti.pos + (pcart/mcart)*deltat


Gravitational Force:

  1. CONSTANTS

G = 6.7e-11 mEarth = 6e24 mcraft = 15e3 deltat = 60 t = 0

r=craft.pos-Earth.pos #finds the change in position m=mcraft rmag= mag(r) #finds the magnitude of change in position Fmag=(G*mcraft*mEarth)/(rmag**2)

# ^^Calculates the new magnitude of gravitational force

rhat=r/rmag

#^^Calculates the direction of tbe change in position

Fnet=-Fmag*rhat

  1. ^^Calculates net force

Spring Force:

L0 = 0.3 Lvec = ball.pos - ceiling.pos Lhat = norm(Lvec) Lmag = mag(Lvec) Fspr = (-ks)*(Lmag - L0)*(Lhat)

Kinetic Energy:

Kinetic = (1/2)*(mball*(vel**2))


Computational Model

VPython is used to create computational models of various real world situations so that we can see how these equations used in the code can manipulate these situations.