【问题标题】:minimize a function that contains an array parameter最小化包含数组参数的函数
【发布时间】:2021-07-24 19:06:02
【问题描述】:

我正在尝试最小化包含数组参数的函数。

我试图在以下示例中简化我的问题。函数Func 包含参数c2 = linspace(-1,1,10)。优化是关于 thet1、phai1、thet2 和 phai2,使它们以区间 (0,2*pi) 为界。

import numpy as np
import scipy.optimize as spo

a1 = 0
c1 = 0
cc2 = np.linspace(-1,1,10)
alpha1 = 1+a1/3

def Func(ang):
    
    thet1 = ang[0]
    phai1 = ang[1]
    thet2 = ang[2]
    phai2 = ang[3]
    
    RhoABC = np.array([[[1-a1,0,thet1,0,0,0,0,c1],[0,alpha1,0,0,phai2,0,c2,0],[0,0,alpha1,0,0,c2,thet2,0],[0,0,0,alpha1,c2,0,0,0],[0,phai1,0,c2,alpha1,0,0,0],[0,0,c2,0,0,alpha1,0,thet2],[0,c2,0,0,0,0,alpha1,0],[c1,0,0,phai1,0,0,0,1-a1]] for c2 in cc2])   
    w, v = np.linalg.eig(RhoABC)  
    return w[1]

angs0 = [.4*np.pi,1.9*np.pi,1.2*np.pi,.7*np.pi]

bnd = (0*np.pi,2*np.pi)
bnds = (bnd,bnd,bnd,bnd)

result = spo.minimize(Func,angs0,bounds=bnds) 

输出是: ValueError:轴太多:2(effrank=2),预期排名=1

我只想将最小值(有趣)绘制为 c2 的函数。有什么建议吗?

【问题讨论】:

    标签: python minimize


    【解决方案1】:

    要最小化的函数应该返回一个浮点数(参见doc)。但是,Func 返回一维数组而不是浮点数。那是因为RhoABC 是3d,所以w 是2d,这使得w[1] 是1d 数组。

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 2017-08-18
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-17
      相关资源
      最近更新 更多