[R] Problem : solving a equation with R , fail with uniroot function

Ben Bolker bolker at ufl.edu
Wed Jun 10 15:28:05 CEST 2009




yann lancien wrote:
> 
> Thanks for your help !
> I got another problem with my function :
> test <- function(x,bb0=-3,bb1=5,c0=2,r0=0) {
>   ((exp(c0-r0)*(bb0+x)*(bb1-x))/((bb0+x+1)*(bb1-x-1))-1)}
> 
> With this :
> curve(test(x),from=-10,to=10)
> 
> we can see that there are 2 roots : one in [3,4] and one in [5,10]
>> uniroot(test,c(5, 10))$root
> [1] 5.243265
>> uniroot(test,c(3, 4))$root
> [1] 3.069772
> 
> Even this works :
>> uniroot(test,c(3, 10))$root
> [1] 5.243263
> 
> but it's unfortunate that :
>> uniroot(test3,c(-10, 10))$root
> *Erreur dans uniroot(test3, c(-10, 10)) :
>   les valeurs de f() aux points extrêmes ne sont pas de signe opposé*
> doesn't work, because I have to find the root for c0 in [-10,10] but I
> can't
> change the range at each loop:
> 
> I want to do something like this, do you have any ideas ?
> 
>> for (i in -10:10) {
> + z <- uniroot(test,c(-10, 10), c0=i)$root
> + print(z)}
> 
> Yann
> 

This is the point at which somebody's (I forget whose) R-help signature
comes to mind: "what is the problem you are trying to solve?
You could develop an elaborate brute-force solution that combined
try() [to catch the function when it doesn't work] and attempts to
find roots in (for example) [-10,-9],[-9,-8],[-8,-7], ... [9,10] -- but
how are you going to decide which root you want when there is
more than one?  I'm afraid this
is no longer an R problem ... you might have to go back and re-think
your approach to the problem ...

  Ben Bolker
-- 
View this message in context: http://www.nabble.com/Problem-%3A-solving-a-equation-with-R-%2C-fail-with-uniroot-function-tp23953174p23962342.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list