[R] How to solve difficult equations?

ken knoblauch knoblauch at lyon.inserm.fr
Wed Apr 25 09:25:51 CEST 2007


I don't see the problem, except that you might want to think about
what the error message is telling you.

A little exploration of your function always helps, too.

 > ss <- seq(-2, 2, len = 100)
 > plot(ss, fn(ss), type = "l")
 > uniroot(fn, c(-1, 1))
Erreur dans uniroot(fn, c(-1, 1)) : f() values at end points not of 
opposite sign


 > fn(-1)
[1] 3.330833
 > fn(0)
[1] -0.0025
 > fn(1)
[1] 0.5857353
 > uniroot(fn, c(-1, 0))
$root
[1] -0.6999466

$f.root
[1] -13118.83

$iter
[1] 18

$estim.prec
[1] 7.70751e-05

 > uniroot(fn, c(0, 1))
$root
[1] 0.001760625

$f.root
[1] 8.86832e-06

$iter
[1] 3

$estim.prec
[1] 6.103516e-05


> This below is not solvable with uniroot to find "a":
> fn=function(a){
> b=(0.7/a)-a
> (1/(a+b+1))-0.0025
> }
> uniroot(fn,c(-500,500))  gives
> "Error in uniroot(fn, c(-500, 500)) : f() values at end points not of
> opposite sign"
>
> I read R-help posts and someone wrote a function:
> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/92407.html
> but it is not very precise. Is there any '"standard" function in R 
> that can
> solve this? thanks.



More information about the R-help mailing list