【发布时间】:2021-03-12 02:15:18
【问题描述】:
是否需要一些时间来逐行向我解释这个函数如何使用牛顿拉夫森方法计算函数的根?以及 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
感谢一百万的任何帮助!
【问题讨论】: