Alan Weiss <aweiss@mathworks.com> wrote in message <j2374e$5a4$1@newscl01ah.mathworks.com>...
> On 8/12/2011 5:29 AM, Saad wrote:
> > Alan Weiss <aweiss@mathworks.com> wrote in message
> > <j215to$mrj$1@newscl01ah.mathworks.com>...
> >> On 8/11/2011 2:06 PM, Saad wrote:
> >> > 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
> >>
> >> See the answers in this thread:
> >> http://www.mathworks.com/matlabcentral/newsreader/view_thread/311442
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
> >
> > 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
>
> To answer your second question first:
> As explained in the documentation
> http://www.mathworks.com/help/techdoc/math/bsgpq6p17.html
> you can set the TolFun option to 1e2 to stop the iterations when the
> difference between successive evaluations is less than 1e2 (similar,
> but not exactly the same as what you asked). You can also set an output
> function
> http://www.mathworks.com/help/techdoc/math/bsgpq6p23.html
> to stop the iterations according to your criterion. In particular, see
> http://www.mathworks.com/help/techdoc/math/bsgpq6p23.html#bsgpq6q39
>
> 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,
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
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 1e02 right? Can I just set the TolX to 1e2 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
