fn<- function(x){x^3-3*x+1}
fn(0) # =1
## [1] 1
FRoot_1 <- function(a,b,j=10){ #(a=lower_bound, b=upper_bound, j=#of_iterations)
c<- (a+b)/2 #midpoint of the interval
print(fn(c)) #Value of the function at the midpoint
for(i in 1:j){ #for_loop
if (fn(a)*fn(c)<0 ){
b<-c
}
else {
a<-c
}
c<- (a+b)/2
print(fn(c))
}
return(c(c,fn(c)))
}
run this code to check that the function work properly
you can use any values other than the written below
FRoot_1(0,1)
## [1] -0.375
## [1] 0.265625
## [1] -0.07226562
## [1] 0.09301758
## [1] 0.009368896
## [1] -0.03171158
## [1] -0.01123571
## [1] -0.0009493232
## [1] 0.00420583
## [1] 0.001627262
## [1] 0.000338721
## [1] 0.347167969 0.000338721