MATLAB: Nonlinear equations & fsolve. Error “Failure in initial objective function evaluation. FSOLVE cannot continue.”

fevalfsolvefusernonlinear equations

Hello there, I'm trying to solve a set of nonlinear equations with fsolve (I'm quite new to Matlab and just tried this one out, not sure if fsolve fits my problem right, maybe someone has an even better idea?) and it returns the error shown above. This is my function:
function F = root2d(x)
Pabs=[100, 200, 400, 600, 800, 1000, 1200, 1400];
P_loss_PV2BAT=[81.1511, 90.8539, 109.6374, 127.5911, 144.7151,...
161.0094, 176.4739, 191.1088];
F(1)=Pabs(1)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(1)*(x(7)+x(8))-(P_loss_PV2BAT(1)*1000);
F(2)=Pabs(2)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(2)*(x(7)+x(8))-(P_loss_PV2BAT(2)*1000);
F(3)=Pabs(3)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(3)*(x(7)+x(8))-(P_loss_PV2BAT(3)*1000);
F(4)=Pabs(4)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(4)*(x(7)+x(8))-(P_loss_PV2BAT(4)*1000);
F(5)=Pabs(5)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(5)*(x(7)+x(8))-(P_loss_PV2BAT(5)*1000);
F(6)=Pabs(6)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(6)*(x(7)+x(8))-(P_loss_PV2BAT(6)*1000);
F(7)=Pabs(7)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(7)*(x(7)+x(8))-(P_loss_PV2BAT(7)*1000);
F(8)=Pabs(8)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Pabs(8)*(x(7)+x(8))-(P_loss_PV2BAT(8)*1000);
Out of the 8 unknowns, two are variables and 6 are coefficients. I didn't know how else to fit them into fsolve so I just made them all into variables… And thats how I call it:
fun=@root2d;
x0=[0,0];
options = optimoptions('fsolve','Display','iter');
x=fsolve(fun,x0,options)
There must already be something wrong with the function, because it keeps saying:
Index exceeds matrix dimensions.
Error in root2d (line 9)
F(1)=Pabs(1)^2*((x(1)/x(2))+(x(3)/x(4)))+x(5)*x(2)+x(6)*x(4)+...
Error in fsolve (line 230)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
It might also be that I'm completely wrong using fsolve here, but I don't know how else to solve this problem. Any ideas would be greaty appreciated!

Best Answer

x0 must have 8 elements, not 2.
Best wishes
Torsten.