記錄一下代碼,方便下次套用模板
options=optimset('MaxFunEvals',1e4,'MaxIter',1e4); [x,fval,exitflag] = fsolve(@(x) myfun1(x),[75;1.5],options) function f = myfun1(x) f=tan(x(1)*pi/180) - ( ( 1025*9.8*pi*x(2)/4-980 )/(0.625*4*(2-x(2))*24*24) );%有兩個未知數x(1)和x(2),從參數里傳進來 end
options理解成設定要求,精度范圍,沒有則默認,是多少問題不大。
[75;1.5]是x(1)和x(2)的初值,如果是同一個數不同初值則是[ 70 1;75 1.5 ],在初值附近找最優解。理解成:或許有多個最優解,如果初值不一樣,最優解也不一樣。非線性幾乎都是近似解。至於初值怎么設置,結合問題分析,比如桿子靠牆的傾斜角度大約在60度以上,而不是十幾二十度。
函數myfun1的求解情況是f=0。
fval表示誤差,越小越好。
exitflag表示迭代退出條件,為1的時候最理想。
1 fsolve converged to a root.
2 Change in X too small.
3 Change in residual norm too small.
4 Computed search direction too small.
0 Too many function evaluations or iterations.
-1 Stopped by output/plot function.
-2 Converged to a point that is not a root.
-3 Trust region radius too small (Trust-region-dogleg).
最終求出來兩個值,分別表示兩個未知數x(1)和x(2)。
如果是多個方程,一般是有聯系的,求出一個之后靠着關系求別的方程未知數。