Skip to content
Prev 312511 / 398506 Next

loop function and integrate?

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