I'm new to Matlab. I was trying to perform optimisation on "8w+7x+15y+12z=0" but I got this error.
Not enough input arguments.
Error in sec2_4>@(w,x,y,z)(8*w)+(7*x)+(15*y)+(12*z)
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in sec2_4 (line 20)
fmincon(f,x0,A,b,Aeq,beq,lb,ub)
Caused by:
Failure in initial objective function evaluation. FMINCON
cannot continue.
The following is my code. I'm not sure is this because I got more than 2 parameters. Is it that I need to get a contour plot and assign better x0? But I don't think fcontour works for my equation.
f = @(w,x,y,z) (8*w)+(7*x)+(15*y)+(12*z);
Aeq = [1,1,1,1];
beq = [140];
lb = [0,0,0,0];
ub = [140,285,90,85];
A = [-1,-1,0,0;1,1,0,0];
b = [-80;40];
x0 = [140,285,90,85];
fmincon(f,x0,A,b,Aeq,beq,lb,ub)

 採用された回答

Star Strider
Star Strider 2017 年 10 月 7 日

0 投票

The optimisation routines accept a vector of parameters, so you need to write your function in that context.
Try this:
% % % b(1) = w, b(2) = x, b(3) = y, b(4) = z
f = @(b) (8*b(1))+(7*b(2))+(15*b(3))+(12*b(4));
Aeq = [1,1,1,1];
beq = [140];
lb = [0,0,0,0];
ub = [140,285,90,85];
A = [-1,-1,0,0;1,1,0,0];
b = [-80;40];
x0 = [140,285,90,85];
B = fmincon(f,x0,A,b,Aeq,beq,lb,ub); % Return Optimised Parameters

2 件のコメント

Amanda Chong
Amanda Chong 2017 年 10 月 7 日
That works but now it says:
Solver stopped prematurely.
fmincon stopped because it exceeded the function evaluation limit,
options.MaxFunctionEvaluations = 3000 (the default value).
So I thought if I need to use fcontour but then it says
Warning: Error updating FunctionContour.
Index exceeds matrix dimensions.
Star Strider
Star Strider 2017 年 10 月 7 日
That simply means that the fmincon solver did not find what it considered the ‘optimum’ in the number of function evaluations allotted to it. you can certainly increase the function evaluation limit in an ‘options’ structure, but I doubt it would significantly improve the outcome.
I do not understand your wanting to use a contour plot. Your function has four parameters, and a contour plot will allow you to evaluate one variable (possibly a parameter in the context of the current problem) as a function of two others.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Problem-Based Optimization Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by