Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Consider a needle of length L 100 that is dropped somewhere onto the middle of a piece of lined paper such that the separation between adjacent lines is d 2.Once dropped, the needle's position is determined by the position of its midpoint, x, and its rotation from the vertical, o Part A Suppose x and 6 are each determined by drawing from a random uniform distribution (from 0 to d and from 0 to 2T , respectively). Perform 10,000 drops and, for each, determine how many lines the needle crosses. Summarize your results with a histogram. What is the average number of crossings? Part B Repeat part A, but now suppose the angle is drawn from a normal distribution with a mean value 0 and o 0.3. This models some level of control (if the person is trying to drop the needle vertically, but can't do so reliably).

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()


    
    


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote