#Program to evaluate integration using Monte carlo method (four leaf rose):
from math import*
def f(x):
y=sin(2*x)
return y
import random as rn
nt=input("Enter total no. darts:")
nt=int(nt)
ns=0.0
for i in range (nt):
x=rn.random()
y=rn.random()
x1=-1+2*x
y1=-1+2*y
r=sqrt(x1**2+y1**2)
o=atan2(y1,x1)
if r<=abs(f(o)):
ns=ns+1
A=ns/nt*4
print("The value of the integration is:",A)
from math import*
def f(x):
y=sin(2*x)
return y
import random as rn
nt=input("Enter total no. darts:")
nt=int(nt)
ns=0.0
for i in range (nt):
x=rn.random()
y=rn.random()
x1=-1+2*x
y1=-1+2*y
r=sqrt(x1**2+y1**2)
o=atan2(y1,x1)
if r<=abs(f(o)):
ns=ns+1
A=ns/nt*4
print("The value of the integration is:",A)
Comments
Post a Comment