如何解决为什么 fsolve 给出初始条件作为解?
from scipy.optimize import fsolve
def f_sum(beta,gamma,x):
f = (
3 * x ** (3 * (1 - gamma)) / (np.exp(1 / beta) - x ** (1 - gamma))
+ x ** (4 * (1 - gamma)) / (np.exp(1 / beta) - x ** (1 - gamma)) ** 2
- 3 * x ** (3) / (np.exp(1 / beta) - x ** (1))
- x ** (4 * (1)) / (np.exp(1 / beta) - x ** (1)) ** 2
)
return f
def equilibrium(p,beta,x):
if x > 1:
return 1000
elif x < -1:
return -1000
else:
g = (
x * (1 - x ** 2) * (1 - p) / 2
+ f_sum(beta,(x + 1) / 2)
- f_sum(beta,(1 - x) / 2)
)
return g
print(fsolve(lambda x: equilibrium(1.0,0.1,0.3775,x),0.6))
我使用 fsolve 来找到严格介于 -1 和 1 之间的函数的根。x 介于 -1 和 1 之间。而 gamma 介于 0 和 0.5 之间。测试版为正。
我知道该函数有 3 个根 -1、0 和 1。我只是尝试看看是否有更多的特定参数组合。
但是,对于具体的参数,解与初始条件是一样的。我试图绘制函数,但没有这样的固定点,所以这显然是错误的。
我还收到以下错误:“RuntimeWarning:迭代没有取得良好进展,由 从最近十次迭代的改进。 warnings.warn(msg,RuntimeWarning)"
有什么问题吗?我还尝试更改所选参数的初始条件(在打印函数中),但高于 initial=0.6 时,它一直给出初始值。
解决方法
考虑到你的 equilibrium
函数的复杂性及其有限的域 x∈[-1,1],最好使用 least_squares
,例如
import numpy as np
from scipy.optimize import least_squares
def f_sum(x,beta,gamma):
f = (
3 * x ** (3 * (1 - gamma)) / (np.exp(1 / beta) - x ** (1 - gamma))
+ x ** (4 * (1 - gamma)) / (np.exp(1 / beta) - x ** (1 - gamma)) ** 2
- 3 * x ** (3) / (np.exp(1 / beta) - x ** (1))
- x ** (4 * (1)) / (np.exp(1 / beta) - x ** (1)) ** 2
)
return f
def equilibrium(x,p,gamma):
g = (
x * (1 - x ** 2) * (1 - p) / 2
+ f_sum((x + 1) / 2,gamma)
- f_sum((1 - x) / 2,gamma)
)
return g
现在我们可以在定义的域 (bounds
) 中找到根
roots = least_squares(
equilibrium,x0=(-1,-.5,.5,1),args=(2.5,4.4,0.06),bounds=(-1,1)
)
给予
print(roots)
active_mask: array([-1,1])
cost: 4.1713539926658053e-22
fun: array([-1.99740779e-11,-4.23644453e-12,0.00000000e+00,4.28902747e-12,1.99740779e-11])
grad: array([ 1.27723833e-10,-1.00057191e-12,1.01299110e-12,-1.27723833e-10])
jac: array([[-6.39447955,-0.,0.,-0. ],[ 0.,0.23618199,-0.12019887,0.236182,-6.39447955]])
message: '`gtol` termination condition is satisfied.'
nfev: 6
njev: 6
optimality: 1.358478265237574e-12
status: 1
success: True
x: array([-1.,-0.34105647,0.34105647,1. ])
根在哪里
print(roots.x)
[-1. -0.34105647 0. 0.34105647 1. ]
我们可以验证
import matplotlib.pyplot as plt
x = np.linspace(-1,1,1000)
y = equilibrium(x,2.5,0.06)
plt.plot(x,y)
plt.axhline(0,color='r',lw=1)
plt.vlines(roots.x,*plt.ylim(),color='k',ls='--')
固定根
即使您的函数 equilibrium
非常复杂,也相对容易证明(无需任何计算和/或估计)在域 x∈[-1,1] 中存在三个根 x={- 1,1} 独立于 beta 和 gamma。
其实我们可以写函数对相似词进行分组
这里,对于 p≠1,f(x)=0 的必要条件是 A=0 简单地导致
所以我们现在可以很容易地证明这些条件是唯一的根 f(x)=0 并且独立于 beta 和 gamma
所以,三个固定根为 x={-1,1},其中 f(x)=0 ∀β,γ
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。