How can I minimize a function with constraints on matrix ?

Hi everyone,
I am trying to run a simulation where I want to minimize a parameter (lambda) such that a function equals to 0 (I don't want to minimize this function, I just want to find the lowest lambda which makes my function equal 0).
I am not sure what tools to use, I tried with fmincon but my constraints are on matrix and fmincon seems to support only scalar...(at a moment I need a matrix to be equal to a matrix of zero)
Some help would be awesome :)
Thanks

10 Comments

You can formulate your constraints elementwise.
If your matrix is mxn, you will arrive at m*n equality constraints.
Best wishes
Torsten.
@Torsten I don't see how I can do that with fmincon... my "ceq" term will be a matrix but fmincon compares it to the scalar 0..
And the parameter Aeq*lambda=beq won't work cause I don't want a Aeq*lambda but a Aeq*f(lambda)=beq
If you reshape ceq, it will not be a matrix of size (mxn), but a vector of length m*n.
Best wishes
Torsten.
All right I tried but I have an error telling me my function should return a scalar value.
Here it's my code:
%%%%optimization with fmincon %%%%
lambda=0;
A=wRb*(F1*U);
B=F2*U;
C=cross(A,B);
wRb0=wRb;
Rs=[cos(lambda) -sin(lambda) 0;
sin(lambda) cos(lambda) 0;
0 0 1];
optim=@(lambda)cross((Rs*wRb)*F1*U,F2*U);
if C>0
while C>0
lambda=fmincon(optim,0,[],[],[],[],0,acos((A'/norm(A))*wRb0(:,3)),@contrainte);
Rs=[cos(lambda) -sin(lambda) 0;
sin(lambda) cos(lambda) 0;
0 0 1];
Rd=Rs*wRb;
end
else
Rd=wRb;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
And my contrainte function with ceq reshaped as you told me:
%%%%%%constraints function %%%%%%%
function [c,ceq]=contrainte(lambda)
global wRb
Rs=[cos(lambda) -sin(lambda) 0;
sin(lambda) cos(lambda) 0;
0 0 1];
c=[];
X=Rs*wRb;
ceq=[X(:,1);X(:,2);X(:,3)];
return
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sorry for the syntax I don't know how to make it look like a whole code :/
Thanks for all the help already :D
optim must return a scalar ; in your case, it returns a (3x1) vector.
From your first description of the problem I thought that optim just returns lambda ...:
optim=@(lambda) lambda
?
Best wishes
Torsten.
Yeah sorry I didn't precise enough, the optimization must find the lowest lambda that gives me a matrice that ensures that my cross product is null. I am not sure if any tools exist for this cause the optimization is on lambda but the objective function is actually a cross product giving me a vector as you noticed..
Do you think that multi-objective goal tool as fgoalattain or fminimax might be more useful ?
Thanks again for all your answer
I think the objective function is @(lambda) lambda.
Further, you will have to constrain lambda to be between 0 and 2*pi (A*x<=b facility).
The constraint function should force the vector components to be zero.
Best wishes
Torsten.
All right I will try this way I get it, I juste have a question: you tell me to constrain lambda between 0 and 2pi using Ax<=b but what's the difference between this condition and the use of the lower and upper bound ?
(Actually my lambda is between 0 and an upper bound)
Thanks
No difference. I guess your solution with lower and upper bound is better. You'll get all possible rotations in your matrix if 0<=lambda<=2*pi - so you should choose 2*pi as upper bound.
Best wishes
Torsten.
Okay thanks a lot Torsten for all your answer I managed to make it work properly :)

Sign in to comment.

Answers (1)

If your condition is not that a function equals 0 but rather that a matrix of functions each equal 0, then you have a challenge.
Might I suggest that you instead minimize the sum of squares of the matrix elements?
You said that you wanted the smallest p for which that function becomes 0. Is it the case that there are multiple p values that achieve that, but you need the smallest of those different values? (Sort of in the spirit of looking for the smallest eigenvalue?) If that is the case, is there something known about the range of p that might be suitable, or something known about the maximum number of different p that might work?

5 Comments

No it's not a matrix of function, more precisely it's a cross product that I need to be null. One of the term of this product comes from a another product where lambda appears to update this 2nd product. I have to recompute the products each time and as soon as a value of lambda enables the 1st cross product to be null I stop the optimization.
Is that clearer or not ?
Is there something known about the range of lambda that might be suitable, or something known about the maximum number of different lambda that might work?
Suppose I find a lambda that gives 0 for the cross-product. Is there any way for me to know, looking at it in isolation, that it is the lowest such lambda that needs to be considered? Is there a way for me to know, looking at it and the history of the other lambda I have found, that it is the lowest such lambda that needs to be considered?
Imagine I gave you "black box" function which I assured you was a polynomial with a finite number of real roots, but I did not tell you how many roots, and for any given argument the black box function tells you Yes or No, the argument was a root. And imagine I asked you to find the smallest root. You've found 602 roots so far: can you stop, or do you have to search all the way to -infinity?
Actually it's the lowest lambda in the sense of the first one. Lambda is an angle of rotation about an axis that gives a rotation matrix, and this matrix appears in the cross product, so basically I begin from lambda=0 and I optimize until I find the first lambda that gives the cross product = 0. As I begin from 0 this first lambda will be the lowest one that's what I meant
The minimizes make projections about where to try next, and might overshoot and end up in a different local minima. The first found will not necessarily be the one with the smallest lambda . However, it does help to know that the lower bound is 0: then if you find a different value, you can restrict your search to be between 0 and that value; if it comes up with a different value then you make that the new lowest and repeat using that as the upper bound; if no value is found and the search is fine-enough grained then you can end.
If you did not have a lower bound then the search would have to continue indefinitely.
All right thanks a lot for the explanation I finally found how to make it work :)

Sign in to comment.

Asked:

on 22 Feb 2017

Commented:

on 23 Feb 2017

Community Treasure Hunt

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

Start Hunting!