假设方程形式f(x)=exp(exp(-x)),求这个方程与g(x)=x的交点,即exp(exp(-x))=x,则可以使用下面的形式进行 求解:
fixedpoint(ftn,x0,tol):
ftn:表示所要求的方程f(x);注意:不是f(x)-x,要注意和迭代的区别
x0:表示初始的x值。
tol:表示当呈现运行时的精度,即|xn-xn-1|<tol时停止,选取此时的x作为exp(exp(-x))=x的解。
> ftn1 <- function(x) return(exp(exp(-x)))
> fixedpoint(ftn1, 2, tol = 1e-6)
At iteration 1 value of x is: 1.144921
At iteration 2 value of x is: 1.374719
At iteration 3 value of x is: 1.287768
At iteration 4 value of x is: 1.317697
At iteration 5 value of x is: 1.307022
At iteration 6 value of x is: 1.310783
At iteration 7 value of x is: 1.309452
At iteration 8 value of x is: 1.309922
At iteration 9 value of x is: 1.309756
At iteration 10 value of x is: 1.309815
At iteration 11 value of x is: 1.309794
At iteration 12 value of x is: 1.309802
At iteration 13 value of x is: 1.309799
At iteration 14 value of x is: 1.3098
Algorithm converged
[1] 1.3098
> exp(exp(-1.3098))
[1] 1.3097