Created
February 28, 2011 21:54
-
-
Save icio/848120 to your computer and use it in GitHub Desktop.
Newton-Raphson in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#| Using the Newton-Raphson Method, find the root of | |
#| the function f (with derivative fdx) near the | |
#| point x (with tolerance e [solution x has abs f(x) < e] | |
#| and within a limit of 1000 steps) | |
newtonRaphson <- function(x, f, fdx, e = 0.001, maxSteps = 1000) | |
{ | |
step = 0; | |
while (abs(x) > e && step < maxSteps) | |
{ | |
step = step + 1; | |
x = x - f(x) / fdx(x); | |
} | |
return(list( | |
x = x, fx = f(x), step = step | |
)); | |
} | |
newtonRaphson(10, function(x) x^3, function(x) 3*x^2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment