10分钟
求解非线性方程组
求解非线性方程组:
scipy.optimize.fsolve(func, x0, args=(), fprime=None, full_output=0, col_deriv=0,
xtol=1.49012e-08, maxfev=0, band=None, epsfcn=None, factor=100, diag=None)
func
:是个可调用对象,它代表了非线性方程组。给他传入方程组的各个参数,它返回各个方程残差(残差为零则表示找到了根)x0
:预设的方程的根的初始值args
:一个元组,用于给func
提供额外的参数。fprime
:用于计算func
的雅可比矩阵(按行排列)。默认情况下,算法自动推算full_output
:如果为True
,则给出更详细的输出col_deriv
:如果为True
,则计算雅可比矩阵更快(按列求导)xtol
:指定算法收敛的阈值。当误差小于该阈值时,算法停止迭代maxfev
:设定算法迭代的最大次数。如果为零,则为100*(N+1)
,N
为x0
的长度band
:If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for fprime=None)epsfcn
:采用前向差分算法求解雅可比矩阵时的步长。factor
:它决定了初始的步长diag
:它给出了每个变量的缩放因子
返回值:
x
:方程组的根组成的数组infodict
:给出了可选的输出。它是个字典,其中的键有:nfev
:func
调用次数njev
:雅可比函数调用的次数fvec
:最终的func
输出fjac
:the orthogonal matrix, q, produced by the QR factorization of the final approximate Jacobian matrix, stored column wiser
:upper triangular matrix produced by QR factorization of the same matrix
ier
:一个整数标记。如果为 1,则表示根求解成功mesg
:一个字符串。如果根未找到,则它给出详细信息
假设待求解的方程组为:
那么我们的func
函数为:
def func(x):
x1,x2,x3=x.tolist() # x 为向量,形状为 (3,)
return np.array([f1(x1,x2,x3),f2(x1,x2,x3),f3(x1,x2,x3)])
数组的
.tolist()
方法能获得标准的python
列表
而雅可比矩阵为:
def fprime(x):
x1,x2,x3=x.tolist() # x 为向量,形状为 (3,)
return np.array([[df1/dx1,df1/dx2,df1/df3],
[df2/dx1,df2/dx2,df2/df3],
[df3/dx1,df3/dx2,df3/df3]])
学员评价