简体   繁体   English

R 中指数函数的求根

[英]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此函数有一个实根,即 $\sqrt(7)-2$ 或 0.6457513,如Stack Mathematics 中所述

if I try to find the root in R:如果我尝试在 R 中找到根:

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

the root that R reports me is not the correct one. R 报告我的根不是正确的根。

Any help why is that happening?任何帮助为什么会这样?

The uniroot solution is correct. uniroot解决方案是正确的。

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)

在此处输入图像描述

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:请注意,在 -1 和 0 之间的某处有一个单根。如果我们在 uniroot 解上画一条垂直线,它应该与该线穿过 x 轴的点重合:

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

在此处输入图像描述

Note that it's easy to confirm whether the value 0.6457513 is a root by passing it to your function.请注意,通过将值0.6457513传递给您的函数,很容易确认它是否为根。 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)如果您查看有关数学溢出的答案,它们会显示t = sqrt(7) - 2 ,但请记住t被替换为exp(x) ,因此exp(x) = sqrt(7) - 2 ,因此根在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM