簡體   English   中英

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

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

是否需要一些時間來逐行向我解釋這個 function 如何使用牛頓拉夫森方法計算 function 的根? 以及 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 ]的根。但是當這個function是高階時我們不能立即解出根,所以我們使用程序1000(n)次猜測哪個最接近根。 有時這個 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