fmincon with a scheduling problem

Hello everyone! I found an example of scheduling with fmincon on the web. It is a scheduling problem about the production of two plants. I copied the code but I get errors: some one could tell me why?
The code is the following
global M;
global S;
M(1,1) = 130; M(1,2) = 150;
M(2,1) = 160; M(2,2) = 180;
S(1,1) = 30; S(1,2) = 40;
S(2,1) = 35; S(2,2) = 45;
%STOCK LIM
L1 = 70000;
x0 = [200; 100; 100; 200];
A = [0 M(1,2) 0 M(2,2)];
b = L1;
Aeq = [1 1 0 0; 0 0 1 1];
beq = [300; 300],
lb = [0; 0; 0; 0];
ub = [inf; 300; inf; 300];
[x, fval] = fmincon(@objec_func_E15,x0,A,b,Aeq,beq,lb,ub)
with objective function
function f=objec_fun_E15(x)
global M;
global S;
s=x(1,1)*M(1,1)*S(1,1) + x(2,1)*M(1,2)*S(1,2) + x(3,1)*M(2,1)*S(2,1) + x(4,1)*M(2,2)*S(2,2);
f = -s
Thanks in advice.

3 Comments

I copied the code but I get errors: some one could tell me why?
Not until you post the errors so that we can see them (copy/pasted exactly).
Incidentally, beware the hazards of global variables! There are better alternatives, e.g.,
[x, fval] = fmincon(@(x) objec_func_E15 (x,M,S) ,x0,...)
function f=objec_fun_E15(x,M,S)
f=-(x(1,1)*M(1,1)*S(1,1) + x(2,1)*M(1,2)*S(1,2) + x(3,1)*M(2,1)*S(2,1) + x(4,1)*M(2,2)*S(2,2));
Matt J
Matt J on 26 Apr 2013
Edited: Matt J on 26 Apr 2013
Incidentally also, since your objective function and constraints are all linear, you should consider using LINPROG instead of FMINCON.
Marco Rebecchi Commented:
You're right, the errors were
??? Error using ==> feval Undefined function or method 'objec_func_E15' for input arguments of type 'double'.
Error in ==> fmincon at 574 initVals.f = feval(funfcn{3},X,varargin{:});
Error in ==> Knapsack26042013 at 31 [x, fval] = fmincon(@objec_func_E15,x0,A,b,Aeq,beq,lb,ub)
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Now I'll try with your solution.

Sign in to comment.

Answers (2)

Marco Rebecchi
Marco Rebecchi on 26 Apr 2013
Edited: Matt J on 26 Apr 2013
Relocated to Comment by Matt J
Matt J
Matt J on 26 Apr 2013
The error means that MATLAB could not find 'objec_func_E15'. It is apparently not on the path.

Products

Asked:

on 26 Apr 2013

Community Treasure Hunt

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

Start Hunting!