# HW3 of Biophysics

# Problem 2
import matplotlib.pyplot as plt
import numpy as np
NUM_SIMS = 30
NUM_STEPS = 1000
STEP_LENGTH = 1 # delta
STEP_TIME = 1 # tau
CHOICES = [-1, 1]
posX = np.zeros(NUM_SIMS)
posY = np.zeros(NUM_SIMS)
def get_msd(posX, posY):
    return (np.sum(posX * posX + posY * posY)) / NUM_SIMS
msd = np.empty(NUM_STEPS)
for i in range(NUM_STEPS):
    posX += np.random.choice(CHOICES, size=NUM_SIMS) * STEP_LENGTH
    posY += np.random.choice(CHOICES, size=NUM_SIMS) * STEP_LENGTH
    msd[i] = get_msd(posX, posY)
print(msd[-1] / 4 / NUM_STEPS)
0.7s
Python
plt.figure()
plt.plot(msd)
plt.gcf()
1.1s
Python
# Problem 3 (B)
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.special import comb
N = 20 # numer of segments
RANGE = 2 * math.sqrt(N) # one-sided range
DELTA = 0.5 # segment length
POINTS = RANGE * 4 # number of points on the X-axis
R = np.linspace(-RANGE, RANGE, num=POINTS, dtype=np.float64) # X-AXIS
def get_prob_combination(N):
    return comb(N, (N + R/DELTA) / 2) / 2**N
def get_prob_gaussian(N):
    return 2 / math.sqrt(2 * math.pi * N) * np.exp(-R * R / (2 * N * (DELTA**2)))
probCombi = get_prob_combination(N)
probGaussian = get_prob_gaussian(N)
plt.figure()
plt.plot(R, probCombi, R, probGaussian)
plt.gcf()
1.2s
Python