Dimension issues when using fsolve

3 Ansichten (letzte 30 Tage)
Kyriacos
Kyriacos am 25 Nov. 2011
Kommentiert: Ning Wu am 22 Sep. 2015
Hello everyone, I face some "dimension issues" when using fsolve. Thus, I tried to use the exact example in the matlab help: http://www.mathworks.fr/help/toolbox/optim/ug/fsolve.html (example 1). Following the example, my initial condition is always given by:
x0 = [-5; -5];
After playing around a bit, I discovered that when I put the function F as follows:
F = [2*x(1) - x(2) - exp(-x(1)); -x(1) +2*x(2) - exp(-x(2))];
I get the error message: ??? Error using ==> vertcat CAT arguments dimensions are not consistent.
When I type:
F = [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))];
it works! The difference? In the second case, there is a space between the "+" and 2*x(2). First, I do not understand why these two ways of typing F should be any different. Second, it seems that fsolve is sensitive to dimensions. What should I be careful of when using it?
Thanks a lot!
Kyriacos
  1 Kommentar
Ning  Wu
Ning Wu am 22 Sep. 2015
I had the same problem when using fsolve. Titus is correct on this. To avoid this problem, just set tp1=2*x(1) - x(2) - exp(-x(1)); tp2=-x(1) +2*x(2) - exp(-x(2)); and write F=[tp1;tp2]

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Titus Edelhofer
Titus Edelhofer am 25 Nov. 2011
Hi Kyriacos,
if you leave out the first half of F you see the difference:
F = [-x(1) +2*x(2) - exp(-x(2))]
F =
5.0000 -158.4132
F = [-x(1) + 2*x(2) - exp(-x(2))]
F =
-153.4132
In the first case the + is interpreted as a unary plus (like if you write -2).Therefore you get the error because you can't combine one number and a vector of length 2 into a matrix.
Titus
  1 Kommentar
Titus Edelhofer
Titus Edelhofer am 25 Nov. 2011
Forgot to mention: although generally speaking spaces make code easier to read, this is a good example of being careful to use spaces in expressions using [], because space works as column separator.

Melden Sie sich an, um zu kommentieren.


Kyriacos
Kyriacos am 25 Nov. 2011
Hi, Thanks for your reply. I am not sure I understand well what you mean by "unary plus", Can you please elaborate a bit more?
Also, regarding dimensions: my system is one of 21 equations and 21 unknowns. The way I do it, I write F by separating each equation by a ";" which is like changing a line in the vector of outputs right? So I am not sure how leaving a space between expressions works here, since as you say it is interpreted as a column operator but for me both my initial conditions and the output are a column-vector x of dimension 21.
I am giving you the whole code to be a bit more clear.
function f = myss(x)
is=0.15 ; theta=0.5 ; rho = 0.44 ; yT_y = 0.5; yN_y = 1-yT_y ; cT_c = 0.43 ; cN_c = 1-cT_c ; beta = 1/1.04 ; delta = 0.1 ; alphaT = (1-0.61) ; alphaN = (1-0.56) ;
%x0 = [1; 1; 1; 1; 1; 1; 1; 1; 1; 0.5; 0.5; 1; 1; 1; 0.2; 0.2; 0.2; 1; 1; 1; 1; ] ;
f = [x(13) - 1 ; x(2) - x(8) - x(9) ; x(3) - x(6) ; x(10)*x(2)/x(1) - yT_y ; x(11)*x(9)/x(1) - is ; x(5) + x(7) - x(10)*(x(8) + x(9)) ; x(5) + x(7) - (x(15)*x(8)^((theta-1)/theta) + (1-x(15))*x(9)^((theta-1)/theta))^(theta/(theta-1)) ; x(11) - x(10) ; x(14) - x(11)/x(10) ; x(5)/(x(14)*x(4)) - cT_c ; x(13)*x(6)/(x(14)*x(4)) - cN_c ; x(13) - ((1 - x(16))/x(16))*(x(5)/x(6))^(1/rho) ; x(4) - (x(16)*x(5)^((rho-1)/rho) + (1 - x(16))*x(6)^((rho-1)/rho))^(rho/(rho-1)) ; x(14)*x(4) - x(5) - x(13)*x(6) ; x(17) - x(15)*(x(8)^(-1/theta))*(x(15)*x(8)^((theta-1)/theta) + (1 - x(15))*x(9)^((theta-1)/theta))^(1/(theta-1)) ; x(2)/x(19)*beta*alphaT*x(17) - (1-beta*(1-delta)) ; x(18) - (x(16)/(1 - x(16)))*(x(5)/x(6))^(-1/rho) ; x(3)/x(20)*alphaN*beta - (1-beta*(1-delta))*x(18) ; x(21) - x(19) - x(20) ; x(7)/x(21) - delta ; x(1) - (x(10)*x(2) + x(13)*x(3)) ];
Then, I run it as follows: [x,fval, exitflag] = fsolve(@myss,x0)
Btw, it does not converge but I am currently working on it.
Thanks again!
Kyriacos

Titus Edelhofer
Titus Edelhofer am 25 Nov. 2011
Hi Kyriacos,
unary plus is similar to unary minus: instead of
x = 42.0;
you can just as well write
x = +42.0;
Having said this, MATLAB interprets
F = [2.0 +3.0]
equally to
F = [2.0 3.0]
instead of (what you wanted to have)
F = [2.0+3.0]
My recommendation for making your code somewhat more robust and easier to read: seperate the individual functions
f = zeros(21, 1);
f(1) = x(13) - 1;
f(2) = x(2) - x(8) - x(9);
f(3) = x(3) - x(6);
...
or (doing basically the same):
f1 = x(13) - 1;
f2 = x(2) - x(8) - x(9);
f3 = x(3) - x(6);
...
f = [f1; f2; f3; ...]
Hope this helps,
Titus

Kategorien

Mehr zu Linear Programming and Mixed-Integer Linear Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by