是否需要一些时间来向我解释,一行一行,这个函数如何使用newton raphson方法计算函数的根?以及r代码是如何执行的。尤其是回归部分?
newton <- function(f, delta = 0.0000001, x_0 = 2, n=1000){
h = 0.0000001
i = 1; x1 = x_0
p = numeric(n)
while (i <= n) {
df.dx = (f(x_0 + h) - f(x_0)) / h
x1 = (x_0 - (f(x_0) / df.dx))
p[i] = x1
i = i+1
if (abs(x1 - x_0) < delta) break
x_0 = x1
}
return(p[1: (i-1)]) #
}
目前我已经定义了这样的变量,但我不确定它是否正确:
f = the function we input
delta = the accuracy threashold we are willing to accept
x_0 = our initial guess
n = the number of iterations
h = the distance from original guess to true root
abs = the current value of the function
非常感谢任何形式的帮助!
逻辑是找到[function f(x) = 0
]的根。但是当这个函数是高阶函数时,我们不能立即解出根,所以我们使用一个程序1000(n)
乘以来猜测哪个最接近根。有时这个函数是连续可微的,但有时不是,所以如果我们发现p[]
数据收敛于一个精确数,我们得到根,否则不是。
f = the function we input (Y)
delta = the accuracy threashold we are willing to accept (Y)
x_0 = our initial guess (Y)
n = the number of iterations (Y)
h = the distance from original guess to true root (N)
h = the distance from X1 to X0,this value much little ,the root much closed.
abs = the current value of the function (N)
abs is a sys