hello guys! thank u for the help, but u didnt understood what i need. 1st, it is a[i] cuz i want to sum 1 + x[i], for all i's not j. but i've solved it! :) like i said, my code is more complex, but, if you need to integrate several functions in a loop, thats what you should do:
w2 = seq(-1,-1/3,length=100) ib = length(w2) bin = w2[2] - w2[1] w3 <- numeric(ib-1) for (h in 1:ib-1) w3[h] = (w2[h] + w2[h+1])/2 end probt <- numeric(ib) di2 <- numeric(ia) a2 <- numeric(ia) ic = ib-1 arealog <- numeric(ic) log_probt <- numeric(ic) funcs <- list() funcs[] # loop through to define functions for(i in 1:ic){ funcName <- paste( 'func', i, sep = '' ) func = paste('function(z){c/(Ho*sqrt(dens*(1+z)^3 + (1-dens)*(1+z)^(3*(1+w3[', i,']))))}',sep = '') funcs[[funcName]] = eval(parse(text=func)) } end for(j in 1:ic){ for(i in 1:ia){ d2 = integrate(funcs[[j]], lower = 0, upper = z[i]) di2[i] = d2[1] a2[i] = 1 + z[i] } end di2 = unlist(di2) dist2 = di2*a2 mag2 = 5*log(dist2) + 25 prob2 = (1/disp*sqrt(2*pi))*exp( - ((mag2 - probv)^2)/2*disp^2) log_prob2 = log(prob2) log_proba2 = sum(log_prob2) log_probt[j] = log_proba2 arealog[j] = bin*(log_proba2) } end log_probt2 = sum(arealog)/(w2[ib] - w2[1]) -- View this message in context: http://r.789695.n4.nabble.com/loop-function-and-integrate-tp4651436p4651489.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.