the fmincon sets a wrong dimension for my decision variables and that gets me errors on dimension disagreement.

2 views (last 30 days)
I have a nonlinear objective function with nonlinear constraints that I would like to minimize. I pass parameters and functions for the calculation of the objective function and constraints. So basically my fmincon looks like this:
[x,FVAL]=fmincon(@(x)myOF(x,PSR,PEBU,PWE,PLE,PAE,PHW,PHT,PMT,PST,PHR,PIVTT,PIVTQ,PIVTH,PIVTE,PIVTWE,PIVTLE,PIVTAE,PIVTHW,PCT,PICTE,PMSCp,POR,PER,PSEp,PICTWE,PICTLE,PICTAE,PICTHW,AHW,DHW,VHW,AWE,DWE,VWE,AAE,DAE,VAE,ALE,DLE,VLE,UP,I,J,K,T),x0,[],[],[],[],[],[],@(x)constraints(x,PSR,PEBU,PWE,PLE,PAE,PHW,PHT,PMT,PST,PIVTH,PIVTE,PIVTWE,PIVTLE,PIVTAE,PIVTHW,PCT,PICTE,PICTWE,PICTLE,PICTAE,PICTHW,IB,D,I,J,K,T),options);
'myOF' and 'constraints' are both functions of x and many parameters and the all the parameters are imported from excel. all the parameters are matrices of four dimensions.
I define my x0 to be a zero matrix with four dimensions but after I run the optimization I get error on dimension match because the x matrix is automatically set to be a 2 dimensional. when I run 'myOF' and 'constraints' manually I get no errors.. I am wondering how I can control the dimension of the final x matrix so that Matlab can continue the calculations without having problems with the non-matching dimensions.

Accepted Answer

Alan Weiss
Alan Weiss on 7 Apr 2014
fmincon takes the dimensionality of the problem from x0. Give the correct x0 and you will have no dimensionality problems.
Also, I suggest that you save yourself some writing and possible copy-paste errors by writing
fun = @(x)myOF(x,PSR,...); % The ... stand for the rest of the parameters
nonlcon = @(x)constraints(x,PSR,...)
and then call fmincon as
[x,FVAL] = fmincon(fun,x0,[],[],[],[],[],[],nonlcon,options)
Alan Weiss
MATLAB mathematical toolbox documentation
  3 Comments
Alan Weiss
Alan Weiss on 8 Apr 2014
I am surprised that you are having this problem. Perhaps you should contact technical support, in case this is a bug in Optimization Toolbox.
As a workaround, you can define x0 as a column vector
x0 = x0(:);
and then, inside your objective and nonlinear constraint functions, reshape x to the size you want
x = reshape(x,5,3,4,8);
Then at the end of your function files reshape it back to a column vector
x = x(:);
Alan Weiss
MATLAB mathematical toolbox documentation
saman Tayari
saman Tayari on 8 Apr 2014
This reshaping seems to be working. Though I need to make sure everything is in good shape after that doing that trick . Plus I will contact the technical support as the 5x96 dimension seems strange. I REALLY APPRECIATE YOUR HELP I felt so desperate for few days.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!