fsolve multi variables help
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Emilio Lopez
am 13 Dez. 2017
Kommentiert: Emilio Lopez
am 14 Dez. 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 Kommentare
Akzeptierte Antwort
Star Strider
am 13 Dez. 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 Kommentare
Matt J
am 14 Dez. 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.
Weitere Antworten (1)
Matt J
am 13 Dez. 2017
Bearbeitet: Matt J
am 14 Dez. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Assembly finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!