matlab-fsolve函数求解多元非线性方程

记录一下代码,方便下次套用模板

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)。

如果是多个方程,一般是有联系的,求出一个之后靠着关系求别的方程未知数。

 

猜你喜欢

转载自www.cnblogs.com/shoulinniao/p/11440844.html