[R] uniroot

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Fri Aug 27 20:52:24 CEST 2021


Hello,

Yes, it's FAQ 7.31 but it's not uniroot's fault.
The proper way of checking the result would be to call the function fun, 
not to take the digits output by the print method and compute the 
function's expression with them.



rui using rui:~$ R -q -f rhelp.R
fun <- function(x) {x^x -23}

# Clearly the root lies somewhere between 2.75 and 3.00
x0 <- uniroot(fun, lower = 2.75, upper = 3.00,  tol = 0.001)

# uniroot result
x0$f.root
#[1] 0.0001136763

# check the root, right
fun(x0$root)
#[1] 0.0001136763

# OP result, wrong
2.923125^2.923125 - 23
#[1] 0.0001222225


sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
  [1] LC_CTYPE=pt_PT.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=pt_PT.UTF-8        LC_COLLATE=pt_PT.UTF-8
  [5] LC_MONETARY=pt_PT.UTF-8    LC_MESSAGES=pt_PT.UTF-8
  [7] LC_PAPER=pt_PT.UTF-8       LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=pt_PT.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] compiler_4.1.1


Also, why change the default tol to a lesser one?


Hope this helps,

Rui Barradas


Às 18:42 de 27/08/21, Jeff Newmiller escreveu:
> Yes. This kind of issue is covered in any decent undergraduate course in numerical methods... it is not specific to R. It is also related to FAQ 7.31.
>   https://en.m.wikipedia.org/wiki/Root-finding_algorithms
> 
> https://en.m.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding
> 
> On August 27, 2021 10:30:38 AM PDT, Thomas Subia via R-help <r-help using r-project.org> wrote:
>> Colleagues,
>>
>> I've been using uniroot to identify a root of an equation.
>> As a check, I always verify that calculated root.
>> This is where I need some help.
>>
>> Consider the following script
>>
>> fun <- function(x) {x^x -23}
>>
>> # Clearly the root lies somewhere between 2.75 and 3.00
>>
>> uniroot(fun, lower = 2.75, upper = 3.00,  tol = 0.001)
>>
>> # output
>> $root
>> [1] 2.923125
>>
>> $f.root
>> [1] 0.0001136763
>>
>> # Let's verify this root.
>>
>> 2.923125^2.923125 - 23
>>
>> 0.0001222225
>>
>> This result is different than what was calculated with uniroot
>> 0.0001222225		# verified check using x = 2.923125
>> 0.0001136763		# using $f.root
>>
>> Does this imply that the root output of  2.923125 may need more significant
>> digits displayed?
>>
>> I suspect that whatever root is calculated, that root may well be dependent
>> on what interval one defines where the root may occur
>> and what tolerance one has input.
>> I am not sure that is the case, nevertheless, it's worth asking the
>> question.
>>
>> Some guidance would be appreciated.
>>
>> Thanks!
>>
>> Thomas Subia
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list