Question: Can you send me full source code in python? So that I can run on colab? [ 1 0 marks ] Use Monte Carlo method

Can you send me full source code in python? So that I can run on colab?[10 marks] Use Monte Carlo method to estimate the area of the shaded region. Also
make a scatter plot of your sample points as we did in our lecture. Color the rectangle
green, half circle red, both triangle blue.
I did it but there is a problem on this code for the triangle part please make correction and give me full code
import numpy as np
import matplotlib.pyplot as plt
def estimate_area(num_samples):
points_inside_rectangle =0
points_inside_half_circle =0
points_inside_triangle1=0
points_total = num_samples
for _ in range(num_samples):
x = np.random.uniform(0,10)
y = np.random.uniform(0,8)
# Check if point is inside the green rectangle
if (x >=2) and (x =6) and (y >=0) and (y =4):
points_inside_rectangle +=1
# Check if point is inside the upper semi-circle
elif ((x -4)**2+(y -4)**2=4) and (y >=4):
points_inside_half_circle +=1
# Check if point is inside the triangle (6,0),(8,2),(6,4)
elif (x >=6) and (x =8) and (y >=0) and (y =4) and (y =2*x -12) and (y =-2*x +20):
points_inside_triangle1+=1
# Estimate area
total_area =10*8
shaded_area = total_area *((points_inside_rectangle + points_inside_half_circle + points_inside_triangle1)/ points_total)
return shaded_area
def plot_points(num_samples):
x_rectangle_inside =[]
y_rectangle_inside =[]
x_half_circle_inside =[]
y_half_circle_inside =[]
x_triangle_inside =[]
y_triangle_inside =[]
for _ in range(num_samples):
x = np.random.uniform(0,10)
y = np.random.uniform(0,8)
if (x >=2) and (x =6) and (y >=0) and (y =4):
x_rectangle_inside.append(x)
y_rectangle_inside.append(y)
elif ((x -4)**2+(y -4)**2=4) and (y >=4):
x_half_circle_inside.append(x)
y_half_circle_inside.append(y)
elif (x >=6) and (x =8) and (y >=0) and (y =4) and (y =2*x -12) and (y =-2*x +20):
x_triangle_inside.append(x)
y_triangle_inside.append(y)
plt.figure(figsize=(8,6))
plt.scatter(x_rectangle_inside, y_rectangle_inside, color='green', label='Rectangle Inside')
plt.scatter(x_half_circle_inside, y_half_circle_inside, color='red', label='Half Circle Inside')
plt.scatter(x_triangle_inside, y_triangle_inside, color='blue', label='Triangle Inside')
# Plot rectangle
rect_points = np.array([[2,0],[6,0],[6,4],[2,4],[2,0]])
plt.plot(rect_points[:,0], rect_points[:,1], color='green', label='Rectangle')
# Plot upper semi-circle
theta = np.linspace(0, np.pi,100)
circle_x =4+2* np.cos(theta)
circle_y =4+2* np.sin(theta)
plt.plot(circle_x, circle_y, color='red', label='Upper Semi-circle')
# Plot triangle (6,0),(8,2),(6,4)
triangle_points = np.array([[6,0],[8,2],[6,4],[6,0]])
plt.plot(triangle_points[:,0], triangle_points[:,1], color='blue', label='Triangle')
plt.xlim(0,10)
plt.ylim(0,8)
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Monte Carlo Method - Sample Points')
plt.legend()
plt.show()
# Number of samples
num_samples =10000
# Estimate area
estimated_area = estimate_area(num_samples)
print("Estimated area of shaded region:", estimated_area)
# Plot sample points
plot_points(num_samples)
 Can you send me full source code in python? So that

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!