如何通过多个参数最小化函数?

问题描述 投票:0回答:1

我想通过 4 个参数

dchi(a, b, c, d)
a
b
c
来最小化函数
d

这行好像有错误
res = sp.optimize.minimize(dchi, first_guess)
,请问具体写错了什么以及如何修改?

import functools
import scipy as sp


def CH(chi: float, T: float, a: float, b: float, c: float, d: float, H: float, TC: float, C:float) -> float:
    return (a*(T - TC)*chi**(1/c)+ b * chi**(1/c) * chi**(1/d) * H**(1/d)- 1)+C/(T-TC)


exp=[[10.0052, 87.9716], [11.0433, 86.3866], [12.359, 84.8015], [169.852,   60.6291], [171.173, 60.6291], [172.494, 60.6291], [173.83,   60.6291]]
#print(exp[0][0])

def dchi(a, b, c, d):
    sd=0
    for i in range(len(exp)):
        CH_bound = functools.partial(CH, T=exp[i][0], a=a, b=b, c=c, d=d, H=315, TC=1000, C=2)
        ch1 = sp.optimize.root_scalar(f=CH_bound, x0=0)
        sd=sd+(ch1.root - exp[i][1]) ** 2
    return sd

#print(dchi(1,1,1,1))

first_guess = [1,1,1,1]
res = sp.optimize.minimize(dchi, first_guess)

#print(res)

错误:

Traceback (most recent call last):
  File "C:\Users\...\PycharmProjects\pythonProject6\main.py", line 23, in <module>
    res = sp.optimize.minimize(dchi, first_guess)
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_minimize.py", line 705, in minimize
    res = _minimize_bfgs(fun, x0, args, jac, callback, **options)
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_optimize.py", line 1419, in _minimize_bfgs
    sf = _prepare_scalar_function(fun, x0, jac, args=args, epsilon=eps,
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_optimize.py", line 383, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 158, in __init__
    self._update_fun()
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 251, in _update_fun
    self._update_fun_impl()
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 155, in update_fun
    self.f = fun_wrapped(self.x)
  File "C:\Users\...\PycharmProjects\pythonProject6\venv\lib\site-packages\scipy\optimize\_differentiable_functions.py", line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)
TypeError: dchi() missing 3 required positional arguments: 'b', 'c', and 'd'

Process finished with exit code 1
python scipy minimize minimization
1个回答
0
投票

要解决至少几个问题,请使用复杂的、正确解压参数,并检查根收敛:

import functools

import numpy as np
import scipy as sp


def CH(
    chi: float, T: float, a: float, b: float, c: float, d: float, H: float, TC: float, C: float,
) -> float:
    chi = chi + 0j
    err = (
        a*(T - TC)*chi**(1/c)
        + b * chi**(1/c) * chi**(1/d) * H**(1/d)
        + C/(T-TC)
        - 1
    )
    return np.abs(err)


exp = (
    (10.0052, 87.9716), (11.0433, 86.3866), (12.359, 84.8015),
    (169.852,   60.6291), (171.173, 60.6291), (172.494, 60.6291), (173.83,   60.6291),
)


def dchi(params: np.ndarray) -> float:
    a, b, c, d = params
    sd = 0
    x0 = 0
    for exp_a, exp_b in exp:
        CH_bound = functools.partial(CH, T=exp_a, a=a, b=b, c=c, d=d, H=315, TC=1000, C=2)
        ch1 = sp.optimize.root_scalar(
            f=CH_bound, x0=x0,
        )
        assert ch1.converged
        sd += (ch1.root - exp_b)**2
        x0 = ch1.root
    return sd


first_guess = (1,1,1,1)
res = sp.optimize.minimize(
    fun=dchi,
    x0=first_guess,
)
assert res.success, res.message
print(res.x)

这确实更进一步,但在上下文中你的函数是有问题的并且最终不会收敛。您显示的任何变量是否有界限?

© www.soinside.com 2019 - 2024. All rights reserved.