且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用numpy(和scipy)查找函数的全零?

更新时间:2023-02-26 15:32:13

我看到的主要问题是,您是否真的可以找到 all 的根源---如评论中已经提到的那样,这并不总是可能的.如果您确定您的功能不是完全病理性的(已经提到sin(1/x)),那么下一个就是您对缺少一个或多个根的容忍度是多少.换句话说,要准备多长时间才能确保您不会错过任何一个---据我所知,没有通用的方法可以为您隔离所有根,因此您必须自己做.您所显示的已经是一个合理的第一步.几点评论:

The main problem I see with this is if you can actually find all roots --- as have already been mentioned in comments, this is not always possible. If you are sure that your function is not completely pathological (sin(1/x) was already mentioned), the next one is what's your tolerance to missing a root or several of them. Put differently, it's about to what length you are prepared to go to make sure you did not miss any --- to the best of my knowledge, there is no general method to isolate all the roots for you, so you'll have to do it yourself. What you show is a reasonable first step already. A couple of comments:

    在这里,布伦特的方法确实是一个不错的选择.
  • 首先,应对分歧.由于在您的函数中分母中存在贝塞尔,因此您可以首先求解它们的根-***在例如Abramovitch和Stegun(
  • Brent's method is indeed a good choice here.
  • First of all, deal with the divergencies. Since in your function you have Bessels in the denominators, you can first solve for their roots -- better look them up in e.g., Abramovitch and Stegun (Mathworld link). This will be a better than using an ad hoc grid you're using.
  • What you can do, once you've found two roots or divergencies, x_1 and x_2, run the search again in the interval [x_1+epsilon, x_2-epsilon]. Continue until no more roots are found (Brent's method is guaranteed to converge to a root, provided there is one).
  • If you cannot enumerate all the divergencies, you might want to be a little more careful in verifying a candidate is indeed a divergency: given x don't just check that f(x) is large, check that, e.g. |f(x-epsilon/2)| > |f(x-epsilon)| for several values of epsilon (1e-8, 1e-9, 1e-10, something like that).
  • If you want to make sure you don't have roots which simply touch zero, look for the extrema of the function, and for each extremum, x_e, check the value of f(x_e).