Download from visual import * “Dr. Carter`s first vPython program 18-Sept

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1) Google “Vpython”
2) Go to first hit
Go do “Download
for Windows”
3) Download 32 bit version. Notice there are
two steps
First python 2.7.5
Then Vpython 2.7
Python and vPython software
• Python is a “high level” software language.
Like C++, Java, etc
• “interpreted” (like Perl) as opposed to
“compiled” (like C)
• Used in lots of software games, movies,
scientific situations
• vPython connects Python is pre-made
visualization software. Lots of physics
routines done in vPython.
Open “VIDLE for python”
Type in:
# Dr. Fazzini’s first Python program
# 27-Feb-2017
print 'hello world'
Hit <F5>
(Remember that tabs are important in python)
Let’s add a mistake and see what happens:
# Dr. Fazzini’s first Python program
# 27-Feb-2017
print 'hello world'
print 'hello world'
Put a <TAB> here.
Hit <F5>
(Remember that tabs are important in python)
Add a Loop
# Dr. Fazzini’s second Python program
# 27-Feb-2017
index = 0
while index <= 100:
print 'index is ',index
index += 1
print ('I am done! ')
Tabs go here.
Hit <F5>
Add a Random Number
from visual import *
# Dr. Fazzini’s second Python program
# 27-Feb-2017
index = 0
while index <= 100:
print 'index is ',index, random.random()
index += 1
print 'I am done'
Hit <F5>
Slow things down so you can see
from visual import *
# Dr. Fazzini’s second Python program
# 27-Feb-2017
index = 0
while index <= 100:
print 'index is ',index, random.random()
index += 1
sleep(0.01)
print ('I am done!')
Hit <F5>
Physics Model:
Model the following:
– Box starts with a 1000 particles
– Every step, there is a 0.5% chance each
particle will escape
– Print the number of particles inside and
outside the box.
from visual import *
STEP 1
# for random and sleep
# There are 1000 particles, all but one of them are in
# the box.
Ntotal = 1000
Nright = 1
Nleft = Ntotal - Nright
#
index = 0
#
# Setup the big loop
while index < 2000 :
sleep(0.01)
index += 1
# See if anyone moves from the left box to the outside
# There is a 0.5% chance for every particle.
index2 = 0
while index2 <= Nleft:
if random.random() < 0.005 :
Nleft -= 1
Nright += 1
index2 += 1
#
# give status
print (Nleft,' ', Nright)
#
# emergency stop
if Nleft < 0: break
print ('done')
Add some stuff in to get ready for
the three assignments.
Add two lines to the very top
from visual import *
# for sleep and random
from visual.graph import * # for the graphing features
from math import *
# for the log function
Assignment #1
1) Instead of just empty space, make a
second box to the right side.
2) There is now a 0.5% chance that each
particle in the right side will make back to
the left side.
3) Print out Nright, Nleft and Entropy each
step.
Hints
1)
2)
3)
4)
5)
Remember S = k ln(W)
In this case W = Ntotal!/(Nright!)(Nleft!)
In Python N! = math.factorial(N)
In Python ln(X) = log(X)
When printing the entropy, you’ll want to
plot S/Smax so it scales from 0 to 1
Assignment #2
Graph it.
Go to:
http://www.cod.edu/people/faculty
/cartert/PHY2115/
Click on the code-snipets link
Extra Credit
1) Make a plot to see if the entropy ever
goes down by just a little
HINT: Keep track of the previous maximum
value of the entropy at each step. Plot the
current value of entropy subtracted from the
previous maximum. According to the 2nd law,
that number should never be negative.