找到单调函数的根:牛顿失败

时间:2018-02-22 12:03:24

标签: python scipy

我有一个形状函数$$ f(x)= \ sum_i a_i \ cdot x ^ {e_i} - c $$,其中所有参数都是正数。 现在我想(数字地)计算这个函数的根。

  • f是单调的
  • $$ f(0)= -c $$
  • 所以根必须是正面的

我想应用牛顿的方法/割线方法,如scipy.optimize.newton,但有时会失败。

#secant method
f1 = (lambda a: 6.75304970913061 * a**2.37142857142857 - 1.91006495309903)
scipy.optimize.newton(f1,0)

无法在50个步骤中收敛,在100次或1000次迭代后,结果变得更糟(不应该发生)。

或者,我可以计算

#Newton
f2 = (lambda a: 0.672716686237341 * a **0.0624999999999993 + 0.87283444645141 * a ** 0.134615384615384 - 1.34775906114245)
f2prime = (lambda a: 0.0420447928898333 * a ** -0.937500000000001 + 0.117496944714613 * a ** -0.865384615384615)
scipy.optimize.newton(f2,1,fprime = f2prime)

由于我有负面的权力,我从1开始,但后来我得到Failed to converge after 50 iterations, value is (2.9502746750095213e+29-7.147769018388161e+29j)

我需要调用什么来解决上述类型的每个实例?

1 个答案:

答案 0 :(得分:2)

scipy.optimize.newton的SciPy文档建议在函数更改符号的区间[a,b]中使用scipy.optimize.brentq。对于如上所述的单调函数,可以通过尝试足够大的数字来找到a = 0和b。

{…}
pokemon: {…}
count: 949
next: "https://pokeapi.co/api/v2/pokemon/?limit=20&offset=20"
previous: null
results: […]
0: {…}
name: "bulbasaur"
url: "https://pokeapi.co/api/v2/pokemon/1/"
__proto__: Object { … }
1: {…}
name: "ivysaur"
url: "https://pokeapi.co/api/v2/pokemon/2/"
__proto__: Object { … }
2: {…}
name: "venusaur"
url: "https://pokeapi.co/api/v2/pokemon/3/"
__proto__: Object { … }
3: {…}
name: "charmander"
url: "https://pokeapi.co/api/v2/pokemon/4/"
__proto__: Object { … }
4: {…}
name: "charmeleon"
url: "https://pokeapi.co/api/v2/pokemon/5/"
__proto__: Object { … }
5: {…}
name: "charizard"
url: "https://pokeapi.co/api/v2/pokemon/6/"
__proto__: Object { … }
6: {…}
name: "squirtle"
url: "https://pokeapi.co/api/v2/pokemon/7/"
__proto__: Object { … }
7: {…}
name: "wartortle"
url: "https://pokeapi.co/api/v2/pokemon/8/"
__proto__: Object { … }
8: {…}
name: "blastoise"
url: "https://pokeapi.co/api/v2/pokemon/9/"
__proto__: Object { … }
9: {…}
name: "caterpie"
url: "https://pokeapi.co/api/v2/pokemon/10/"
__proto__: Object { … }
10: {…}
name: "metapod"
url: "https://pokeapi.co/api/v2/pokemon/11/"
__proto__: Object { … }
11: {…}
name: "butterfree"
url: "https://pokeapi.co/api/v2/pokemon/12/"
__proto__: Object { … }
12: {…}
name: "weedle"
url: "https://pokeapi.co/api/v2/pokemon/13/"
__proto__: Object { … }
13: {…}
name: "kakuna"
url: "https://pokeapi.co/api/v2/pokemon/14/"
__proto__: Object { … }
14: {…}
name: "beedrill"
url: "https://pokeapi.co/api/v2/pokemon/15/"
__proto__: Object { … }
15: {…}
name: "pidgey"
url: "https://pokeapi.co/api/v2/pokemon/16/"
__proto__: Object { … }
16: {…}
name: "pidgeotto"
url: "https://pokeapi.co/api/v2/pokemon/17/"
__proto__: Object { … }
17: {…}
name: "pidgeot"
url: "https://pokeapi.co/api/v2/pokemon/18/"
__proto__: Object { … }
18: {…}
name: "rattata"
url: "https://pokeapi.co/api/v2/pokemon/19/"
__proto__: Object { … }
19: {…}
name: "raticate"
url: "https://pokeapi.co/api/v2/pokemon/20/"
__proto__: Object { … }
length: 20
Error while processing route: pokemon Assertion Failed: You must include an 'id' for pokemon in an object passed to 'push'