求解非線性超定方程組,網上搜到的大多是線性方程組的最小二乘解法,對於非線性方程組無濟於事。
這里分享一種方法:SciPy庫的scipy.optimize.leastsq函數。
import numpy as np from scipy.optimize import leastsq from math import sqrt def func(i): x,y,z = i return np.asarray(( x**2-x*y+4, x**2+y**2-x*z-25, z**2-y*x+4, x**3+y**3+z**3-127.6 )) root = leastsq(func, np.asarray((1,1,1))) # 初始猜測值 print(root[0])
運行結果:
[ 1.00886951 5.00607313 1.036197 ]
缺點:只是普通的最小二乘解法,對於參數過於相近的情況,比如病態雅克比矩陣的求解效果不好。
有知道L-M算法(Levenberg-Marquardt algorithm)的朋友望告知。