Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fminsearch with matrix as an input

229 views
Skip to first unread message

Saad

unread,
Aug 10, 2011, 12:46:32 PM8/10/11
to
Dear all,

I would like to minimize a function called "cal4" (please see below) that is function of a matrix H. I would like to do the minimization by changing the matrix H ( 7 by 7). When I write the code:

%%%%%%%%%%%%%%%%%%%%%%%%%
start0=eye(7);

g= fminsearch(@(H)cal4(H), start0);

I get the following error message:
??? Subscripted assignment dimension mismatch.

Error in ==> fminsearch at 205
fv(:,1) = funfcn(x,varargin{:});

Error in ==> cal5 at 106
g= fminsearch(@(H)cal4(H), start0);

Any ideas where the problem is? I dont understand why there is a dimension mismatch because the output of "cal4" is also 7 by 7 matrix. Sorry I can not copy cal4 because the function is quite long but I am happy to clarify further if necessary.


Now I also would like to minimize "cal4" by only changing some elements of the matrix (not the whole matrix because I would like to the diagonals to be equal to 1 all the time) .

How I can do that using fminsearch? I tried something like

g= fminsearch(@(H(2,1))cal4(H), 0.1) but matlab does not accept that.....

Any help or suggestion is welcomed. Thank you very much

Best Regards

S

Matt J

unread,
Aug 10, 2011, 1:53:13 PM8/10/11
to
"Saad " <saad.ba...@imperial.ac.uk> wrote in message <j1ucl8$jbh$1...@newscl01ah.mathworks.com>...

>
> Any ideas where the problem is? I dont understand why there is a dimension mismatch because the output of "cal4" is also 7 by 7 matrix.
=================

