########################################
## Root Finding: False Method
#########################################
FalseMethod = function(fn, # input function
TOL, # error tolerance
max.iter = 100, # max allowed iterations
x1, # initial value #1
x2, # initial value #2
x3,
detail = TRUE, # output intermediate output
## adding controls for the graphic
graphic = TRUE, # default
xlimit, # x-limits: a vector of lower and upper limits
ylimit, # y-limits: a vector of lower and upper limits
... # other optional arguments if any
){
ctr = 0 # counter of iteration
ERR = abs(x2 - x1)
## base graph
if(graphic ==TRUE){
xx = seq(xlimit[1], xlimit[2], length = 500) # x-coordinates
yy = fn(xx) # y-coordinates
plot(xx, yy, type = "l",
xlab = "x",
ylab = "Y",
xlim = xlimit,
ylim = ylimit,
lwd = 2,
col = "blue",
lty = 1)
}
abline(h = 0, lty = 2, col = "navy")
##
while(ERR > TOL){
ctr = ctr + 1
new.x = x1-((x2-x1)*fn(x1))/(fn(x2)-fn(x1)) # here we switch out the secant formula for the false position formula which is similar
ERR = abs(new.x - x2)
if(ERR < TOL){
cat("\n\nThe algorithm converges!")
cat("\nThe approximated root is", new.x,".")
cat("\nThe absolute relative error ERR=",ERR,".")
cat("\nThe number of iterations:", ctr, ".")
break
} else{
if(graphic == TRUE){
segments(x2, fn(x2), x1, fn(x1), lty = 1, col = "purple")
segments(new.x, fn(new.x), new.x, 0, col = "red", lty = 2)
}
if(detail == TRUE){
cat("\nIteration:", ctr,", approximated root:", new.x,", absolute relative error:", ERR,".")
}
# updating the two values. CAUTION: order matters
x1 = x2
x2 = new.x
}
if(ctr == max.iter){
cat("\n\nThe maximum number of iterations attained!")
cat("\nThe algorithm did not converge!")
break
}
} # close the while-loop
} # close the function environment
# define the function f(x) that satisfies f(x) = 0
test_func = function(x){3*x-exp(x)}
###
# call the function
FalseMethod(fn = test_func, # input function
TOL = 10^(-5), # error tolerance
max.iter = 100, # max allowed iterations
x1=1, # initial value #1
x2=2, # initial value #2
detail=TRUE, # output intermediate output
## adding controls for the graphic
graphic = TRUE, # default
xlimit=c(0,3), # x-limits: a vector of lower and upper limits
ylimit = c(-1, 35) # y-limits: a vector of lower and upper limits
)
##
## Iteration: 1 , approximated root: 1.168615 , absolute relative error: 0.8313847 .
## Iteration: 2 , approximated root: 1.311517 , absolute relative error: 0.1429012 .
## Iteration: 3 , approximated root: 1.797043 , absolute relative error: 0.4855265 .
## Iteration: 4 , approximated root: 1.436778 , absolute relative error: 0.3602651 .
## Iteration: 5 , approximated root: 1.486766 , absolute relative error: 0.04998839 .
## Iteration: 6 , approximated root: 1.515326 , absolute relative error: 0.02855947 .
## Iteration: 7 , approximated root: 1.512012 , absolute relative error: 0.003313826 .
## Iteration: 8 , approximated root: 1.512134 , absolute relative error: 0.0001220417 .
##
## The algorithm converges!
## The approximated root is 1.512135 .
## The absolute relative error ERR= 5.757598e-07 .
## The number of iterations: 9 .
Here we get the same results as the secant method although the false position converges slower than secant.