VPython Class 6: Plotting, Standing Waves, and Animation 2 ...

Physics 212E

Classical and Modern Physics

Spring 2016

VPython Class 6: Plotting, Standing Waves, and Animation

1.

Introduction

Were changing directions this week: we wont be using the 3D graphical visualization capability of

Vpython. Rather, we will be using the more conventional 2D plotting capability of the python module

matplotlib to look at the motion of standing waves.

2.

Getting started

Reminder:

? All Programs Python 3.2 IDLE (Python GUI)

? From the Python shell, select File Open

? Open a new program

? As usual, the first line of your program should be:

from visual import *

NOTE: Although we wont be using Vpythons graphics, the Vpython module contains many

standard mathematical functions that we will use. (You could import the numpy or scipy modules to get many of the same features.)

? The second line of your program should be:

import matplotlib.pyplot as plt There are other ways to import matplotlib,

but this will work. When imported this way, every command from this module must be preceded

by plt. as a prefix.

3.

Plotting a function

Lets start with the function y(x) = 3x3 ? 10x2 , and lets make a plot in the range ?2 < x < 3.

? We first need to choose a set of points at which to calculate and plot the function. The linspace

command will do this. Try the command

x = linspace(-2,3,11)

This creates an array of x values. We can make the functioning a little more clear in a program

with some commentary:

1

lep = -2.0

# Left end-point for x

rep = 3.0

# Right end-point for x

np = 11

# Number of points (number of intervals = np-1

x = linspace(lep,rep,np)

? Calculate the value of the function for all your values of x. Python makes this easy. Modify your

script as follows:

lep = 0.0

# Left end-point for x

rep = 5.0

# Right end-point for x

np = 11

# Number of points (number of intervals = np-1

x = linspace(lep,rep,np)

y = x**2

print(y)

Create arrays for x and y(x) for the function y(x) = 3x3 ? 10x2 .

? We can create a plot object as follows:

plt.plot(x,y)

This doesnt actually display the plot on the screen. For that you need to add

plt.show()

? You can add lots of stuff to your graph. For example, you can add items like the following:

plt.xlabel(Label for horizontal axis)

plt.ylabel("Label for vertical axis")

plt.title("This is the title")

plt.grid(True)

plt.ylim([-40,40])

# Label for horizontal axis

# Label for vertical axis

# Turn on grid

# Set scale for plot, and

# turn off autoscaling

? At this point the graph of the function should be displayed. Before moving forward, define a

function in your program, say f(x), that calculates the y values. Put the function definition at

the beginning of your program (but after the import statements. After defining the function, you

should replace the line

y = x**2

with the line

y = f(x)

2

This isnt really necessary in a program this simple, but isolating the function this way will help in

future exercises.

? Save a working version of your plotting program, with an appropriate new name (I call mine

grapher.py). You can use this program in later exercises when you need to plot a function.

? Return to the program you have been working on, leaving the version of grapher.py in its

current form so that it will be available for future use.

? Consider a string stretched between x = 0 and x = 1.

C Change your function f(x) so that it will plot the spatial profile of the string when it is

oscillating in its lowest-frequency mode, with the longest possible wavelength.

C Change the parameters in your program to so the graph has appropriate x and y limits.

C Make a graph of the spatial profile of the

C Make a graph of the spatial profile of the string when it is oscillating in the next lowest

frequency mode.

C Make a graph of the spatial profile when the two lowest frequency modes (with equal amplitudes) are both excited at the same time on the string.

4.

Animation

Animating your plot takes only a few more steps. Lets work first on the graph of the lowest frequency

standing wave mode of the string. We are going to use a while loop and draw a new function every

time the program goes through the loop. Here are the changes you will need to make:

? Give your plot a name, but this name must end with a comma. Your plot command should look

something like

p, = plt.plot(x,y)

? Comment out your show() command.

? Add a while loop to your program:

while True:

y = f(x)

p.set_ydata(y)

t = t + 0.01

plt.draw()

# Calculate new y-data at new t

# Update y-data in plot

# Increment the time

? Add the following line before the plt.plot(x,y) command.

plt.ion()

3

? Run your program when you are plotting just the lowest-frequency mode.

? Run your program when you are plotting just the second-lowest-frequency mode. (What should

the angular frequency be for this mode?)

? If you have time, animate the string when there is a linear combination of both modes.

5.

Submit your work

Submit one complete script with comments that I can run, that demonstrates how far you got. For your

submitted scripts please use the following filename format:

vpXX_yourlastname.py,

where XX is the two-digit assignment number (in this case 06) and _ is the underscore character. For

example, a good filename would be: vp06_ligare.py

Once youve saved a copy of your code with this filename you can submit it by copying it into my

drop box at facultystaff/m/mligare. (You cant save it directly there, but you can copy or drag

the file into it.)

4

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download