scipy fsolve对于输入中的几个值失败,以及如何改善求解器收敛

时间:2019-05-21 13:59:31

标签: python variables scipy equation

我正在使用scipy.optimize fsolve查找两个方程的根。 fsolve在一定范围的b值(0.1到0.6)下效果很好,但对于0.9或0.99则无效。

我曾尝试移至Minimum_squares或最小化,但在提供初始条件时遇到了元组错误。

包括来自以下调查结果的编辑都是自我的:

from scipy.optimize import fsolve
import scipy.stats as st
from numpy import *
import numpy as np



def rod(var1, var2, mu, sigma):
    return (st.lognorm.ppf(var1, s = sigma, scale = np.exp(mu), loc = sigma))/(st.lognorm.ppf(var2, s = sigma, scale = np.exp(mu), loc = sigma))
def fs_spfs(var1, mu, sigma):
    return (st.lognorm.ppf(var1, s = sigma, scale = np.exp(mu), loc = sigma))


a = 44.0
b = 0.5  #fsolve works for 0.5, 0.9, 0.99 but not for 0.95, incidentally works for 0.950001
c = 1.26

def f(x):
    y = np.zeros(2)
    y[0] = ((fs_spfs((1-b), x[0], x[1]) - a))
    y[1] = (((fs_spfs(0.9, x[0], x[1])/fs_spfs(0.1, x[0], x[1]))   - c))
    print(y)
    return y


x0 = np.array([1., 0.01])
solution = fsolve(f, x0)
print( "(x, y) = (" + str(solution[0]) + ", " + str(solution[1]) + ")")

b = 0.5的结果

b = 0.5

(x, y) = (3.7821340072441982, 0.09035467410258388)

fs_spfs((1-b), solution[0], solution[1]) # expected answer = 44.
43.99999999999982

rod(0.9, 0.1, solution[0], solution[1]) # exptected answer = 1.26
1.2599999999999958

b = 0.9的结果

b = 0.9

(x, y) = (3.8979025451494755, 0.09033430819655046)

fs_spfs((1-b), solution[0], solution[1]) # expected answer = 44.
43.999999999989164


rod(0.9, 0.1, solution[0], solution[1]) # exptected answer = 1.26
1.2600000000001814

同样适用于b = 0.99,但不适用于b = 0.95。顺带适用于b = 0.950001

1 个答案:

答案 0 :(得分:0)

在大多数常见情况下,遵循初始条件似乎是可行的:

x0 = np.array([0.000001, 0.0000001])

适用于0.999之前的值,但仍无法适用于0.9999。