Reputation: 29
I have found this code that can integrate a given function and give the answer using Monte-Carlo method. However, I wanted to implement it in python but I don't know how to make the "srand(time(Null))" and Rand_Max parts happen in python. Also I want to use the "func(x)" in such a way that I can input different function to get their integration value. Say I have a probability density function and I want to use this integration to find expectation value of x and x^2 to calculate variance of x (in this case, integration of x or x^2 times the probability density function).
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;
double func(double x)
{
return (1+cos(x))*sin(abs(2*x))/abs(1+sin(2*x));
}
/*Common Monte Carlo method formula for integration*/
double MC_ID(double a, double b, double c, double d, int N)
{
int i;
double x, y, area;
int count=0;
for(i=0; i<=N; i++)
{
x=a+(b-a)*(rand()/(double)RAND_MAX);
y=c+(d-c)*(rand()/(double)RAND_MAX);
if(y<=func(x)) count++;
area = (b-a)*(d-c)*((double)count/(double)N);
}
return area;
}
int main()
{
srand(time(NULL));
cout<<MC_ID(M_PI,M_PI,-0.3,0.9,10000<<endl;
return 0;
}
I want to implement it in python numpy library but I can't convert the seed and "srand(time(null))" and Rand-Max parts.
import math
import random
def func(x):
return (1+math.cos(x))*math.sin(abs(2*x))/abs(1+math.sin(2 *x)) #I want to make it (the "func(x)") so that I can input multiple equations to find its integration value or make the MC_ID a module to use it on another file?
def MC_ID(a, b, c, d, N):
i = None
x = None
y = None
area = None
count = 0
for i in range(0, N + 1):
x = a+(b-a)*(random.random()/float(RAND_MAX)) #Problem***
y = c+(d-c)*(random.random()/float(RAND_MAX)) #Problem***
if y<=func(x):
count += 1
area = (b-a)*(d-c)*(float(count)/float(N))
return area
#srand(time(None)) #Problem***
MC_ID(-math.pi, math.pi, -0.3, 0.9, 10000)
#This is my humble attempt.
Upvotes: 1
Views: 192
Reputation: 921
I think there is no default RAND_MAX
equivalent in python so we will add a function calculating it.
And for srand(time(None))
used in C++ it can be ignored in python.
So let's try the following code:
import math
import random
def func(x):
return (1+math.cos(x))*math.sin(abs(2*x))/abs(1+math.sin(2 *x))
def RAND_MAX(size):
""" Generates pseudo-numbers range and returns the max value
Parameters
----------
size : The length of random number list
Returns
-------
Max of generated numbers
"""
rands=[random.random() for i in range(size)]
m=max(rands)
return m
RAND_MAX=RAND_MAX(10000)
def MC_ID(a, b, c, d, N):
i = None
x = None
y = None
area = None
count = 0
for i in range(0, N + 1):
x = a+(b-a)*(random.random()/float(RAND_MAX))
y = c+(d-c)*(random.random()/float(RAND_MAX))
if y<=func(x):
count += 1
area = (b-a)*(d-c)*(float(count)/float(N))
return area
MC_ID = MC_ID(-math.pi, math.pi, -0.3, 0.9, 10000)
print(MC_ID)
Output
3.475858111931747
import math
import random
from sympy import *
import sympy
def main():
expression = sympy.Symbol(input("Write the equation.. "))
func(expression)
a = float(input("Value of a:"))
b = float(input("Value of b:"))
c = float(input("Value of c:"))
d = float(input("Value of d:"))
N = int(input("Value of N:"))
MC_ID_Value = MC_ID(a, b, c, d, N)
print(f"Integrataion value: {MC_ID_Value}")
def func(expression):
return expression
def RAND_MAX(size):
""" Generates pseudo-numbers range and returns the max value
Parameters
----------
size : The length of random number list
Returns
-------
Max of generated numbers
"""
rands=[random.random() for i in range(size)]
m=max(rands)
return m
RAND_MAX=RAND_MAX(10000)
def MC_ID(a, b, c, d, N):
i = None
x = None
y = None
area = None
count = 0
for i in range(0, N + 1):
x = a+(b-a)*(random.random()/float(RAND_MAX))
y = c+(d-c)*(random.random()/float(RAND_MAX))
if y<=func(x):
count += 1
area = (b-a)*(d-c)*(float(count)/float(N))
return area
if __name__ == "__main__":
main()
Will this work to input the equation via terminal input to find the expression turned into an equation for integration?
Upvotes: 1