VPython Loops

From Physics Book
Revision as of 15:40, 27 November 2016 by Ncastro9 (talk | contribs)
Jump to navigation Jump to search

Claimed by Nicolas Castro (Fall 2016)

An introduction into iteration in VPython and the implementation of loops.

The Main Idea

In VPython, loops are essential in order to create animations, repeat a calculation multiple times, or complete some task that must be done until a condition is met. Loops are based on the concept of iteration. Iteration is simply the repetition of a block of code and looping is the implementation of this idea.

Let’s say that we needed to find the final momentum of an object over five seconds with a time interval of one second. Given the initial conditions, we could do this problem by hand fairly quickly. Now, let’s say you needed to find the final momentum after 50 seconds. We can still definitely do this problem by hand, except it will take substantially longer. Using VPython, we can solve this problem very simply. We can write a script that will calculate the final momentum at each time interval and then use looping to have the script run 50 times or until a condition is met. This makes our lives way easier and can give us the answer to our problem in no time.

Using the concept of iteration and implementing it with loops allows us to take many repeated calculations or updates and condense the problem into a few lines of code that will simply be repeated. Without looping, we would do this problem by hand, making it quite cumbersome to do. Therefore, loops are extremely useful, in that it makes coding in any language significantly more efficient and allows us to break down large problems into a series of small repeated calculations. We can use iteration through two types of loops: ‘while’ loops and ‘for’ loops. Each has its own purpose and both are useful in their own situations.

For Loop

For loops are based on a specified list and can repeat a function a specific number of times.

Examples

For loops can be used in conjunction with a list of items or numbers.

solarsystem = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for planet in solarsystem:
    print(planet) 

In the example of above a list will be printed as follows:


For loops can also be used to complete a function for a certain number of times given as a range.

for x in range(0,3):
    print "I love physics!" 

This will print the print the give phrase 3 times.

While Loop

While loops are used to repeat a function until a certain value or criteria is met, which is generally less restrictive than a for loop.

Examples

While loops are very useful in physics for representing time intervals. For example, if you wanted to express that a object was moving over a certain time period you could represent it as such:

#initial values
ball = sphere(pos=vector(0, 0, 0), radius=1)
time = 0
velocity = 5
#calculations
while time < 100
     time = time+10
     pos = velocity*time 

This loop starts with the initial values of position = 0 meters, time = 0 seconds, and velocity = 5 m/s. The loop will run until the time value reaches 100 seconds. Inside the loop, time is first updated so that every iteration of the loop increases the time value (i.e. the first run of the loop time becomes 10 seconds, the second run time becomes 20 seconds, and so on). Next the position is updated using a physics formula: change in distance = velocity * time.

While loops can also be used to create objects in a pattern. For example, if you wanted to create series of spheres in a line you could use the following code:

#initial values
distance = 0
#calculations
while distance < 100:
    distance = distance + 10
    ball = sphere(pos=vec(distance,0,0) , radius=1)
 

This loop will create a ball of the same radius in a line along the x axis every 10 meters. It's possible to alter distances along the y and z axis the same why simply by creating a variable for the y or z part of the vector.

While loops can also be used to create objects in a circular path!

#initial values
theta = 0
#calculations
while theta < 2*pi:
    theta= theta + pi/6
    location = vector(cos(theta),sin(theta),0)
    ball = sphere(pos=(location), radius=0.1) 

This code creates a series of 12 spheres in a circle by changing theta each time the loop is iterated. Changing the the increment by which theta is increased (in this case pi/6) you can change the number of spheres that are formed.


History

The idea of loops predate computer programming, but the first instance of loops being used in this application was by Ada Lovelace to calculate Bernoulli numbers which was described in 1842.

See also

External links

1. More on For Loops

2. More on While Loops

3. Loops and Sequences