Skip to content

loop function and integrate?

6 messages · faeriewhisper, Rui Barradas, Berend Hasselman

#
Hi guys! 
I have to compute something and i don't know what i'm doing wrong. my code
is a bit complex, but imagine that is something like this: 

a = c(1,2,3,4) 
ia = length(a) 

x = seq(1,100,length=0.1) 
ib = length(x) 

int1 = numeric(ib) 
b = numeric(ib) 

for(j in 1:ia) { 
   H = function(x) {sin(x + a[j])} 
      for(i in 1:ib) {	
                int =  integrate(H, lower = 0, upper = x[i]) 
                int1[i] = int[1] 
                b[i] = 1 + a[i] 
                } 
        end 
   int1 = unlist(int1) 
   int2 = int1*b 
   ss[j] = sum(int2) 
} 
end 

if i try this code without the for loop it's ok, but when i put the for on,
i get all sort of errors... 
Thank you for your help :) 



--
View this message in context: http://r.789695.n4.nabble.com/loop-function-and-integrate-tp4651436.html
Sent from the R help mailing list archive at Nabble.com.
#
Now i've managed to do this:

funcs <- list()
funcs[]

# loop through to define functions
for(i in 1:ib-1){

    # Make function name
    funcName <- paste( 'func', i, sep = '' )

    # make function
    func = paste('function(x){sin(x + a[', i,'])))}',sep = '')

    funcs[[funcName]] = eval(parse(text=func))
  

    }
end

but still cant apply the integrate in a loop for all the different
functions..... :/
help me out guys.... pretty please :)



--
View this message in context: http://r.789695.n4.nabble.com/loop-function-and-integrate-tp4651436p4651455.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

Your code doesn't run without initializing 'ss' to something. And I've 
made some changes, but I don't understand what you are trying to do. See 
comments inline.


a = c(1,2,3,4)
ia = length(a)

x = seq(1, 100, by=0.1)   # It was 'length = 0.1' (!)
ib = length(x)

ss <- numeric(ia)  # New, 'ss' must exist.
int1 = numeric(ib)
b = numeric(ib)

for(j in 1:ia) {
     H = function(x) {sin(x + a[j])}
     for(i in 1:ib) {
         int =  integrate(H, lower = 0, upper = x[i])
         int1[i] = int[1]
         b[i] = 1 + a[j]   # It was 'a[i]', didn't make any sense.
     }
     int1 = unlist(int1)
     int2 = int1*b
     ss[j] = sum(int2)
}
ss


And for loops (or any other type of loops) do _not_ end with 'end'.

Hope this helps,

Rui Barradas

Em 30-11-2012 15:08, faeriewhisper escreveu:
#
On 30-11-2012, at 16:08, faeriewhisper wrote:

            
What are you doing?
What's the "end" doing in your code in two places.
end is a function to extract and encode the last observation of a time series object. See ?end
It makes absolutely no sense to put them in your code.
Remove them immediately.

You haven't declared ss to be a vector.
So before the start of the j loop insert ss <- numeric(ia)

And simplify your code:

ss <- numeric(ia)

for(j in 1:ia) { 
  H = function(x) {sin(x + a[j])} 
     for(i in 1:ib) {	
               int =  integrate(H, lower = 0, upper = x[i])  
               int1[i] = int$value
               b[i] = 1 + a[i] 
               }
  ss[j] = sum(int1*b)    
} 
ss

And more simplification is possible by eliminating b, which I leave to you.

Berend
#
On 30-11-2012, at 19:34, Berend Hasselman wrote:

            
There is no need to define the function H in the j loop.
You could do this:

H = function(x,A) {sin(x + A)}

for(j in 1:ia) {
     aj <- a[j]
     for(i in 1:ib) {
               int =  integrate(H, lower = 0, upper = x[i], A=aj)
               int1[i] = (1 + a[i])* int$value
               }
  ss[j] = sum(int1)
}

And if Rui is right  a[i] can be replaced by aj.

Berend
#
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.