范例取自Option Pricing Models and Volatility Using Excel VBA
Page-8 Newton-Raphson Method
f(x) = x^2-7*x+10
f'(x) = 2*x-7
R code:
fun<-function(x){x^2-7*x+10}
dfun<-function(x){2*x-7}
max.it <- 50
tol <- 0.00001
current.x = 1 #start value
for(i in 1:max.it)
{
x=current.x
current.x=x-fun(x)/dfun(x)
dff=current.x-x
if(abs(dff) < tol) break
x=current.x
#print(x)
}
x #x=2