MATLAB: System of nonlinear equations

fsolve

Dear all, I am trying to solve a system of 4 nonlinear equations. I act as below:
Creat a file as : equationssystem.m
The content of file is:
function F = equationssystem(x)
F = [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3) ...
-0.0004677*x(2)^2+0.6711*x(2)-158.14;...
2*x(1)*x(2)-2292.8*x(1)+x(3)-0.0009124*x(2)+0.6382;...
x(1)*x(4)^2+1314232.96*x(1)-2292.8*x(1)*x(4)+x(3)*x(4)-1146.4*x(3) ...
-0.001071*x(4)^2+1.939*x(4)-819.55;...
2*x(1)*x(4)-2292.8*x(1)+x(3)-0.002141*x(4)+1.941];
end
Then in command window, I write:
x0 = [0; 1200; -1; 1100];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@equationssystem,x0,options)
The initial guesses are close to the real results. But I am facing these errors:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in equationssystem (line 8)
F = [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3) …
Error in fsolve (line 219)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
I would really appreciate that if somebody help me to fix these errors and solve my equations since I have a huge sets of coefficients for which I have to solve these equations and I am a beginner in matlab.

Best Answer

The spaces and continuation ellipses in ‘equationssystem’ are the problem.
I slightly edited the function and defined it as an anonymous function to get:
equationssystem = @(x) [x(1)*x(2)^2+1314232.96*x(1)-2292.8*x(1)*x(2)+x(3)*x(2)-1146.4*x(3)-0.0004677*x(2)^2+0.6711*x(2)-158.14;
2*x(1)*x(2)-2292.8*x(1)+x(3)-0.0009124*x(2)+0.6382;
x(1)*x(4)^2+1314232.96*x(1)-2292.8*x(1)*x(4)+x(3)*x(4)-1146.4*x(3)-0.001071*x(4)^2+1.939*x(4)-819.55;
2*x(1)*x(4)-2292.8*x(1)+x(3)-0.002141*x(4)+1.941];
that then ran correctly:
x0 = [0; 1200; -1; 1100];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(equationssystem,x0,options)
to produce:
x =
-7.994193158578257e-02
1.139800767940268e+03
-6.533564950069347e-01
1.139198642923269e+03
fval =
-4.001776687800884e-11
-1.043609643147647e-14
-3.183231456205249e-11
-3.042011087472929e-14