pv_f<-function(fv,r,n)fv/(1+r)^n
cashFlows<-c(-100,40,50,60)
myNPV<-function(r){
    n<-length(cashFlows)
    npv<-0
    for(i in 1:n){
        fv<-cashFlows[i]
        pv<-pv_f(fv,r,(i-1))
       npv<-npv+pv
    }
    return(npv)
}


small<-10000
r<-0
irr<--9
while(r<1){
   npv<-abs(myNPV(r))
   if(npv<small){
       small<-npv
       cat('small=',small,"\n")
       irr<-r
    }
    r<-r+0.00001
}

cat("irr=",irr,"\n")
