This is for a computational physics course utilizing the python programming lang
ID: 653969 • Letter: T
Question
This is for a computational physics course utilizing the python programming language.
The problem is to simulate the dispersion of cream particles in a 2 dimensional cup of coffee. The idea is that 'n' cream particles start at the center of the cup, and eventually disperse throughout the cup by means of a 'random walk'. The random walk is where a particle can move +1 or -1 in the x direction and +1 or -1 in the y direction for each time step based on a randomly generated number.
The exact wording of the problem is as follows:
Perform the random-walk simulation of spreading cream, and let one of the walls of the container possess a small hole so that if a cream particle enters the hole, it leaves the container. Calculate the number of cream particles in the container as a function of time. Use the parameters of a 50x50 2-dimensional cup, with a hole 10 units in length along one of the edges.
I have started and here is the code I have currently:
import numpy
import pylab
import random
n=10
t=0
x = numpy.zeros(n)
y = numpy.zeros(n)
for t in range(100):
for i in range(n):
r=random.random()
r2=random.random()
if r>0.50 and x[i]<50:
x[i] = x[i]+1
else:
x[i] = x[i]-1
if r<0.50 and x[i]<-50:
x[i] = x[i]-1
else:
x[i] = x[i]+1
if r2>0.50 and y[i]<50:
y[i] = y[i]+1
else:
y[i] = y[i]-1
if r2<0.50 and y[i]<-50:
y[i] = y[i]-1
else:
y[i] = y[i]+1
Sorry the formatting of the code is off, but I couldn't figure out a better way to upload it.
The code now doesn't give me the independance of movement in the x and y directions like it should.
The code also moves every particle to the boundary and once it is at the boundary, it stays there.
I do not know how to put in the hole in the side of the cup.
Again, the language used is Python.
Please help!
Explanation / Answer
import random
x = int(raw_input("Please enter a number up to 100. "))
z = random.randint(40,100)
""" x and z can both be used below without errors -- testing/learning"""
randomNumber = random.randint(1,z)
print "first random",randomNumber
if 35 >= randomNumber:
newRandom = randomNumber * .5
newRandom = newRandom + 9
print "second random " "%.f" % newRandom
elif 36 <= randomNumber <= 93:
newRandom = randomNumber + 7
print "second random " "%.f" % newRandom
else:
newRandom = randomNumber
print "second random",newRandom
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.