Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

root finding in R for exponential function

I have a function with exponentials :

f = function(x){
  exp(3*x)+4*exp(2*x)-3*exp(x)
}

this function has one real root which is $\sqrt(7)-2$ or 0.6457513 as discussed here in Stack Mathematics

if I try to find the root in R :

uniroot(f, lower = -3, upper = 3)$root
[1] -0.4373415

the root that R reports me is not the correct one.

Any help why is that happening ?

like image 449
Homer Jay Simpson Avatar asked Sep 15 '26 17:09

Homer Jay Simpson


1 Answers

The uniroot solution is correct.

We can see this by plotting your function:

f = function(x){
  exp(3*x) + 4*exp(2*x) - 3*exp(x)
}

plot(f, xlim = c(-3, 0))
abline(h = 0, lty = 2)

enter image description here

Note that there is a single root somewhere between -1 and 0. If we draw a vertical line at the uniroot solution, it should coincide with the point where the line crosses the x axis:

abline(v = uniroot(f, lower = -1, upper = 0)$root, lty = 2)

enter image description here

Note that it's easy to confirm whether the value 0.6457513 is a root by passing it to your function. This returns:

f(0.6457513)
#> [1] 15.77041

Which tells us this is not a root.

If you look at the answer on math overflow, they show that t = sqrt(7) - 2, but remember t was substituted for exp(x), so exp(x) = sqrt(7) - 2, and therefore the root is at x = log(sqrt(7) - 2)

log(sqrt(7) - 2)
#> [1] -0.4373408

If we feed this number into your function we can see that it is indeed a root (within the limits of floating point arithmetic)

f(log(sqrt(7) - 2))
#> [1] 4.440892e-16
like image 98
Allan Cameron Avatar answered Sep 18 '26 08:09

Allan Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!