在给定范围内找到函数的根 [英] Find a root of a function in a given range

查看:73
本文介绍了在给定范围内找到函数的根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组具有多个根的函数f_t(实际上是两个).我想找到第一个"根,并在大多数情况下使用fsolve可以正常工作.问题是,随着t趋于无穷大,两个根收敛. (我的函数的简单示例是f_t(x) = x^2 - 1/t).因此,t越大,fsolve犯的错误就越多.有一个预定义的函数,类似于fsolve,我可以告诉它应该只在给定范围内查找(例如,总是在[0, inf中找到根).

I have a set of functions f_t with several roots (two actually). I want to find the "first" root and doing this with fsolve works fine most of the time. The problem is, that the two roots converge, as t goes to infinity. (a simple exmple of my functions would be f_t(x) = x^2 - 1/t). So the larger t gets, the more mistakes fsolve makes. Is there a predefined function, similar to fsolve to which I can tell it should only look in a given range (e.g. find always the root in [0, inf)).

该问题与

The question is essentially the same as https://mathematica.stackexchange.com/questions/91784/how-to-find-numerically-all-roots-of-a-function-in-a-given-range?noredirect=1&lq=1, however the answers there are for Mathematica, I want them in Python.

PS:现在,我将如何编写自己的算法,但是由于这些方法与内置函数相比速度较慢,因此我希望找到一个具有相同功能的内置函数.特别是我已经阅读了这篇文章在给定间隔

PS: I now how I can write my own algorithm, but as these tend to be slower as builtins I was hoping to find a builtin that does the same. Especially I've read this post Find root of a function in a given interval

推荐答案

对于平滑,行为良好的功能,通常认为布伦特方法是保证扎根的最快方法.与列出的其他两种方法一样,您必须提供一个间隔[a,b],该间隔是函数连续的并更改符号.

It is generally accepted that for smooth, well-behaved functions, the Brent method is the fastest method guaranteed to give a root. As with the other two methods listed, you must provide an interval [a,b] across which the function is continuous and changes sign.

Scipy实现已记录在这里.您提到的功能的示例用例可能如下所示:

The Scipy implementation is documented here. An example use case for the function you mentioned could look like this:

from __future__ import division
import scipy

def func(x,t):
    return(x**2 - 1/t)

t0 = 1
min = 0
max = 100000 # set max to some sufficiently large value

root = scipy.optimize.brentq(func, min, max, args = (t0)) # args just supplies any extra
                                                       # argument for the function that isn't the varied parameter

这篇关于在给定范围内找到函数的根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