我用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!