Question: write code below with import random function do not use numpy #New Updated Code import numpy as np import matplotlib.pyplot as plt import math import
write code below with import random function do not use numpy #New Updated Code
import numpy as np
import matplotlib.pyplot as plt
import math
import time
# Calculate pi using Monte Carlo method
def calpinumpoints:
x nprandom.uniform numpoints
y nprandom.uniform numpoints
incircle x y
piest npsumincircle numpoints
return piest
# Function to calculate percent error
def pererrorestpi:
return math.fabsestpi math.pi math.pi
# Number of points to test
points
errors
# Calculate percent error for each number of points
for numpoints in points:
piest calpinumpoints
error pererrorpiest
errors.appenderror
printfPoints: numpoints Pi Estimate: piest Percent Error: error:f
# Plotting the percent error
pltfigurefigsize
pltplotpoints errors, markerocolor'green', linestyle'dashed'
pltxscalelog
pltxlabelPoints
pltylabelPercent Error'
plttitleMonte Carlo Method Percent Error in Pi Calculation via Numpy'
pltgridTrue
pltshow
nsamples
start time.time
end time.time
elaptime end start
printfElapsed time for nsamples samples: elaptime seconds"
nsamples
start time.time
end time.time
elaptime end start
printfElapsed time for nsamples samples: elaptime seconds"
# Measure performance for steps
nsteps
start time.time
end time.time
elaptimesteps end start
printfElapsed time for nsteps steps: elaptimesteps seconds"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
