# solve nonlinear equations

3 views (last 30 days)
john birt on 14 Mar 2011
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?
The m file, moments.m is
function F = moments(x)
F = [2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1) + 0.000216678;
2*x(2)*x(5)*(x(3)^((x(5)-1)/x(5)))-4*(x(4)^2)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1)+(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))^2 - 0.000045035;
-12*x(4)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1)+8*(x(4)^3)*(x(3)^((x(5)-3)/x(5)))*x(2)*x(5)*(x(5)-1)*(x(5)-2)+3*(2*x(2)*x(5)*(x(3)^((x(5)-1)/x(5)))-4*(x(4)^2)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1))+(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))^3 +0.00000020584664 ;
-12*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1)+48*x(4)^2*(x(3)^((x(5)-3)/x(5)))*x(2)*x(5)*(x(5)-1)*(x(5)-2)-16*x(4)^4*(x(3)^((x(5)-4)/x(5)))*x(2)*x(5)*(x(5)-1)*(x(5)-2)*(x(5)-3)+3*(2*x(2)*x(5)*(x(3)^((x(5)-1)/x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x(5)*(x(3)^((x(5)-1)/x(5)))-4*(x(4)^2)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1))^2*(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))+10*(-12*x(4)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1)+8*(x(4)^3)*(x(3)^((x(5)-3)/x(5)))*x(2)*x(5)*(x(5)-1)*(x(5)-2))*(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))^2+10*(2*x(2)*x(5)*(x(3)^((x(5)-1)/x(5)))-4*(x(4)^2)*(x(3)^((x(5)-2)/x(5)))*x(2)*x(5)*(x(5)-1))*(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))^3+(2*x(4)*x(2)*x(5)*(x(3)^((x(5)-1)/x(5))) + x(1))^5 + 0.000000000396254743];

Matt Tearle on 14 Mar 2011
Believe it or not, you didn't put a space before the constant in the 3rd and 4th elements of F. Notice that they both end in blahblah -3.1415926
MATLAB is interpreting that space before the minus sign (and lack of space after it) as two elements on that row -- ie equivalent to blahblah,-3.1415926. Take out the space before the minus sign or add one after it (before the constant).
BTW, rather than letting fsolve find the error, do a quick check by calling moments directly from the Command Window: moments([1;2;3;4;5])