1

当我尝试使用 scipy.optimize fmin 或 scipy.optimize minimize 最小化我的复杂函数时,我遇到了问题。事实上,最小化似乎不想考虑到我有一个复杂的功能这一事实。

我将我的代码分为两部分,第一部分是一个类,第二部分是我的主程序,它调用类并解决最小化问题。

这是我的课:

import sys,os
import numpy as np
import random, math
import matplotlib.pyplot as plt
import cmath

class real :
    def __init__(self):
        self.nmodes = 4
        self.L_ch = 1
        self.w = 2

    def minim(self,eps):
        self.T=np.array([[0.0,0.0],[0.00645-0.0012364j,0.0]])
        self.Id=np.array([[1,0],[0,1]])
        self.disp=np.linalg.det(self.T-self.Id)
        return self.disp

这是我的主要程序:

import sys,os
import numpy as np
import random, math, cmath
from math import exp
import scipy
from scipy.optimize import fmin

from Carlo import *
#################################
# Call the Class and the function
#################################
A=real()
eps=0.003+0.0042j
C=A.minim(eps)
#################################
# Declare some parameters and list
#################################
Cp=0
track=2
variable=np.arange(track,6,1)
Save=[]
Tau=4.1
Freal=12
Fimag=14j
#################################
# Loop for minimize
#################################
for track in variable:
    Cp+=1
    print Cp
    if Cp==1:
        part_real=0.0
        part_imag=0.0
    else:
        part_real=Save[leng-1]/2*3.14*track
        part_imag=Save[leng]/2*3.14*track
    R_0 = fmin(C,[part_real,part_imag])
    print 'La valeur de R_0',R_0
    Save.append(Tau)
    Save.append(Freal+1)
    Save.append(Fimag+1)
    leng = len(Save) -1

但我得到了这个错误:

Traceback (most recent call last):
  File "Main.py", line 40, in <module>
    R_0 = fmin(C,[part_real,part_imag])
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 360, in fmin
    res = _minimize_neldermead(func, x0, args, callback=callback, **opts)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 424, in _minimize_neldermead
    fsim[0] = func(x0)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/scipy/optimize/optimize.py", line 268, in function_wrapper
    return function(x, *args)
TypeError: 'numpy.complex128' object is not callable

有人知道它到底发生了什么吗?谢谢你。

4

1 回答 1

1

In the call: R_0 = fmin(C,[part_real,part_imag]) you should send a function as the first argument and it appears that C is not a function (which is why you get the error "object is not callable"):
scipy.linalg.det returns a float or complex Determinant of a matrix, see:

http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.det.html

于 2014-02-09T18:23:14.130 回答