fsolve multi variables help

조회 수: 10 (최근 30일)
Emilio Lopez
Emilio Lopez 2017년 12월 13일
댓글: Emilio Lopez 2017년 12월 14일
I am trying to solve for angles in a system given 2 equations and 2 unknowns. First I defined ,y two functions in the form
function F=myfun(theta2, theta3, theta4)
F=[.2+.05.*cos(theta2)+.304.*cos(theta3)-.2.*cos(theta4);
.1+.05.*sin(theta2)+.304.*sin(theta3)-.2.*sin(theta4)];
end
then I attempt to solve for the unknowns using fsolve
theta3(1)=170.69;
theta4(1)=116.56;
x0=0
for theta2=-90:5:270;
options = optimset('Display','iter');
x=fsolve(@myfun,X0,options);
end
what am I doing wrong? is this even possible?

채택된 답변

Star Strider
Star Strider 2017년 12월 13일
I can’t figure out what you want to do.
The optimisation routines want the objective function argument to be a vector. The easiest way to do this with your function is to call it as:
@(b)myfun(b(1),b(2),b(3))
so:
x = fsolve(@(b)myfun(b(1),b(2),b(3)),X0,options);
with ‘X0’ now being a three-element vector.
I do not understand the loop. Because ‘X0’ never changes, the solution is the same for every iteration.
  댓글 수: 4
Matt J
Matt J 2017년 12월 14일
This is virtually the same as my solution already presented below. The only difference is that I do not use a fixed initial guess theta3 = 170.69, theta4 = 116.56 for every theta2(k1). It is inadvisable to do so. Much faster convergence and much better resilience to local min can be obtained by doing a coarse initial sweep of F.
Emilio Lopez
Emilio Lopez 2017년 12월 14일
you guys just saved my project grade, thanks! I could not figure it out.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2017년 12월 13일
편집: Matt J 2017년 12월 14일
I suspect you might be after something like this. Note that I changed all of your sin() and cos() to sind() and cosd() since it looks like you are measuring angles in degrees.
Theta2=(-90:5:270); N=numel(Theta2);
[Theta3,Theta4]=ndgrid(1:2:360);
for i=N:-1:1 %Get initial guesses through coarse sampling
Error = sum(abs( myfun(Theta2(i), Theta3(:).', Theta4(:).') ));
[minval,imin]=min(Error);
X0(i,:)=[Theta3(imin), Theta4(imin)];
end
options = optimset('Display','iter');
for i=N:-1:1 %Refine the initial guesses
thisFun=@(X) myfun(Theta2(i), X(1), X(2));
x(i,:)=fsolve(thisFun,X0(i,:),options);
end
function F=myfun(theta2, theta3, theta4)
F=[.2+.05.*cosd(theta2)+.304.*cosd(theta3)-.2.*cosd(theta4);
.1+.05.*sind(theta2)+.304.*sind(theta3)-.2.*sind(theta4)];

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by