# solve nonlinear equations

1 view (last 30 days)
john birt on 14 Mar 2011
Answered: VBBV on 14 Dec 2021
Im trying to solve 5 nonlinear equations with 5 unknowns, but im getting an error?
x0 = [-0.0018; 2.8914; 30.6782; 35.3729; 0.2481]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
[x,fval] = fsolve(@moments,x0,options) % Call solver
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
Error in ==> moments at 2
F = [2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1) + 0.000216678;
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
What is the error in moments.m at line 2? what did i do wrong?

Andrew Newell on 14 Mar 2011
The problem may be in the following line. Make sure it is just a scalar. To give an example,
F = [2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1) + 0.000216678;
x(1)];
will not trigger a vertcat error, but
F = [2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1) + 0.000216678;
x(1:2)];
will.

VBBV on 14 Dec 2021
x0 = [-0.0018; 2.8914; 30.6782; 35.3729; 0.2481]; % Make a starting guess at the solution
options=optimset('Display','iter'); % Option to display output
[x,fval] = fsolve(@moments,x0,options) % Call solver
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithm instead.
First-Order Norm of Iteration Func-count Residual optimality Lambda step 0 6 3.79659e-13 6.16e-07 0.01 1 12 3.65694e-17 6.05e-09 0.001 6.07416e-07 Equation solved, solver stalled. fsolve stopped because the relative size of the current step is less than the value of the step size tolerance and the vector of function values is near zero as measured by the value of the function tolerance.
x = 5×1
-0.0018 2.8914 30.6782 35.3729 0.2481
fval = -6.0473e-09
function F = moments(x)
F = 2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1) + 0.000216678;
end
Define the function with name moments
It seems you are calling it inside fmincon