0% found this document useful (0 votes)
3K views3 pages

Monte Carlo Integration

The document describes Monte Carlo integration, which uses random sampling to estimate integrals. It provides 4 examples of using Monte Carlo integration to calculate integrals and compares the results to using the integrate() function. The Monte Carlo method samples random values within the bounds of integration and averages the function values. This provides estimates that are close to the true integral values calculated by the integrate() function.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views3 pages

Monte Carlo Integration

The document describes Monte Carlo integration, which uses random sampling to estimate integrals. It provides 4 examples of using Monte Carlo integration to calculate integrals and compares the results to using the integrate() function. The Monte Carlo method samples random values within the bounds of integration and averages the function values. This provides estimates that are close to the true integral values calculated by the integrate() function.

Uploaded by

api-285777244
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Monte Carlo Integration

YIK LUN, KEI


[email protected]

Example 1
R1
0 (cos(20x)

=x+

+ sin(50x))2 dx

1
80 sin(40x)

1
200 sin(100x)

1
30 cos(30x)

1
1
70 cos(70x)|0

a_1<- 1 - cos(30*1)/30 - cos(70*1)/70 + sin(40*1)/80 - sin(100*1)/200


b_0<- 0 - cos(30*0)/30 - cos(70*0)/70 + sin(40*0)/80 - sin(100*0)/200
c=a_1-b_0
n<-1000000
x<-runif(n)
f<-function(x) {(cos(20*x)+sin(50*x))^2} # Using only original function
I<-sum(f(x))/n # Sampling and find the average
I # Monte Carlo Integration
## [1] 1.046332
c # Mathemetical Method
## [1] 1.045276
integrate(f,0,1) # built in function "integrate()"
## 1.045276 with absolute error < 2.1e-10

Example 2
R 1 x2
2 /
2dx
0 e

n=10^4
x<-runif(n)
f<-function(x) {exp(-x^2/2)/sqrt(2*pi)}
I=sum(f(x))/n
I # Monte Carlo Integration
## [1] 0.3413131

+ constant

integrate(f,0,1) # built in function "integrate()"

## 0.3413447 with absolute error < 3.8e-15

Example 3
Interval between 2 and 5
R 5 x2
2 /
2dx
2 e

Integral =

Rb
a g(x)dx

y = (x a)/(b a)
x = (b a)y + a
Integral = (b a)

R1
0 g((b

a)y + a)dy

If c g(x) d on interval [a,b]


f (y) =

1
dc g(x)

c=

1
dc g(a

+ (b a)y) c

0 f (y) 1, Therefore Integral = (b a)(d c)


a=2
b=5
g=function(x) { exp(-x^2/2)/sqrt(2*pi) }
f=function(y) { (g(a+(b-a)*y)-c)/(d-c) }
c=min(g(a:b))
d=max(g(a:b))
n=10^4
x=runif(n)
I=(b-a)*(d-c)*sum(f(x))/n+c*(b-a)
I # Monte Carlo Integration

## [1] 0.02240692
integrate(g,2,5) # built in function "integrate()"

## 0.02274985 with absolute error < 2.5e-16

R1
0 g(y)dy

+ c(b a)

Example 4
Interval between 0 and 52.2155
R 52.2155
23.42056
0

x
+ 1.74564 (109 ) exp( 17.004425
) sin( (x+19.88618)
(7.88822108 ) )dx

a=0
b=52.2155
g=function(x) { -23.42056+1.74564*(10^9)*exp(-x/17.004425)*sin(pi*(x+19.88618)/(7.88822*10^8)) }
f=function(y) { (g(a+(b-a)*y)-c)/(d-c) }
c=min(g(a:b))
d=max(g(a:b))
n=10^4
x=runif(n)
I=(b-a)*(d-c)*sum(f(x))/n+c*(b-a)
I # Monte Carlo Integration

## [1] 2638.193
integrate(g,0,52.2155) # built in function "integrate()"

## 2649.597 with absolute error < 2.9e-11

You might also like