The output of cal4 (or whatever function you're minimizing) must be a scalar.

Alan Weiss

unread,
Aug 10, 2011, 2:23:56 PM8/10/11
to

Furthermore, fminsearch does not accept constraints, such as "all the
elements of the diagonal must equal 1." Now this limitation is easy to
work around: just have fminsearch minimize a vector or matrix of the
parameters that you are willing to change. For example, if cal4 were
2-by-2, and you wanted the diagonal entries to equal 1, you could write
something like
function y = cal4(x)
mymatrix = [1 x(1); x(2) 1]
% then calculate the scalar objective function
% of mymatrix
y = theSolution

Your initial point x0 in this case would be a 2-element vector.

For more general minimization with arbitrary constraints, use
Optimization Toolbox or Global Optimization Toolbox.

Alan Weiss
MATLAB mathematical toolbox documentation

Saad

unread,
Aug 11, 2011, 2:06:27 PM8/11/11
to

Alan Weiss

unread,
Aug 11, 2011, 2:10:00 PM8/11/11
to

Saad

unread,
Aug 12, 2011, 5:29:10 AM8/12/11
to
Alan Weiss <awe...@mathworks.com> wrote in message <j215to$mrj$1...@newscl01ah.mathworks.com>...

Hi Alan

Thank you very much for your help on this. I set up the matrix just like you said. Actually my issue is slightly more involved than this (sorry this is going to be a bit long).

I am trying the minimize the following function "cal6":

%%%%File 1%%%%%%%%%%%%%%%%%%%%%%%

function M= cal6(X,n)

%%% some constants
n_ask=n;

H= [1 X(1) X(2) ; X(3) 1 X(4) ; X(5) X(6) 1 ];

P= cal4(H,n);

%% As you can see one of the input of "cal6" is P and the same P is the output of %%another function (P in my case is a 'n by n' matrix)

%% M is the output of "cal6" that minimizes the difference btw variance of the %% second column of P and some constant n_ask
%% M is a scalar

M=(var(P(:,2))-n_ask^2)^2;

%%%%%%%%%%%File 2%%%%%%%%%%%%%%

%% I create another file where I call the function "cal6" to minimize
n_ask=0.5;
start_X=zeros(1,6);

g= fminsearch(@(X)cal6(X, n), start_X)

%%%
Now as I said I am trying to minimize "cal6" by changing "H". and I would like to use the new estimate of "H" as new input "cal4" and minimize again "cal6" and keep repeating the operation until the norm of "H_new" minus "H_old" is smaller than 0.01. My questions are:

IS this repeating process done automatically by fminsearch? How I can stop the iteration when the norm of the difference is smaller than 0.01? Finally, is there any other way to write my code in an efficient way?

Thank you very much for your advice on this.

Best Regards

S

Alan Weiss

unread,
Aug 12, 2011, 8:42:54 AM8/12/11
to

To answer your second question first:
As explained in the documentation
http://www.mathworks.com/help/techdoc/math/bsgpq6p-17.html
you can set the TolFun option to 1e-2 to stop the iterations when the
difference between successive evaluations is less than 1e-2 (similar,
but not exactly the same as what you asked). You can also set an output
function
http://www.mathworks.com/help/techdoc/math/bsgpq6p-23.html
to stop the iterations according to your criterion. In particular, see
http://www.mathworks.com/help/techdoc/math/bsgpq6p-23.html#bsgpq6q-39

For your first question:
You do not need to perform iterations manually. Your cal6 function calls
cal4, and fminsearch tries to minimize cal6, which is (I think) what you
want.

I don't see any obvious way to make your code more efficient, except
perhaps by passing in some options such as a different TolFun or an
output function. It strikes me that there might not be a unique minimum
to your problem, but I could be wrong.

Good luck,

Saad

unread,
Aug 12, 2011, 9:29:13 AM8/12/11
to
Alan Weiss <awe...@mathworks.com> wrote in message <j2374e$5a4$1...@newscl01ah.mathworks.com>...

Hi Alan

Thank you for your quick reply. The constraint that I would like to impose is on the input "H" not the function value "cal6" (which is the function I am trying to minimize)

Saad

unread,
Aug 12, 2011, 9:37:11 AM8/12/11
to
Alan Weiss <awe...@mathworks.com> wrote in message <j2374e$5a4$1...@newscl01ah.mathworks.com>...

Hi Alan

Thank you for your quick reply (and sorry If you received an incomplete response). I was saying that the constraint that I would like to impose is on the input "H" and not on the function value "cal6" (which is the function I am trying to minimize). In that case I have to set TolX to 1e-02 right? Can I just set the TolX to 1e-2 without setting up an output function as you suggested?
Since "H" is a matrix, would the algorithm automatically calculates the differences btw the norms?


Thank you for your advice

Best
S

Alan Weiss

unread,
Aug 12, 2011, 11:37:35 AM8/12/11
to
> Hi Alan
>
> Thank you for your quick reply (and sorry If you received an incomplete
> response). I was saying that the constraint that I would like to impose
> is on the input "H" and not on the function value "cal6" (which is the
> function I am trying to minimize). In that case I have to set TolX to
> 1e-02 right? Can I just set the TolX to 1e-2 without setting up an
> output function as you suggested?
> Since "H" is a matrix, would the algorithm automatically calculates the
> differences btw the norms?
>
>
> Thank you for your advice
>
> Best
> S

I do not understand what you mean when you say "the constraint that I

would like to impose is on the input "H" and not on the function value

"cal6" (which is the function I am trying to minimize)." What constraint
are you talking about? The exit condition (I mean the reason the
function stops iterating)?

To understand what TolX and TolFun do, look here:
http://www.mathworks.com/help/toolbox/optim/ug/f12471.html#brhv4_o-1

Saad

unread,
Aug 12, 2011, 11:51:30 AM8/12/11
to
Alan Weiss <awe...@mathworks.com> wrote in message <j23hbv$cvl$1...@newscl01ah.mathworks.com>...


Hi Alan

Yes I mean the exit condition...I would like to pout the exit condition on "H" and not the function itself. Shall I set TolX to 1e-02 right? Then the norm would be calculated automatically I guess? Thank you

Best

S

Paul Kerr-Delworth

unread,
Aug 16, 2011, 5:46:12 AM8/16/11
to
"Saad " <saad.ba...@imperial.ac.uk> wrote in message <j23i62$fop$1...@newscl01ah.mathworks.com>...

Hi Saad,

TolX sets the tolerance on the exit condition of your variables, X. From a quick look at your code, these variables make up your matrix H. As such, you can think of TolX as an exit condition on the matrix H in this case.

The norm used in the stopping test in fminsearch is calculated automatically and you do not have to implement it.

Hope this helps!

Best regards,

Paul

0 new messages