Professional Documents
Culture Documents
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
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
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
1
dc g(x)
c=
1
dc g(a
+ (b a)y) c
## [1] 0.02240692
integrate(g,2,5) # built in function "integrate()"
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()"