# [S] RE: Solving f(x)=0 - SUMMARY

Fri, 13 Mar 1998 08:05:00 +0100

The short reply is: uniroot(). (Luk Wouters and Kevin Brand). Barry
Brown suggested to look at his library invmf on statlib.

Jim Stapleton wrote:
I use a function "newton". For the multivariate case I use a function
"newtonraphson". Both require derivatives, though the derivatives can
be defined in approximation from the difference quotients. They are a
bit crude and not fast, though fast enough for my purposes.
js

> newton
function(x0, G, g, ep = 0.00001, z = 0, step = 1)
{
#Given the function G and its derivative g,
#newton uses the Newton method, beginning at x0,
#to find a point xp at which G is zero. G and g
#may each depend on a parameter z. The result
#is a list of the vector of x-values, the vector
#of G(x) values, and , the number of iterations.
#To get only the final x and final G(x) replace
#list(xv,Gv,lxv) by c(newx,Gnew,lxv).
# ep determines the stopping rule.
x0vec <- x0
count <- 0
Gvaluevec <- Gvalue <- G(x0, z)
while(abs(Gvalue) > ep) {
r <- Gvalue/g(x0, z)
d <- min(abs(r), step) * sign(r)
x0 <- x0 - d
Gvalue <- G(x0, z)
Gvaluevec <- c(Gvaluevec, Gvalue)
x0vec <- c(x0vec, x0)
count <- count + 1
if(count > 50)
stop("count > 50")
}
list(x0, x0vec, Gvaluevec, count)
}

Many thanks to all who replied.

_________________________________
Subject: Solving f(x)=0
Date: 3/11/98 9:14 AM

Dear S+ users,

Is anybody aware of a FAST function to solve numerically f(x)=0?

TIA