Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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')
pltxlim(0,10)y
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)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions

Question

denigration of emotional outbursts; being reserved;

Answered: 1 week ago