General Physics II

Physics 201

Python Programming

VPython programming resource information and guides
 

For general information about VPython visit the VPython Overview.For directions to download and install VPython visit the VPython Home Page. The easist way to run a Python program is to to open the IDLE program which is a GUI (graphical user interface) for Python.

Start Here

The VPython Tutorial is just a couple of pages and is a good introduction to Python and programming.

 

The VPython Introduction is a good

 

There are other resources available on the web like the YouTube Instructional Videos. Let me know if there are other resources you have found that are worth posting for your peers.

  The Official Python Website is a good reference site for any Python coding issues - but beware this site contains a lot of information.
BUG1

Use IDLE instead of VIDLE. VIDLE has problems printing to the printer.

BUG2 When saving programs using the save dialog window the IDLE (or VIDLE) editor crashes unless you click on "Desktop" on the left and then save the file. This is a known Tk bug. Thanks to Dr Craig for the screen shot and tracking down a solution to this issue.

Meredith's Measured Programming Method

 

Python programming for physics (students) is a short introduction - with examples - of the Python language.
Your programs can be more efficient if you use functions.

   

Programming Examples

Click on a link below to download a VPython program to your computer. Save the program to your desktop and then click to edit/run (or open from within IDLE).
  The first step in learning any programming language is learning the syntax. This is similar to learning a foreign language. Can you create an object? Can you create an arrow pointing to that object? CODE - Balls and Vectors
  The reason we want to learn a programming language is so that we can create models of the physicaal workd to test our understanding of physics. Can you make a cart and track about the right size, shape and color? Can you make the cart move along the track? CODE - Cart on a Track
 

Once we know how to animate an object we can start imparting the object with real data like mass and start changing its motion with forces. But first we want to be able to create a graph of the carts motion. What does the graph of position vs time look like for a cart traveling at a constant velocity? What happens if you change the velocity? What happens if the cart has constant acceleration? CODE - Plotting Motion     Note: You need to import the graphics package.

 

How do you apply a changing force to an object? We use the momentum principle in either an acceleration formulation (F=ma) or using momentum (p=mv). Both require that we start giving the object certain properties like mass. The code below applies a short impulse (1 second) force to an object. There are other differences in how the force is set and turned in both of these programs. CODE - Acceleration Method and Momentum Method

  You are now able to use your physics to model any system. You just need to learn the syntax for creating springsif you want to visualize them. You could still have an object oscillating due to an "invisible" spring - it just woundn't look as cool. CODE - Mass on a Spring
  This code models how to use a defined function to determine the force at different times and then plot force a s function of time (the thrust curve) for an Estes 1/2A3 engine. CODE - Rocket Thrust
  This program races two carts with different masses. Which cart will have the most kinetic energy? Which cart will have the most momentum? Modify the program for diffferent racing conditions. CODE - Racing Carts
  This program determines the temperature change in some water given the heat input rate and heat output rate. Modify the program to make the heat output rate proportional to the temperature difference. CODE - Heat Flow
  These programs use the spring-model for an interactive force to simulate an elastic and inelastic collision for a cart hitting a wall. The elastic colision CODE - Cart Wall Collide and the inelastic colision CODE - Cart Wall Smash
  Subtracting vectors in 3D and creating unit (normalized) vectors CODE - Vectors
  It is stratightforward to plot the cross product of two vectors. This program plots the cross product of two vectors and then uses the concept to plot the magnetic field from a moving charge. CODE - Vector Cross Product
This program is written by one of the book's authors to demonstrate cross products CODE - Cross Products

New
April 8

The motion of a charged object is different in an electric field and a magnetic field. If you combine both fields you obtain cyclotron motion.
This program plots the motion of a charged object in an electric field. CODE - Charge in E field
This program plots the motion of a charged object in a magnetic field. CODE - Charge in B field