Find the answer to this problem by coding it in the computer program python. I a
ID: 3711124 • Letter: F
Question
Find the answer to this problem by coding it in the computer program python. I am having trouble with the coding it.
Explanation / Answer
#part a
import numpy as np
import math
import matplotlib.pyplot as plt
L = 100
d = 2
theta = 0
x = 0
tol = 1e-18
#uniform distribution for thetas part a
thetas = np.random.uniform(0 ,2*math.pi,10000)
#uniform distribution for x part a
xs = np.random.uniform(0, d , 10000)
#list to have the crossing in each throw
crossings = []
for i in range(10000):
x = xs[i]
theta = thetas[i]
right = abs((L*math.sin(theta))/2)
left = right
#length left to the middle
left -= x
#length to the right of middle
right -= (d-x)
#total crossings
crossing = int((left+tol)/d) + int((right+tol)/d)
crossings.append(crossing)
#average crossing in part a
average_crossings = int(sum(crossings)/10000)
print("average crossings:(part a)",average_crossings)
#plotting histogram
plt.hist(crossings)
plt.title("Distribution of crossings in needle throw(part a)")
plt.xlabel("No of crossing")
plt.ylabel("No of occurances")
plt.show()
#part b
#normal distribution for thetas part b mean = 0 , sd = 0.3
thetas_normal = np.random.normal(0,0.3,10000)
crossings_normal = []
for i in range(10000):
x = xs[i]
theta = thetas_normal[i]
right = abs((L*math.sin(theta))/2)
left = right
#length to the left of middle
left -= x
#length to the right of middle
right -= (d-x)
#total crossings
crossing = int((left+tol)/d) + int((right+tol)/d)
crossings_normal.append(crossing)
#average crossings in part b
average_crossings_normal = int(sum(crossings)/10000)
print("average crossings:(part b)",average_crossings_normal)
#plotting histogram in part b
plt.hist(crossings_normal)
plt.title("Distribution of crossings in needle throw(part b)")
plt.xlabel("No of crossing")
plt.ylabel("No of occurances")
plt.show()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.