Answer to Question #275726 in Python for Fajla Rabby

Question #275726

 Use Monte Carlo integration to estimate the integral of a function. Also calculate the error-> limit(1,0) ∫ 𝑥 ^2 (3𝑥 + 1)𝑑x


1
Expert's answer
2021-12-05T13:07:45-0500
from scipy import random
import numpy as np

a = 0
b = 1
N = 1000


ar = np.zeros(N)

for i in range(len(ar)):
    ar[i] = random.uniform(a, b)

integral = 0.0

def f(x):
    return x**2*(3*x+1)

for i in ar:
    integral += f(i)

ans = (b-a)/float(N)*integral

print(f"The answer {ans}.")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS