I am trying to solve an equation using Bisection method. However, when I try to run this, I get the below error
"Error in if ((fn(kVec, tVec, b) * fn(kVec, tVec, a) > 0) | (b > a)) { :
argument is of length zero"
bisect<-function(kVec,tVec,fn,b,a,tol=1e-15){
i<-0
r<-(b+a)/2
res<-c(i,r,fn(kVec,tVec,r))
if ((fn(kVec,tVec,b)*fn(kVec,tVec,a)>0)|(b>a)) {
return('Improper start values') }
else
while (abs(fn(kVec,tVec,r))>tol) {
i<-i+1
if (fn(kVec,tVec,b)*fn(kVec,tVec,r)>0) {
b<-r
r<-(b+a)/2
}
else {
a<-r
r<-(b+a)/2
}
res<-rbind(res,c(i,r,fn(kVec, tVec,r)))
}
return(res)
}
FCfunc<-function(kVec,tVec,b){
for(i in 1:k){
((kVec[i]*(tVec[i]*exp(-tVec[i])-tVec[i-1]*exp(-b*tVec[i-1])))
}
}
bisect(kVec,tVec,FCfunc,0.00001,10.00001,tol=10e-16)
#bisection method
a<--give trial value-;a
b<--give trial value;b
f<-function(x){-give function-}
f(a)
f(b)
c<-(a+b)/2;c
while(f(c)!=0&&b-a>.00002)
{
if(f(c)==0)
{
c
}
if(f(c)<0)
{
a<-c
}
else
{
b=c
}
{
c<-(a+b)/2;c
}
}
c