Docsity

VPython, Simple Mechanics - Laboratory 1 | PHY 300, Lab Reports of Physics

Toggle sidebar
bg1
Lab 1 - VPython, simple mechanics
Thursday 18 January - Due: Thursday 25 January
The aim of this lab is to get you started with programming in Python. While
programming has a strong abstract and theoretical component, it is also some-
thing which most people learn with practice and experimentation. Here, you
will learn about IDLE, the environment in which you develop and run Python
programs. We will also experiment with solving some simple mechanics problems
using Python/VPython.
1 Some introductory comments
Programming takes patience and practice. Writing good programs requires planning and a
logical approach. For someone who has not programmed before there is usually a period of
frustration. Don’t worry - this is normal and will decrease with practice. Be careful to type
things exactly as they are written in the lab – computers are very literal beasts and require
all i’s to be dotted and t’s to be crossed before they will do your bidding. If in doubt refer
to your Python book and the online documentation http://www.vpython.org/webdoc for
Visual Python.
1.1 Python
For the purposes of this class, a program in Python is simply one or more text files. These
files are collections of lines of text that give a recipe telling the computer what to do. A
program can be more than one file: one of the files will be the main program and the other
files will be modules that give the main program additional capabilities. These files are read
and interpreted by Python as commands. These commands have meanings such as ”print
this” or ”draw that”, multiply these two numbers or do the following commands 1000 times.
The Python interpeter, while reading the instructions, manages the data you create and
follows your instructions to give more detailed instructions to the computer on what to draw
or multiply or print. Python is called a high level language which means that you can tell
the computer to do quite complicated things with relatively few, transparent instructions.
The interpreter is then able to understand these instructions and translate them ultimately
into binary instructions (strings of 0 and 1’s) which can be fed to the CPU of the computer
to execute.
1.2 IDLE
IDLE stands for Integrated DeveLopment Environment. It is the interface that you will use
for writing and executing Python programs. It contains (amoung other things):
•An interactive shell - the shell executes your Python commands each time you type
them in and hit return.
pf3
pf4

Partial preview of the text

Download VPython, Simple Mechanics - Laboratory 1 | PHY 300 and more Lab Reports Physics in PDF only on Docsity!

Lab 1 - VPython, simple mechanics

Thursday 18 January - Due: Thursday 25 January

The aim of this lab is to get you started with programming in Python. While programming has a strong abstract and theoretical component, it is also some- thing which most people learn with practice and experimentation. Here, you will learn about IDLE, the environment in which you develop and run Python programs. We will also experiment with solving some simple mechanics problems using Python/VPython.

1 Some introductory comments

Programming takes patience and practice. Writing good programs requires planning and a logical approach. For someone who has not programmed before there is usually a period of frustration. Don’t worry - this is normal and will decrease with practice. Be careful to type things exactly as they are written in the lab – computers are very literal beasts and require all i’s to be dotted and t’s to be crossed before they will do your bidding. If in doubt refer to your Python book and the online documentation http://www.vpython.org/webdoc for Visual Python.

1.1 Python

For the purposes of this class, a program in Python is simply one or more text files. These files are collections of lines of text that give a recipe telling the computer what to do. A program can be more than one file: one of the files will be the main program and the other files will be modules that give the main program additional capabilities. These files are read and interpreted by Python as commands. These commands have meanings such as ”print this” or ”draw that”, multiply these two numbers or do the following commands 1000 times. The Python interpeter, while reading the instructions, manages the data you create and follows your instructions to give more detailed instructions to the computer on what to draw or multiply or print. Python is called a high level language which means that you can tell the computer to do quite complicated things with relatively few, transparent instructions. The interpreter is then able to understand these instructions and translate them ultimately into binary instructions (strings of 0 and 1’s) which can be fed to the CPU of the computer to execute.

1.2 IDLE

IDLE stands for Integrated DeveLopment Environment. It is the interface that you will use for writing and executing Python programs. It contains (amoung other things):

  • An interactive shell - the shell executes your Python commands each time you type them in and hit return.
  • A program editor - this allows you to put a set of commands into a program file. Once you enter and save a program file, IDLE has the capacity to run your program as a sequence of commands, rather than one at a time.

1.3 VPython

VPython is a special graphical extension to Python (technically a module) which gives a very nice simple environment to produce animations (simulations) of physical systems and record quantitative output data in the form of, for example, graphs. Typically a separate window is fired up to contain the graphical output.

1.4 Lab reports

You should record all your work and observations for each lab into a separate file (use Word, Wordpad or your favorite text/document editor). You can subsequently print this out on the cluster printer to hand in.

2 Starting IDLE and the shell

  1. You need to log onto your PC. Enter your NetID and password. You should save all of your work to your CMS/Unix account (the I: drive of the PC) and/or email it to yourself.
  2. Open up MS word or some other editor for your lab report. Put your name on the top of the page, the lab number and the date.
  3. Your report is to be a narrative of what you do. Be concise but be complete. I expect your lab report to be 2/3 pages typically.
  4. Save your report to a file (on your I: drive or SUnix account) with a name like ”My- name.lab1.doc”. This will help organize your work from this class.
  5. Now launch IDLE by first looking under the Start menu in windows. Find ”SU Aca- demic Departments”, then ”Physics”. There will be a menu choice for ”VPython”. Select that item.
  6. IDLE will open up a single window. There are two types of window that Python uses:
    • Shell windows. Shell windows have some text in them and contain the prompt which looks like this ” >>>> ”. These are good for interactive work and appear with diagnostic information when running a Python program.
    • File windows: these are for editing programs. An empty file window appears by default when you fire up Python. Additional file windows can be gotten by pulling down the File menu at the top of the IDLE editor page.
  1. Modify this force function to handle a constant downward (y direction) force of mag- nitude −1. This models gravity near the Earth’s surface (in some funny units). Also, add the following line to the bottom the euler.py program. The if must preserve the previous level of indentation so that it is run every time through the while loop

if(ball.pos.y<0): run=

Also change the condition while(1): to while(run): at top of loop. You will need to set run=1 somewhere after you create the ball object also. This if statement is a standard Python construct and allows us here to break out of the infinite update loop if a suitable logical condition is satisfied. In this case the variable run is a logical (sometimes called Boolean) variable which can take the values either 1 (true) or false (0). Notice also that the attribute pos of the ball object is accessed with the code ball.pos and that the y-component is further accessed with another dot operator i.e ball.pos.y. Finally replace the statement from force import * by the new one from force_projectile import *

  1. What does the condition ball.pos.y<0 mean physically?
  2. Run the code and record the result.
  3. Finally, download and run the code bounce.py from the course page. It just shows a ball bouncing elastically in a box. This example shows off some well the 3D capabilities of Vpython. What happens if you drag the mouse while holding down the middle mouse button. What happens if you hold the right mouse button instead?
  4. See if you can modify the code to change the size and color of the ball.