非線性項組合的系統
對于系統中的每一個復雜擬合,即每一個殘差函數,都能表示為非線性方程的趨勢,例如較為復雜的系統函數組,
from optimtool.base import sp, np
x = sp.symbols("x1:5")
res1 = 0.5*x[0] + 0.2*x[1] + 1.5*x[0]**3 - 2*x[2] # Damped oscillator with nonlinear term
res2 = 3*x[0] + x[1]**2 - x[2]**2 - 0.1*np.random.normal() # Coupled oscillator system
res3 = x[2]*(1 - x[3]) - x[0]*x[1] + 1.5*sp.sin(x[3]) # Predator-prey like interaction
res4 = x[3]*(x[0] - x[1]) + 0.5*sp.exp(-x[2]) - 2.0*x[1] # Delay differential equation component
要解上述方程組,需要用到線搜索、非線性最小二乘、系統初始狀態,這些方法在optimtool的nonlinear_least_square模塊中能找到。在給定系統初始狀態為(1.0, 0.5, 0.2, 0.8),默認線搜索的前提下,優化的系統組是[res1, res2, res3, res4],使用如下指令調用levenberg_marquardt方法,
import optimtool.unconstrain as ou
ou.nonlinear_least_square.levenberg_marquardt([res1, res2, res3, res4], x, (1.0, 0.5, 0.2, 0.8), verbose=True, epsilon=1e-3)
系統內的迭代趨勢如下,一個系統的解在優化過程里的局部最優解可以有多個,這里給出的是系統達到收斂閾值的平滑區域的解,
(1.0, 0.5, 0.2, 0.8) 6.457126524403201 0
[ 0.40429326 0.43564665 -0.17927255 1.02536062] 1.7366068984048804 1
[0.1438984 0.25405175 0.10550085 0.32590509] 0.2107042535909221 2
[0.04771543 0.25630044 0.0574562 0.0532376 ] 0.014097257076394237 3
[ 0.02155738 0.24964789 0.0327689 -0.00291764] 0.0005100292092591925 4
[ 0.01767332 0.2456185 0.02926977 -0.01465774] 1.103580671559125e-05 5
[ 0.01711515 0.24490507 0.02880226 -0.01647978] 1.2913438683552677e-07 6
可視化圖例中迭代點快速收斂到局部最優的原因是wolfe線搜索方法和非線性系統的乘積組合funcs=(1/2)funcr.T*funcr,也是最小二乘法的線搜索基礎。