Skip to content

Instantly share code, notes, and snippets.

@icio icio/gist:848120

Created Feb 28, 2011
Embed
What would you like to do?
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
You can’t perform that action at this time.