fsolve multi variables help
38 views (last 30 days)
Show older comments
Emilio Lopez
on 13 Dec 2017
Commented: Emilio Lopez
on 14 Dec 2017
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?
0 Comments
Accepted Answer
Star Strider
on 13 Dec 2017
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 Comments
Matt J
on 14 Dec 2017
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.
More Answers (1)
Matt J
on 13 Dec 2017
Edited: Matt J
on 14 Dec 2017
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)];
0 Comments
See Also
Categories
Find more on General Applications in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!