我用fsolve算非线性方程组出错,求大神指教。

6 次查看(过去 30 天)
nogak
nogak 2022-11-17
回答: tilojej 2022-11-17
function F=answer(x)
Ya=-300;Yb=300;Yc=-500;Zc=-450;Yd=500;Zd=-450;Theta=8;
F=[(x(1)-Ya)^2+x(2)^2-((Yc-Ya)^2+Zc^2);(x(3)-Yb)^2+x(4)^2-((Yd-Yb)^2+Zd^2);(x(1)-x(3))^2+(x(2)-x(4))^2-((Yc-Yd)^2+(Zc-Zd)^2);(x(2)-x(4))/(x(1)-x(3))-(tan(180*Theta/pi))];
end
x0=[1;2;3]
options=optimset('Display','iter');
[x,fval]=fsolve(@answer,x0,options)
Attempted to access x(4); index out of bounds because numel(x)=3.
Error in answer (line 3)
F=[(x(1)-Ya)^2+x(2)^2-((Yc-Ya)^2+Zc^2);(x(3)-Yb)^2+x(4)^2-((Yd-Yb)^2+Zd^2);(x(1)-x(3))^2+(x(2)-x(4))^2-((Yc-Yd)^2+(Zc-Zd)^2);(x(2)-x(4))/(x(1)-x(3))-(tan(180*Theta/pi))];
Error in fsolve (line 218)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
如题,不知错误在哪里。。谢谢!

采纳的回答

tilojej
tilojej 2022-11-17
方程中有4个未知数,那你初值对应应该给4个:
function main
x0=[1;2;3;41111]
options=optimset('Display','iter');
[x,fval]=fsolve(@answer,x0,options)
end
function F=answer(x)
Ya=-300;Yb=300;Yc=-500;Zc=-450;Yd=500;Zd=-450;Theta=8;
F=[(x(1)-Ya)^2+x(2)^2-((Yc-Ya)^2+Zc^2);(x(3)-Yb)^2+x(4)^2-((Yd-Yb)^2+Zd^2);(x(1)-x(3))^2+(x(2)-x(4))^2-((Yc-Yd)^2+(Zc-Zd)^2);(x(2)-x(4))/(x(1)-x(3))-(tan(180*Theta/pi))];
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!