Help with 'fmincon' fucntion for optimsation

2 views (last 30 days)
Hi,
I am trying to find the minimum value of the function with fmincon, but i need some help. My code is written here.
clc
m = 8;
a =[.75,0.75,0.85,0.8];
x0 = [0,0,0,0];
lb = [0,0,0,0];
ub = [5,2,4,5];
nonlincon = @nlcon;
objective = @(x) (x(1)*a(1))+(x(2)*a(2))+(x(3)*x(3))+(x(4)*a(4));
[i,fval] = fmincon(objective,x0,[],[],[],[],lb,ub,nonlincon);
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
disp(i)
4.2066 1.4184 0.3750 0.0000
fval
fval = 4.3594
function [c,ceq] = nlcon(x)
c = [];
ceq = sum(x)-6;
end
  1. I need to include the variable (m) into nlcon fucntion and use it instead of 6. I tried different ways and its not working. I hope some of you can help me with this.
  2. The minimum value from the program is wrong. If you multiply (x*a), you would see fval = 4.53. But the result shows 4.359. Also the minimum possible fval = 4.5, when x1 = 4.5 and x2 = 1.5. How can i make this correct?
I really appreciate your help on this one. Thank you in advance :).

Accepted Answer

Torsten
Torsten on 5 Oct 2022
Edited: Torsten on 5 Oct 2022
Your problem is linear. You might want to try "linprog" instead of "fmincon".
m = 6;
a =[.75,0.75,0.85,0.8];
x0 = [0,0,0,0];
lb = [0,0,0,0];
ub = [5,2,4,5];
Aeq = [1 1 1 1];
beq = m;
objective = @(x) (x(1)*a(1))+(x(2)*a(2))+(x(3)*a(3))+(x(4)*a(4));
[i,fval] = fmincon(objective,x0,[],[],Aeq,beq,lb,ub)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
i = 1×4
4.3430 1.6570 0.0000 0.0000
fval = 4.5000

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by