简体   繁体   English

牛顿-拉夫森 function 在 R (ELI5)

[英]Newton-raphson function in R (ELI5)

Would something take a few moments to explain to me, line by line, how this function calculates the root of a function using the newton raphson method?是否需要一些时间来逐行向我解释这个 function 如何使用牛顿拉夫森方法计算 function 的根? And how the r code is executing that.以及 r 代码是如何执行的。 Especially the return-part?特别是退货部分?

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)]) #
}

Currently I've defined the variables like this but I'm not sure if it is correct:目前我已经定义了这样的变量,但我不确定它是否正确:

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

Thanks a million for any sort of help!感谢一百万的任何帮助!

The Logic is find [ function f(x) = 0 ]'s roots。but we can't solve the root immediately when this function is Higher Order,so we use a program 1000(n) times to guess which one is closest to the root.逻辑是找到[ function f(x) = 0 ]的根。但是当这个function是高阶时我们不能立即解出根,所以我们使用程序1000(n)次猜测哪个最接近根。 Sometimes this function is continuously differentiable,but sometimes not, so if we found p[] data is convergent to a accuracy number, we get the root, else not.有时这个 function 是连续可微的,但有时不是,所以如果我们发现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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM