Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fminsearch with matrix as an input

Subject: Fminsearch with matrix as an input

From: Saad

Date: 10 Aug, 2011 16:46:32

Message: 1 of 3

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

Subject: Fminsearch with matrix as an input

From: Matt J

Date: 10 Aug, 2011 17:53:13

Message: 2 of 3

"Saad " <saad.badaoui07@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.

Subject: Fminsearch with matrix as an input

From: Alan Weiss

Date: 10 Aug, 2011 18:23:56

Message: 3 of 3

On 8/10/2011 1:53 PM, Matt J wrote:
> "Saad " <saad.badaoui07@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.

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us