From: diana van dijk on
Hi,

I'm trying to use the fmincon function for 2 variables vec1 and vec2:

Code:
global par1 par2 par3 par4 par5 par6 par7 vec1 vec2
vec1 = linspace(.001, 2, 200)
vec2 = linspace(.001, .5, 50)
mat1 = zeros( length(vec1), length(vec3) )

%bounds: 0 < vec1 < z
%bounds: -inf < vec2 < inf

for i = 1:length(vec1)
for j = 1:length(vec2)
z = vec1(i);
w = vec2(j);
[SS MAT] = fmincon( 'namefile',[100 25],[],[],[],[],[0 -inf],[z inf],[],[],z,w,mat1 );
%additional arguments are z,w,mat1, which are inputs in the function file
Sstar(i,j) = SS;
matnext(i,j) = -MAT;
end
mat1 = matnext;
end

Function file:
function [final] = namefile(z,s,w,g,mat1)
global par1 par2 par3 par4 par5 par6 par7 vec1 vec2
now = par1*s - par2*s^2 - par3*s/(par4*z) - par5*g
vec1_next = (z - s )^alpha
vec2_next = (1-par6)*w + g;
matnext = interp2( vec1,vec2,mat1,vec1_next',vec2_next','cubic' )
final = -( now + par7 * matnext);

When I run the code I get the following error:
FMINCON cannot continue because user supplied objective function failed with the following error:
Error using ==> mpower
Matrix must be square.

I don't see where I made a mistake. Any help is very much appreciated!
From: Matt J on
"diana van dijk" <matlab_di(a)yahoo.com> wrote in message <hnrvfd$o3u$1(a)fred.mathworks.com>...

> [SS MAT] = fmincon( 'namefile',[100 25],[],[],[],[],[0 -inf],[z inf],[],[],z,w,mat1 );
> %additional arguments are z,w,mat1, which are inputs in the function file
===============

If fmincon allows you to pass additional arguments this way, it is undocumented. The online doc says that fmincon accepts at most 10 arguments.

Regardless, for some reason you are passing only 3 additional arguments
whereas your function file takes 4 additional arguments: s,w,g,mat1

You also have an additional argument named z here, whereas your function file definition suggests that z is the main argument, not an additional one.

Finally, by passing an initial point [100,25] you are telling fmincon that
namefile(z,...) is a function of a 1x2 optimization variable z, which means fmincon will iteratively try to pass a variety of 1x2 vectors to namefile. This is probably the main reason for the error message because namefile contains mpower expressions z^p which are invalid unless z is a scalar or square matrix.
From: Steven Lord on

"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message
news:hnsoto$h30$1(a)fred.mathworks.com...
> "diana van dijk" <matlab_di(a)yahoo.com> wrote in message
> <hnrvfd$o3u$1(a)fred.mathworks.com>...
>
>> [SS MAT] = fmincon( 'namefile',[100 25],[],[],[],[],[0 -inf],[z
>> inf],[],[],z,w,mat1 );
>> %additional arguments are z,w,mat1, which are inputs in the
>> function file
> ===============
>
> If fmincon allows you to pass additional arguments this way, it is
> undocumented. The online doc says that fmincon accepts at most 10
> arguments.

That's the way the "function functions" used to accept additional input
arguments for the objective function, before the introduction of anonymous
functions. It still accepts that syntax, for backwards compatibility, but
to the OP, I STRONGLY recommend you switch to using the anonymous function
approach.

% define s, w, g, mat1 here
[SS, MAT] = fmincon(@(z) namefile(z, s, w, g, mat1), [100 25], [], [], [],
[], [0 -Inf], [z Inf]);

