There were several typos in the Monte Carlo and Riemann summ file I sent
to the questioner, here are teh correct files. I think one of the other
responders switched the limits for x and y ...
#2-dimensional Monte Carlo integration
#flexible, fast, slow to converge, gives good approx answer
M<-900000 # points
rangex<-c(2,3)
rangey<-c(1,5)
f<-function(x,y){20/(x*y^2)}
fint<-mean(f(runif(M,rangex[1],rangex[2]),runif(M,rangey[1],rangey[2])))*
diff(rangex)*diff(rangey)
2-d Riemann sums
N=500
xx<-seq(from=2,to=3,length=N)
yy<-seq(from=1,to=5,length=N)
int2<-0
for(yz in yy){
int2<-int2+sum(f(xx,yz ) )
}
int2.x<-int2*diff(rangex)*diff(rangey)/N^2
Steven C. Wofsy
Abbott Lawrence Rotch Professor of Atmospheric and Environmental Chemistry
Harvard University
Pierce Hall, Room 110D, 29 Oxford St., Cambridge, MA 02138
email: Steven_Wofsy@harvard.edu
Telephone: 617-495-4566 FAX: 617:495-4551
|