fmincon with 3 input variables

Hi everybody, I'm trying to use fmincon for solving an optimization problem with 3 variables that have to be optimized. This is basically my code:
J = @(rR, rG, rB) sum(abs(X_ref-optimization_values(rR, rG, rB)));
initial_guess= [refR; refG; refB];
[rR_opt, rG_opt, rB_opt] = fmincon(J,initial_guess,[],[]);
But I always get the following error: </matlabcentral/answers/uploaded_files/65918/Capture.PNG>
My code works perfectly if I only use one variable for the optimization. Are 3 variables not possible with fmincon? or is my syntax wrong?
Thanks and Merry Christmas to everyone!

 Accepted Answer

John D'Errico
John D'Errico on 20 Dec 2016
Edited: John D'Errico on 20 Dec 2016
You passed in a vector of initial guess. But then somehow, you expect fmincon to magically know how to interpret and split that apart into THREE different inputs to your function. How should it know how to apportion your unknowns? Unless of course, you have the mind-reading toolbox, which is still in beta test for many years.
The function J must take a VECTOR of length 3. Nothing stops you from splitting it apart yourself though, inside the function call, or like this:
J = @(rRGB) sum(abs(X_ref-optimization_values(rRGB(1),rRGB(2),rRGB(3))));

More Answers (0)

Asked:

on 20 Dec 2016

Commented:

on 20 Dec 2016

Community Treasure Hunt

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

Start Hunting!