fsolve multi variables help
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
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)];
Catégories
En savoir plus sur Assembly dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!