title: Bisection method in R
body: <p>I am trying to solve an equation using Bisection method. However, when I try to run this, I get the below error </p>
  
  <pre><code>"Error in if ((fn(kVec, tVec, b) * fn(kVec, tVec, a) &gt; 0) | (b &gt; a)) { : 
    argument is of length zero"
  </code></pre>
  
  <h1>Bisection method routine</h1>
  
  <pre><code>bisect&lt;-function(kVec,tVec,fn,b,a,tol=1e-15){
    i&lt;-0
    r&lt;-(b+a)/2
    res&lt;-c(i,r,fn(kVec,tVec,r))
    if ((fn(kVec,tVec,b)*fn(kVec,tVec,a)&gt;0)|(b&gt;a)) {
      return('Improper start values') }
    else
      while (abs(fn(kVec,tVec,r))&gt;tol) {
        i&lt;-i+1
        if (fn(kVec,tVec,b)*fn(kVec,tVec,r)&gt;0) {
          b&lt;-r
          r&lt;-(b+a)/2 
        }
        else {
          a&lt;-r
          r&lt;-(b+a)/2 
        }
        res&lt;-rbind(res,c(i,r,fn(kVec, tVec,r)))  
    }
  return(res)
  }
  </code></pre>
  
  <h1>Function to define the equation</h1>
  
  <pre><code>FCfunc&lt;-function(kVec,tVec,b){
  for(i in 1:k){
      ((kVec[i]*(tVec[i]*exp(-tVec[i])-tVec[i-1]*exp(-b*tVec[i-1])))
      }
  }
  </code></pre>
  
  <h1>Calling bisection routine with some initial estimates</h1>
  
  <pre><code>bisect(kVec,tVec,FCfunc,0.00001,10.00001,tol=10e-16) 
  </code></pre>