Skip to content

Instantly share code, notes, and snippets.

@icio
Created February 28, 2011 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icio/848120 to your computer and use it in GitHub Desktop.
Save icio/848120 to your computer and use it in GitHub Desktop.
Newton-Raphson in R
#| 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