f<-function(x){x^3-x-1};f1<-function(x){3*x^2-1};
SMfzero(f,0,2)
##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (f, x1, x2, num = 1000, eps = 1e-05, eps1 = 1e-05)
{
i = 0
while ((abs(x1 - x2) > eps) & (i < num)) {
c = x2 - f(x2) * (x2 - x1)/(f(x2) - f(x1))
x1 = x2
x2 = c
i = i + 1
}
print(x2)
print(f(x2))
if (abs(f(x2)) < eps1) {
print("finding root is successful")
}
else print("finding root is fail")
}
Run the code above in your browser using DataLab