我试图使用scipy Optimize fsolve
函数来查找使用COM对象定义的函数的零。Python&SciPy - 使用fsolve与COM连接
首先,我创建了COM对象:
import win32com.client
os = win32com.client.Dispatch("PX32.OpenServer.1")
此COM对象的工作为一个程序,我输入变量,然后进行计算,并获得结果的界面,使用setvalue
,docommand
和getvalue
方法/功能。
如果我这样定义一个函数:
def fn(x):
os.setvalue("INPUT_VAR_STRING", x)
os.docommand("COMMAND_STRING")
return float(os.getvalue("RESULT_STRING"))
通过改变输入值(通过x
传入fn
),我得到不同的输出值:
fn(100)
> 18.139818203
fn(190)
> -40.93182830
但是,如果我尝试使用fsolve
找到fn
函数的根,我得到一个错误:
from scipy.optimize import fsolve
fsolve(fn,150)
> TypeError
> (...)
> TypeError: Internal error - the buffer length is not the sequence length!
这是什么意思?我该如何尝试解决已经出现的这个错误?