fmincon objective function problem

조회 수: 10 (최근 30일)
Sara
Sara 2017년 6월 21일
댓글: Sara 2017년 6월 25일
I need to maximize the following objective function in Matlab:
mean (a*I'*x1+b*(ones(1,24)-I)'*x1)-mean((a*(ones(1,24)-I)'*y1)+(b*I'*y1))
I made two .m file: 1) In the objfun.m file I defined the objective function as:
function f = objfun(x)
f = mean (a*I'*x1+b*(ones(1,24)-I)'*x1)-mean((a*(ones(1,24)-I)'*y1)+(b*I'*y1));
2) In the second (main) .m file I have my parameters and calculations for a, b and I and also below constraints and fmincon:
lb = [0,0];
ub = [10,10];
x0 = [1,2];
[x,fval] = fmincon(@objfun,x0,[],[],[],[],lb,ub)
When I run the program I get below error for fmincon:
''Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.'' Would you please advise how can I fix this problem? Is there anything wrong with my objective function, as it is a function of x1 and y1 but I have objfun(x) only? Or do I call the function wrongly? Any help is appreciated!

채택된 답변

Walter Roberson
Walter Roberson 2017년 6월 21일
Guessing about what a, b, x1, y1 are for:
First make sure x1 and y1 are defined.
Then
f = @(ab) mean (ab(1)*I'*x1+ab(2)*(ones(1,24)-I)'*x1)-mean((ab(1)*(ones(1,24)-I)'*y1)+(ab*2)*I'*y1));
lb = [0,0];
ub = [10,10];
x0 = [1,2];
[ab,fval] = fmincon(@objfun,x0,[],[],[],[],lb,ub)
a = ab(1); b = ab(2);
  댓글 수: 1
Sara
Sara 2017년 6월 22일
Thanks for your reply Walter! a and b are vectors(1x24) and have constant values. x1 and y1 are the variables that should be optimized(vectors 1x24) and I know the lower and upper values for them only. I tried your code in my main file (not in objfun.m) but still I get the same error :(

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Weiss
Alan Weiss 2017년 6월 22일
You need to put ALL of your control variables in one vector or matrix x.
function f = objfun(x,a,b,I)
x1 = x(1:24);
y1 = x(25:end);
f = mean (a*I'*x1+b*(ones(1,24)-I)'*x1)-mean((a*(ones(1,24)-I)'*y1)+(b*I'*y1));
Call the function like this, after you put the a, b, and I arrays in your workspace:
fun = @(x)objfun(x,a,b,I);
[x,fval] = fmincon(fun,...)
Alan Weiss
MATLAB mathematical toolbox documentation
  댓글 수: 3
Alan Weiss
Alan Weiss 2017년 6월 23일
Oh, I didn't know that you were trying to optimize symbolic variables. Basically, you either need to formulate your problem without using symbolic variables, or you need to convert your symbolic expressions to floating-point using matlabFunction as shown in Using Symbolic Mathematics with Optimization Toolbox Solvers.
However, most likely you do not need to do anything symbolically at all. Remember, MATLAB computes numerically, and so you just need to understand that fmincon is going to present your objective function with a numerical x that is, say, a 48-element vector. Your objective function simply needs to accept that vector and compute the value of the objective function. No symbolic math needed.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
Sara
Sara 2017년 6월 25일
Thanks for your response Alan! Now it works! But I get non-zero values for the first 25 items of my x vector (x1) and for the rest of items (24:end) optimization gives 0. I guess I need to define more constraints or add ''if'' statements in my code.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by