Since you're not using them, you no longer need to supply the nonlinear
constraint function or the options structure as empty inputs (just so that
the additional inputs are in the correct locations); the default values will
be used.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: diana van dijk on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hnta7o$jqf$1(a)fred.mathworks.com>...
>
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message
> news:hnsoto$h30$1(a)fred.mathworks.com...
> > "diana van dijk" <matlab_di(a)yahoo.com> wrote in message
> > <hnrvfd$o3u$1(a)fred.mathworks.com>...
> >
> >> [SS MAT] = fmincon( 'namefile',[100 25],[],[],[],[],[0 -inf],[z
> >> inf],[],[],z,w,mat1 );
> >> %additional arguments are z,w,mat1, which are inputs in the
> >> function file
> > ===============
> >
> > If fmincon allows you to pass additional arguments this way, it is
> > undocumented. The online doc says that fmincon accepts at most 10
> > arguments.
>
> That's the way the "function functions" used to accept additional input
> arguments for the objective function, before the introduction of anonymous
> functions. It still accepts that syntax, for backwards compatibility, but
> to the OP, I STRONGLY recommend you switch to using the anonymous function
> approach.
>
> % define s, w, g, mat1 here
> [SS, MAT] = fmincon(@(z) namefile(z, s, w, g, mat1), [100 25], [], [], [],
> [], [0 -Inf], [z Inf]);
>
> Since you're not using them, you no longer need to supply the nonlinear
> constraint function or the options structure as empty inputs (just so that
> the additional inputs are in the correct locations); the default values will
> be used.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Thanks for the advice.

I now have an anonymous function, where OptIn consists of the 2 decision variables (s,g) that are optimized through the function file 'namefile'; z and w are still the state variables over which iteration takes place:

vec1 = linspace(...)
vec2 = linspace(...)
mat1 = zeros( length(vec1), length(vec3) )

for i = 1:length(vec1)
for j = 1:length(vec2)
z = vec1(i);
w = vec2(j);
[OptOut MAT] = fmincon( @(OptIn) namefile(OptIn,z,w,mat1),[100 25],[],[],[],[],[0 -Inf],[z Inf]);
Sstar(i,j) = OptOut(1);
Gstar(i,j) = OptOut(2);
matnext(i,j) = -MAT;
end
mat1 = matnext;
end

and the function file has become:

function [final] = namefile(OptIn,z,w,mat1)
s = OptIn(1)
g = OptIn(2)
now = ...
vec1_next = ...
vec2_next = ...
matnext = interp2( vec1,vec2,mat1,vec1_next',vec2_next','cubic' )
final = -( now + par7 * matnext);

But now I get the following error:

??? Error using ==> fmincon at 504
FMINCON cannot continue because user supplied objective function failed with the following error:
Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
[OptOut MAT] = fmincon( @(OptIn) namefile(OptIn,z,w,mat1),[100 25],[],[],[],[],[0 -Inf],[z Inf]);

Can you see where the problem is? Thanks!
From: Steven Lord on

"diana van dijk" <matlab_di(a)yahoo.com> wrote in message
news:hnue7s$sfa$1(a)fred.mathworks.com...
> "Steven Lord" <slord(a)mathworks.com> wrote in message
> <hnta7o$jqf$1(a)fred.mathworks.com>...

*snip*

> and the function file has become:
>
> function [final] = namefile(OptIn,z,w,mat1)
> s = OptIn(1)
> g = OptIn(2)
> now = ...
> vec1_next = ...
> vec2_next = ...
> matnext = interp2( vec1,vec2,mat1,vec1_next',vec2_next','cubic' )
> final = -( now + par7 * matnext);
>
> But now I get the following error:
>
> ??? Error using ==> fmincon at 504
> FMINCON cannot continue because user supplied objective function failed
> with the following error:
> Error using ==> mtimes
> Inner matrix dimensions must agree.
> Error in ==> [OptOut MAT] = fmincon( @(OptIn)
> namefile(OptIn,z,w,mat1),[100 25],[],[],[],[],[0 -Inf],[z Inf]);
>
> Can you see where the problem is? Thanks!

Try running your objective function using the initial guess [100 25] as the
OptIn parameter. That should show you on which line you're trying to
multiply two matrices whose inner matrix dimensions don't agree; then you
can set a breakpoint on that line and determine the sizes of those matrices
and work your way back to troubleshoot why they're not the size you
expected.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