scipy fsolve对于输入中的几个值失败,以及如何改善求解器收敛 [英] scipy fsolve fails for few values in input, and how to improve solver convergence

查看:375
本文介绍了scipy fsolve对于输入中的几个值失败,以及如何改善求解器收敛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using scipy.optimize fsolve for finding roots of two equations. fsolve works well for some range of b values (0.1 to 0.6), but fails for values like 0.9 or 0.99.

我曾尝试移至Minimum_squares或最小化,但是在提供初始条件时出现了元组错误.

I have tried moving to least_squares or minimize, but get a tuple error whilst providing initial conditions.

包括来自以下发现的编辑都​​是自我的:

Including edits from below findings be self:

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的结果

Results with 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的结果

Results with 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

Works for b = 0.99 as well, but fails for b = 0.95. Incidentally works for b = 0.950001

推荐答案

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

Following initial condition seems to be working for majority of common cases:

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

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

Works for values till 0.999, still fails for 0.9999.

这篇关于scipy fsolve对于输入中的几个值失败,以及如何改善求解器收敛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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