简体   繁体   English

在R中使用二分法查找函数的根

[英]Finding the root of a function using the bisection method in R

I'm trying to find the root of the following function in R f <- x^3 + 2 * x^2 - 7 using the bisection method and the repeat function. 我正在尝试使用bisectionrepeat函数在R f <- x^3 + 2 * x^2 - 7找到以下函数的根。 This code results in an error: 此代码导致错误:

x <- 1.3
tolerance <- 0.000001

repeat {
  f <- x^3 + 2 * x^2 - 7
  if (abs(f) < tolerance) break
  x <- (x^3 + 2 * x^2 - 7)/2
}

Error in if (abs(f) < tolerance) break : 
  missing value where TRUE/FALSE needed

I've set the initial x to be 1.3, the tolerance to be 0.000001 and I know that the root lies between 1 and 2. I have already tried to substitute the last line of the code for f instead of retyping the function, but the same error appears. 我将初始x设置为1.3,将tolerance设置为0.000001,并且我知道根位于1和2之间。我已经尝试用代码的最后一行替换f,而不是重新键入函数,但是出现相同的错误。 Can someone help me? 有人能帮我吗?

Based on a very brief reading of the bisection method, I think you're adjusting x incorrectly. 基于对bisection法的简短阅读,我认为您错误地调整了x You should be bisecting the domain of x (the x value fed into f), not the range of f . 您应该平分x的域(馈入f的x值), 而不是f的范围

There are many reasons your function does not do what you want, but a primary one is that you are not even using the information you have about reasonable values for x, that is values of x that are near the root of the function. 有很多原因导致您的函数无法执行您想要的操作,但是一个主要的原因是您甚至没有使用有关x合理值(即x的值接近函数根)的信息。 You should never be setting your x value to some value of the function for which you are trying to find a root...there's no reason these two values need to be related. 永远不要将x值设置为要为其寻找根的函数的某个值...没有理由将这两个值关联起来。 For example, if the root of a function is near 100, the value of the function, f, for f(100) will be some low number. 例如,如果函数的根接近100,则f(100)的函数f的值将是一个较小的数字。 Then perhaps the value of f near 0 is some very high number. 那么也许f接近0的值是一个非常高的数字。 So if you start with f(x=100), you'll move x to around 0, then run f(0) and get some very big number so you'll move x to that big number, and so on. 因此,如果您以f(x = 100)开头,则将x移至0附近,然后运行f(0)并得到一个非常大的数字,以便将x移至该大数字,依此类推。 You'll be bouncing around according to f's values but not in a way that has anything to do with finding the root. 您将根据f的值来回弹跳,但不会与查找根有任何关系。

Let's try this: 让我们尝试一下:

x <- 1.3
tolerance <- 0.001

repeat {
  message(x)
  f <- x^3 + 2 * x^2 - 7
  if (abs(f) < tolerance) break
  x <- (x^3 + 2 * x^2 - 7)/2
  }
#1.3
#-0.7115
#-3.1738598729375
#-9.4123720943868
#-331.84120816267
#-18160827.4603406
#-2.99486226359293e+21
#-1.34307592742051e+64
#-1.21135573473788e+192
#Error in if (abs(f) < tolerance) break : 
#  missing value where TRUE/FALSE needed

As you see the x value becomes more and more negative until it's absolute value is too large for double: 如您所见, x值变得越来越负,直到它的绝对值对于double而言太大为止:

x <- -1.21135573473788e+192
x^3 + 2 * x^2 - 7
#[1] NaN

You should look up the algorithm of the bisection method, because what you have implemented here is clearly not the correct algorithm. 您应该查找二分法的算法,因为您在此处实现的显然不是正确的算法。

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

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