MATLAB: Pass a parameter to vector of variables for fmincon

fminconfreeze variablepass parameter

I have a function of 3 variables stored in vector x and I am looking for its minimum
x0 = [20 20 20];
lb = [0 0 0];
ub = [100 100 100];
fun = @(x) x(1)+x(2)+x(3)
x_min = fmincon(fun,x0,[],[],[],[],lb,ub)
It works correctly. Now I want to "freeze" x(2) at value of 40 and find the minimum (with only x(1) and x(3) as variables). How to do this?

Best Answer

Probably the easiest way is to set ‘x0’ appropriately and then bound ‘x(2)’ to be 40:
x0 = [20 40 20];
lb = [0 40 0];
ub = [100 40 100];
fun = @(x) x(1)+x(2)+x(3);
x_min = fmincon(fun,x0,[],[],[],[],lb,ub)