Sagistic
Sagistic

Reputation: 37

Matlab: Nonlinear Eq: error

I have the code from a previous question, however, this one is similar, just more equations added. However, I get an error and I'm not sure how to fix it.

Link to my previous question: Matlab: Nonlinear equation solver

    function F = fcn(x)

    F=[x(6) +          x(7)         + x(8)         + x(9)        + x(10)         - 2   ;
       x(6)*x(1)    +  x(7)*x(2)    + x(8)*x(3)    + x(9)*x(4)   + x(10)*x(5)          ;
       x(6)*x(1)^2  +  x(7)*x(2)^2  + x(8)*x(3)^2  + x(9)*x(4)^2 + x(10)*x(5)    - 2/3 ;
       x(6)*x(1)^3  +  x(7)*x(2)^3  + x(8)*x(3)^3  + x(9)*x(4)^3 + x(10)*x(5)          ;
       x(6)*x(1)^4  +  x(7)*x(2)^4  + x(8)*x(3)^4  + x(9)*x(4)^4 + x(10)*x(5)     -2/5 ;
       x(6)*x(1)^5  +  x(7)*x(2)^5  + x(8)*x(3)^5  + x(9)*x(4)^5 + x(10)*x(5)          ;
       x(6)*x(1)^6  +  x(7)*x(2)^6  + x(8)*x(3)^6  + x(9)*x(4)^6 + x(10)*x(5)     -2/7 ;
       x(6)*x(1)^7  +  x(7)*x(2)^7  + x(8)*x(3)^7  + x(9)*x(4)^7 + x(10)*x(5)          ;
       x(6)*x(1)^8  +  x(7)*x(2)^8  + x(8)*x(3)^8  + x(9)*x(4)^8 + x(10)*x(5)     -2/9 ;
       x(6)*x(1)^9  +  x(7)*x(2)^9  + x(8)*x(3)^9  + x(9)*x(4)^9 + x(10)*x(5)          

       ];

    end




clc
clear all;
format long

x0  = [0.9; 0.5; 0.1; -0.5; -0.9; 0.2; 0.4; 0.5; 0.4; 0.2]; %Guess

F0 = fcn(x0);

[x,fval]=fsolve(@fcn, x0) %solve without optimization

options = optimset('MaxFunEvals',10000, 'MaxIter', 10000); %optimization criteria

[x,fval]=fsolve(@fcn, x0, options) %solve with optimization

The error that I get are:

Error using vertcat Dimensions of matrices being concatenated are not consistent.

Error in fcn(line 4) %This is from the function script F=[x(6) + x(7) + x(8) + x(9) + x(10) - 2 ;

Error in fcncall (line 7) %This is from the main script F0 = fcn(x0);

Upvotes: 0

Views: 70

Answers (2)

Daniel
Daniel

Reputation: 36710

That problem wasn't easy to find. Not using a blank before the minus at the end of the rows, you created a row with two elements instead of one. For a simplified example compare this:

>> [2 - 2]

ans =

     0

>> [2 -2]

ans =

     2    -2

>> [(2 -2)]

ans =

     0

Now the corrected version of your code:

  F=[x(6) +          x(7)         + x(8)         + x(9)        + x(10)         - 2   ;
   x(6)*x(1)    +  x(7)*x(2)    + x(8)*x(3)    + x(9)*x(4)   + x(10)*x(5)          ;
   x(6)*x(1)^2  +  x(7)*x(2)^2  + x(8)*x(3)^2  + x(9)*x(4)^2 + x(10)*x(5)    - 2/3 ; ...either set a space
   x(6)*x(1)^3  +  x(7)*x(2)^3  + x(8)*x(3)^3  + x(9)*x(4)^3 + x(10)*x(5)          ;
   (x(6)*x(1)^4  +  x(7)*x(2)^4  + x(8)*x(3)^4  + x(9)*x(4)^4 + x(10)*x(5)    -2/5); ...or use brackets
   x(6)*x(1)^5  +  x(7)*x(2)^5  + x(8)*x(3)^5  + x(9)*x(4)^5 + x(10)*x(5)          ; ...both fixes the problem
   x(6)*x(1)^6  +  x(7)*x(2)^6  + x(8)*x(3)^6  + x(9)*x(4)^6 + x(10)*x(5)    - 2/7 ;
   x(6)*x(1)^7  +  x(7)*x(2)^7  + x(8)*x(3)^7  + x(9)*x(4)^7 + x(10)*x(5)          ;
   x(6)*x(1)^8  +  x(7)*x(2)^8  + x(8)*x(3)^8  + x(9)*x(4)^8 + x(10)*x(5)    - 2/9 ;
   x(6)*x(1)^9  +  x(7)*x(2)^9  + x(8)*x(3)^9  + x(9)*x(4)^9 + x(10)*x(5)          ];

Upvotes: 1

TroyHaskin
TroyHaskin

Reputation: 8401

This is a very subtle error I've encountered before. When creating an array literal as you are doing fcn, negative signs that are directly attached to numbers and preceded by a space, as in rows 5, 7, and 9 of your array literal, are viewed as unary operators (i.e., the - makes the number negative and does not act as a binary minus operation). Therefore, because Matlab allows the delimiting of columns to be made with spaces, the indicated rows are interpreted to have two columns; row 5 column 1 is x(6)*x(1)^4 ... x(10)*x(5), and row 5 column 2 is -2/5.

So, either put a space between the three numbers or eliminate all spaces between the minus signs. For example:

x(6)*x(1)^4  +  x(7)*x(2)^4  + x(8)*x(3)^4  + x(9)*x(4)^4 + x(10)*x(5)     - 2/5;

or

x(6)*x(1)^4  +  x(7)*x(2)^4  + x(8)*x(3)^4  + x(9)*x(4)^4 + x(10)*x(5)-2/5;

Upvotes: 4

Related Questions