标题不清楚,我希望在这里解释得更好:
i具有以下两个具有相同维度的数组ep
和sp
:
ep = [0.00000000e+00, 4.29973987e-05, 1.77977219e-04, 3.08940223e-04, 4.44883670e-04, 5.84806153e-04, 7.28705999e-04, 8.77580573e-04, 1.03342551e-03, 1.19623754e-03, 1.36301748e-03, 1.53675860e-03, 1.72145026e-03. 1.91608833e-03]
sp = [336.17311024, 366.02001118, 427.4927458, 471.53403676, 503.53359236, 527.23879184, 544.98822976, 558.34153011, 568.29913137, 575.9109472, 581.00400657, 584.97104685, 587.14272582, 587.92832846]
我需要根据以下公式获得数组sw
:
sw = (sp/ei^(ei+c)) * ((ep+ei)^(ei+c))
其中c
是重要的,ei
必须是最小化以下其他方程之和的值(在对sp和sw的每个值进行迭代之后):
f = (sp - sw)^2
任何想法?
谢谢!
这样的事情怎么样?您已经描述了错误功能,因此可以使用scipy.optimize.minimize
最小化它:
from scipy.optimize import minimize
ep = np.array([0.0000000e+00, 4.29973987e-05, 1.77977219e-04, 3.08940223e-04, 4.44883670e-04, 5.84806153e-04, 7.28705999e-04, 8.77580573e-04, 1.03342551e-03, 1.19623754e-03, 1.36301748e-03, 1.53675860e-03, 1.72145026e-03, 1.91608833e-03])
sp = np.array([336.17311024, 366.02001118, 427.4927458, 471.53403676, 503.53359236, 527.23879184, 544.98822976, 558.34153011, 568.29913137, 575.9109472, 581.00400657, 584.97104685, 587.14272582, 587.92832846])
def err(x):
ei, c = x
sw = (sp/ei**(ei+c))*((ep+ei)**(ei+c))
return np.sum((sp-sw)**2)
# do minimization
guess = (0.11, 0.19)
res = minimize(err, guess ,method='Nelder-Mead')
# get miniization result
ei, c = res.x
# plot results
fig, ax = plt.subplots(ncols=2)
ax[0].plot(sp)
ax[0].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)))
ax[0].set_title('Function evaluation')
ax[1].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)) - sp, label='Minimized')
ei, c = guess
ax[1].plot((sp/ei**(ei+c))*((ep+ei)**(ei+c)) - sp, label='Initial Guess')
ax[1].set_title('Difference')
ax[1].legend()