MATLAB: Solving a system of anonymous functions using fsolve

fsolvefunction handle

Hi,
I am having problems with the syntax of Matlab when calling fsolve for a system of anonymous functions:
I defined these functions:
C1=@(vet) (r1*cosd(vet(1)))+(r2*cosd(th2))+(r3*cosd(vet(2)))-(r4*cosd(vet(3)));
C2=@(vet)(r1*sind(vet(1)))+(r2*sind(th2))+(r3*sind(vet(2)))-(r4*sind(vet(3)));
C3=@(vet)(r5*cosd(vet(4)))+(r6*cosd(vet(5)))+((r7+vet(12))*cosd(vet(6)))-(r8*cosd(vet(7)));
C4=@(vet)(r5*sind(vet(4)))+(r6*sind(vet(5)))+((r7+vet(12))*sind(vet(6)))-(r8*sind(vet(7)));
C5=@(vet)(r9*cosd(vet(8)))+(r10*cosd(vet(9)))+(r11*cosd(vet(10)))-(r12*cosd(vet(11)));
C6=@(vet)(r9*sind(vet(8)))+(r10*sind(vet(9)))+(r11*sind(vet(10)))-(r12*sind(vet(11)));
C7=@(vet)(th2+gam1-vet(5));
C8=@(vet)(vet(3)+gam2-vet(9));
C9=@(vet)(vet(7)+gam3-vet(11));
C10=@(vet)(r9^2+r10^2+r12^2+(2*r9*r10*(((cosd((vet(8))-(vet(9)))))))-(2*r9*r12*(cosd((vet(8))-(vet(11)))))-(2*r10*r12*(cosd((vet(9))-(vet(11)))))-r11^2);
C11=@(vet)(r2^2+r1^2+r3^2-(2*r2*r1*(cosd((vet(1))-th2)))+(2*r1*r3(cosd((vet(1))-vet(2))))+(2*r2*r3*(cosd(th2-vet(2))))-r4^2);
C12=@(vet)(r5^2+r6^2+r8^2+(2*r
if I try to create the array containing all these equations
system=@(vet) [C1(vet),C2(vet),C3(vet),C4(vet),C5(vet),C6(vet),C7(vet),C8(vet),C9(vet),C10(vet),C11(vet),C12(vet)];
G=[15 20 25 30 35 40 45 50 55 60 65 70] %initial guess
and call fsolve to solve the system
H=@(vet) fsolve(system,G);
I keep getting the following error
"Non scalar arrays of function handles are not allowed; use cell arrays instead";
I looked on the forum for other similar problems but I wasn't able to replicate the solutions since I am having problems understanding some basics aspects of the logic behind the language, what do you think it's going on?

Best Answer

%initial values not specified, gotta invent some
rng(1234);
r1 = rand; r2 = rand; r3 = rand; r4 = rand; r5 = rand; r6 = rand; r7 = rand; r8 = rand; r9 = rand; r10 = rand; r11 = rand; r12 = rand; th2 = rand; gam1 = rand;
gam2 = rand; gam3 = rand;
C1=@(vet) (r1*cosd(vet(1)))+(r2*cosd(th2))+(r3*cosd(vet(2)))-(r4*cosd(vet(3)));
C2=@(vet)(r1*sind(vet(1)))+(r2*sind(th2))+(r3*sind(vet(2)))-(r4*sind(vet(3)));
C3=@(vet)(r5*cosd(vet(4)))+(r6*cosd(vet(5)))+((r7+vet(12))*cosd(vet(6)))-(r8*cosd(vet(7)));
C4=@(vet)(r5*sind(vet(4)))+(r6*sind(vet(5)))+((r7+vet(12))*sind(vet(6)))-(r8*sind(vet(7)));
C5=@(vet)(r9*cosd(vet(8)))+(r10*cosd(vet(9)))+(r11*cosd(vet(10)))-(r12*cosd(vet(11)));
C6=@(vet)(r9*sind(vet(8)))+(r10*sind(vet(9)))+(r11*sind(vet(10)))-(r12*sind(vet(11)));
C7=@(vet)(th2+gam1-vet(5));
C8=@(vet)(vet(3)+gam2-vet(9));
C9=@(vet)(vet(7)+gam3-vet(11));
C10=@(vet)(r9^2+r10^2+r12^2+(2*r9*r10*(((cosd((vet(8))-(vet(9)))))))-(2*r9*r12*(cosd((vet(8))-(vet(11)))))-(2*r10*r12*(cosd((vet(9))-(vet(11)))))-r11^2);
C11=@(vet)(r2^2+r1^2+r3^2-(2*r2*r1*(cosd((vet(1))-th2)))+(2*r1*r3*(cosd((vet(1))-vet(2))))+(2*r2*r3*(cosd(th2-vet(2))))-r4^2);
%posted C12 was corrupt. Substitute something we know can be satisfied
% C12=@(vet)(r5^2+r6^2+r8^2+(2*r %FIXME
C12=@(vet) 0;
%don't call it system, that interfers with the system() function and MATLAB will whine
sys = @(vet) [C1(vet),C2(vet),C3(vet),C4(vet),C5(vet),C6(vet),C7(vet),C8(vet),C9(vet),C10(vet),C11(vet),C12(vet)];
G = [15 20 25 30 35 40 45 50 55 60 65 70]; %initial guess
%with these initial values it takes lots of iterations to converge
opt = optimoptions('fsolve','MaxFunctionEvaluations', 1e7, 'MaxIterations', 1e6);
[H, fval] = fsolve(sys, G, opt);
disp(H)
disp(fval)
Related Question