简体   繁体   English

Python fsolve ValueError

[英]Python fsolve ValueError

Why does the following code return a ValueError? 为什么以下代码返回ValueError?

from scipy.optimize import fsolve
import numpy as np

def f(p,a=0):
    x,y = p
    return (np.dot(x,y)-a,np.outer(x,y)-np.ones((3,3)),x+y-np.array([1,2,3]))

x,y = fsolve(f,(np.ones(3),np.ones(3)),9)


ValueError: setting an array element with a sequence.

The basic problem here is that your function f does not satisfy the criteria required for fsolve to work. 这里的基本问题是您的函数f不满足fsolve所需的条件。 These criteria are described in the documentation - although arguably not very clearly. 这些标准在文档中进行了描述-尽管可以说不是很清楚。

The particular things that you need to be aware of are: 您需要注意的特定事项是:

  1. the input to the function that will be solved for must be an n-dimensional vector (referred to in the docs as ndarray ), such that the value of x you want is the solution to f(x, *args) = 0 . 要解决的函数的输入必须是n维向量(在文档中称为ndarray ),这样您想要的x的值就是f(x, *args) = 0
  2. the output of f must be the same shape as the x input to f . 的输出f必须是相同的形状x输入到f

Currently, your function takes a 2 member tuple of 1x3-arrays (in p ) and a fixed scalar offset (in a ). 当前,您的函数采用1x3-arrays的2个成员tuple (在p )和一个固定的标量偏移量(在a )。 It returns a 3 member tuple of types ( scalar , 3x3 array , 1x3 array ) 它返回一个3成员类型的tuplescalar3x3 array1x3 array

As you can see, neither condition 1 nor 2 is met. 如您所见,条件1和2都不满足。

It is hard to advise you on exactly how to fix this without being exactly sure of the equation you are trying to solve. 在不完全确定要解决的方程式的情况下,很难就如何解决此问题提供建议。 It seems you are trying to solve some particular equation f(x,y,a) = 0 for x and y with x0 = (1,1,1) and y0 = (1,1,1) and a = 9 as a fixed value. 似乎您正在尝试用x0 = (1,1,1)y0 = (1,1,1)a = 9作为a来求解xy某些特定方程f(x,y,a) = 0固定值。 You might be able to do this by passing in x and y concatenated (eg pass in p0 = (1,1,1,1,1,1) and in the function use x=p[:3] and y = p[3:] but then you must modify your function to output x and y concatenated into a 6-dimensional vector similarly. This depends on the exact function your are solving for and I can't work this out from the output of your existing f (ie based on a dot product, outer product and sum based tuple). 可能可以通过传递xy串联来实现此目的(例如,传递p0 = (1,1,1,1,1,1)并在函数中使用x=p[:3]y = p[3:]但是您必须修改函数以类似地输出x和y并连接成6维向量。这取决于您要求解的确切函数,而我无法从现有f (即基于点积,外部积和基于和的元组)。

Note that arguments that you don't pass in the vector (eg a in your case) will be treated as fixed values and won't be varied as part of the optimisation or returned as part of any solution. 需要注意的是不要在矢量传递参数(例如, a在你的情况下),将被视为固定值,不会被改变作为优化的一部分,或者返回任何解决方案的一部分。


Note for those who like the full story... 对于那些喜欢完整故事的人请注意...

As the docs say: 正如文档所说:

fsolve is a wrapper around MINPACK's hybrd and hybrj algorithms. fsolve是MINPACK的hybrd和hybrj算法的包装。

If we look at the MINPACK hybrd documentation , the conditions for the input and output vectors are more clearly stated. 如果我们查看MINPACK混合文档 ,则输入和输出向量的条件会更清楚地说明。 See the relevant bits below (I've cut some stuff out for clarity - indicated with ... - and added the comment to show that the input and output must be the same shape - indicated with <--) 请参阅下面的相关位(为了清楚起见,我已经删掉了一些内容-用...表示,并添加了注释以显示输入和输出必须具有相同的形状-用<-表示)

1 Purpose. 1。目的。

The purpose of HYBRD is to find a zero of a system of N non- linear functions in N variables by a modification of the Powell hybrid method. HYBRD的目的是通过修改Powell混合方法在N个变量中找到N个非线性函数系统的零点。 The user must provide a subroutine which calcu- lates the functions. 用户必须提供一个计算功能的子程序。 The Jacobian is then calculated by a for- ward-difference approximation. 然后通过前向差异近似来计算雅可比行列式。

2 Subroutine and type statements. 2子例程和类型语句。

  SUBROUTINE HYBRD(FCN,N,X, ... 

... ...

FCN is the name of the user-supplied subroutine which calculates the functions. FCN是用户提供的用于计算功能的子例程的名称。 FCN must be declared in an EXTERNAL statement in the user calling program, and should be written as follows. FCN必须在用户调用程序的EXTERNAL语句中声明,并且应编写如下。

  SUBROUTINE FCN(N,X,FVEC,IFLAG) INTEGER N,IFLAG DOUBLE PRECISION X(N),FVEC(N) <-- input X is an array length N, so is output FVEC ---------- CALCULATE THE FUNCTIONS AT X AND RETURN THIS VECTOR IN FVEC. ---------- RETURN END 

N is a positive integer input variable set to the number of functions and variables. N是设置为函数和变量数量的正整数输入变量。

X is an array of length N. On input X must contain an initial estimate of the solution vector. X是长度N的数组。在输入X上,必须包含解矢量的初始估计。 On output X contains the final estimate of the solution vector. 在输出X上包含解向量的最终估计值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM