fminunc on multiple variables

Hi,
I am new to using fminunc and have looked quite a bit into it but couldn't find what I was looking for. If I have a function like: myFun(x,y,z,a,b,c), how can i use fminunc to optimize over x,y and z simlutaneously.
REGARDS

댓글 수: 1

Shivani Chepuri
Shivani Chepuri 2020년 8월 11일
Hi, Can you specify how to write lower and upper bound constraints for x,y,z variables here, using fminunc and fmincon? Also, all x,y,z are vectors. Thanks

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2013년 12월 13일
편집: Walter Roberson 2013년 12월 13일

2 개 추천

@(X) myFun(X(1), X(2), X(3), a, b, c)

댓글 수: 4

Asad
Asad 2013년 12월 13일
THANKS. What if x,y and z are matrices of different sizes. Will this method still work?
Walter Roberson
Walter Roberson 2013년 12월 13일
편집: Walter Roberson 2013년 12월 13일
In such a case, make your initial point, x0, a vector whose total length is the sum of the number of elements in x, y, and z. Then in the anonymous call, extract and (if necessary) reshape the elements of the argument and pass them in to the appropriate location.
@(X) myFun( X(1:5), reshape(X(6:26), 3, 7), reshape(X(27:30), 2, 2), a, b, c)
for the case where the first argument should be 5 x 1, the second 3 x 7, the third 2 x 2.
Louis Perriens
Louis Perriens 2020년 1월 24일
Very solid!! thank you!!
Shivani Chepuri
Shivani Chepuri 2020년 8월 11일
Hi, Can you specify how to write lower and upper bound constraints for x,y,z variables here, using fminunc and fmincon? Also, all x,y,z are vectors. Thanks

댓글을 달려면 로그인하십시오.

Sean de Wolski
Sean de Wolski 2013년 12월 13일

0 개 추천

Objective functions expect all of the variables being optimized to be in "x"
myfun(x,a,b,c)
Where x is now a 1x3 vector. Then inside of myfun:
Use x(1) to refer to the first element, x(2) for y and x(3) for z. To pass in other variables, look at:

댓글 수: 4

Asad
Asad 2013년 12월 13일
THANKS. Can x be a cell in which each element is a matrix of different size?
Sean de Wolski
Sean de Wolski 2013년 12월 13일
No, x must be a vector.
This would mean that you would be optimizing every value in the matrix to solve a numel(elements_in_matrix) dimensional problem - something completely different.
What are you trying to do?
Also, just to be clear, the additional parameters (a,b,c) that are not being perturbed for the minimization can be anything.
Asad
Asad 2013년 12월 13일
I am trying to do unconstrained optimization to calculate location of 3D points, planes and camera matrices.
Sean de Wolski
Sean de Wolski 2013년 12월 16일
So you are basically trying to optimize over n dimensions where n is the total number of elements in all of your matrices.
You can do this the same way Walter and I have suggested: essentially pass in a 1xn vector x and then split it into its matrices etc. inside of the objective function.
Since I would expect fminunc to have trouble with a problem like this due to many local minima and discontinuities, you might want to try using patternsearch in the Global Optimization toolbox. This solver is much better able to handle the above. You might also choose to constrain you search wherever possible. The constraints might not be obvious but some things to think about: are some variables positive? are some within a range? Do some of their value depend on others or for example a row of your matric must sum to x? etc.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2013년 12월 13일

댓글:

2020년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by