[英]Python & SciPy - use fsolve with COM connection
I am trying to use scipy Optimize fsolve
function to find the zero of a function defined using a COM object. 我正在尝试使用scipy Optimize
fsolve
函数来查找使用COM对象定义的函数的零。
First, I create the COM object: 首先,我创建COM对象:
import win32com.client
os = win32com.client.Dispatch("PX32.OpenServer.1")
This COM object works as an interface to a program where I input variables, then make calculations, and obtain results, using the setvalue
, docommand
and getvalue
methods/functions. 这个COM对象用作程序的接口,在这里我使用
setvalue
, docommand
和getvalue
方法/函数输入变量,然后进行计算并获得结果。
If I define a function like: 如果我定义一个像这样的函数:
def fn(x):
os.setvalue("INPUT_VAR_STRING", x)
os.docommand("COMMAND_STRING")
return float(os.getvalue("RESULT_STRING"))
By changing the input value (passed into fn
through x
), I get different output values: 通过更改输入值(通过
x
传递到fn
),我得到了不同的输出值:
fn(100)
> 18.139818203
fn(190)
> -40.93182830
But, if I try to use fsolve
to find the root of the fn
function, I get an error: 但是,如果我尝试使用
fsolve
查找fn
函数的根,则会收到错误消息:
from scipy.optimize import fsolve
fsolve(fn,150)
> TypeError
> (...)
> TypeError: Internal error - the buffer length is not the sequence length!
What does this mean? 这是什么意思? How can I try to solve this error that has appeared?
如何解决已出现的错误?
I found the problem. 我发现了问题。 According to http://kitchingroup.cheme.cmu.edu/pycse/pycse.html ,
根据http://kitchingroup.cheme.cmu.edu/pycse/pycse.html ,
12.11 Using an external solver with Aspen (...)
12.11将外部求解器与Aspen(...)
flashT = float(flashT) # COM objects do not understand numpy types
That was the main thing. 那是主要的事情。 I had to change
我不得不改变
os.setvalue("INPUT_VAR_STRING", x)
for os.setvalue("INPUT_VAR_STRING", x)
用于
os.setvalue("INPUT_VAR_STRING", float(x))
to get the expected results. os.setvalue("INPUT_VAR_STRING", float(x))
获得预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.