MATLAB: Problem with fsolve

fsolveMATLAB

hi guys, I'm trying to solve a 3 equation non-linear system with fsolve, but it always give me an error when i run it. I have F cell of 3 function handle, and that's my input with fsolve. The functions work properly (I tryed to give a random vector of solutions and they gave me a result).
sol0=[0.5 0.3 0.3]
f1=@(sol) exp((P./(R.*T)).*(B(1,1)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
f2=@(sol) exp((P./(R.*T)).*(B(2,2)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
f3=@(sol) exp((P./(R.*T)).*(B(3,3)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
g1=@(sol) exp((1./(R.*T)).*(a./(1+(a.*sol(1)./(b.*(1-sol(1))))).^2));
g2=@(sol) exp((1./(R.*T)).*(b./(1+(b.*(1-sol(1))./(a.*sol(1)))).^2));
fun1=@(sol) (g1(sol).*sol(1).*Ps1)-(f1(sol).*sol(2).*P);
fun2=@(sol) (g2(sol).*(1-sol(1)).*Ps2)-(f2(sol).*sol(3).*P);
fun3=@(sol) (K.*f3(sol).*f2(sol))-f1(sol);
sol=[0 0 0]
F={fun1 fun2 fun3}
sol=fsolve(F,sol0)

Best Answer

After I created values for all the constants (it would have been nice to have had those), this ran for me without error:
F = @(sol) [fun1(sol), fun2(sol), fun3(sol)]
sol=fsolve(F,sol0)
Defining ‘F’ as a cell array is a problem, because then the elements are elements of a cell array, and fsolve objects to that. Defining ‘F’ as a matrix and as its own anonymous function seems to work.