Hello, I am new with the optimization tool of MATLAB and I have two question regarding my optimization function. I have a function with a vector P[1x6] as input and only one scalar output which I want to optimize. Before setting the constraints, I already have a error:
FMINCON requires all values returned by functions to be of data type double.
But my output it's an scalar, so I'm a bit confused. Here my code:
fun = @facturade;
A = []; b = []; Aeq = []; beq = [];
lb = [0 0 0 0 0 0];
ub = [150 150 150 150 150 150];
x0 = [100 100 100 100 100 100];
%nonlcon = @const;
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Then, I would like to know how to write multiple constrains for the problem, such as:
P(1)<=P(2); P(2)<=P(3); P(3)<=P(4); P(4)<=P(5); P(5)<=P(6);
For what I've seen, it's posible with nonlcon function, but as P it's my input vector for my function fun, I'm not sure how to write the function.

댓글 수: 6

Torsten
Torsten 2021년 5월 20일
편집: Torsten 2021년 5월 20일
  1. What do you get if you call fun(x0) before you call fmincon ?
  2. Your constraints on P are linear - they can be put in matrix A and vector b:
A = [1 -1 0 0 0 0; 0 1 -1 0 0 0; 0 0 1 -1 0 0; 0 0 0 1 -1 0; 0 0 0 0 1 -1];
b = [0;0;0;0;0];
Víctor García
Víctor García 2021년 5월 20일
  1. I get an scalar (the solution for that value) and then the error of fmincon
  2. Then how should I write the function fun? as @(x) facturade or @facturade or @(x) facturade(x1,x2,x3,x4,x5,x6) ?
Torsten
Torsten 2021년 5월 20일
편집: Torsten 2021년 5월 20일
  1. What value do you get ?
  2. Just as you wrote in your code: fun = @facturade, and later in your script
function scalar = facturade(P)
scalar = sum(P.^2); % for example
Víctor García
Víctor García 2021년 5월 20일
  1. ans = single 14515.2
  2. I still have the same ERROR
Error using fmincon (line 714)
FMINCON requires all values returned by functions to be of data type double.
Error in Funcion_calcular_power_term (line 36)
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
For the new code:
fun = @facturade;
A = [1 -1 0 0 0 0; 0 1 -1 0 0 0; 0 0 1 -1 0 0; 0 0 0 1 -1 0; 0 0 0 0 1 -1];
b = [0 0 0 0 0];
Aeq = [];
beq = [];
lb = [0 0 0 0 0 0];
ub = [150 150 150 150 150 150];
x0 = [100 100 100 100 100 100];
fun(x0)
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
Torsten
Torsten 2021년 5월 20일
편집: Torsten 2021년 5월 20일
Insert a print command for the calculated scalar directly in function facturade. What is the output to the screen ?
Further, it seems facturade returns a result of type single, not double. I don't know how this happens since on the terminal where I work, I'm not able to download and read your function file.
Víctor García
Víctor García 2021년 5월 20일

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

 채택된 답변

Alan Weiss
Alan Weiss 2021년 5월 20일

0 개 추천

I think that your fracturade function is returning data type SINGLE. Perhaps the quickest fix is to include the following call just before the end of the function:
y = double(y);
A better fix would be to determine why the output is single to begin with and fix that. But calling double will enable you to get on with your work.
Alan Weiss
MATLAB mathematical toolbox documentation

댓글 수: 3

Torsten
Torsten 2021년 5월 20일
Difficult to calculate gradients for fmincon with only one digit of precision ...
Víctor García
Víctor García 2021년 5월 20일
It workd with y = double(y); but the solution it's the initial point, not an optimal at all!
Alan Weiss
Alan Weiss 2021년 5월 20일
Torsten's comment indicates the reason why: the finite difference steps are too small to get a nonzero gradient estimate. You really need more precision in your calculation.
Barring that, set larger finite difference steps as explained here.
Alan Weiss
MATLAB mathematical toolbox documentation

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

추가 답변 (0개)

카테고리

질문:

2021년 5월 20일

댓글:

2021년 5월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by