FMINCON requires all values returned by user functions to be of data type double.

17 views (last 30 days)
Hi All,
I am trying to use fmincon for a function of symbolic variables like the follwoing:
The objfun.m file is :
function f = objfun(x)
HEURES = 3; % hours
N = 4; % Consumers
C = ones(HEURES,N);
Q= sym(sym('x%d%d',size(C)),'real');
Z = 0.1 * (sum(sym(C).*Q,2) .* sum(sym(C).*Q,2)) + sum(sym(C).*Q,2) ;
f = sum(Z);
When I run the script below I get the error message:
Error using fmincon (line 783)
FMINCON requires all values returned by user functions to be of data type double.
-- Script --
clear all;
x0 = [2,2,2,2, 2,2,2,2, 2,2,2,2,]; % Starting guess
maxS = 20; % maxSupply
maxD = 16; % maxDemand
a = 0.1;
Beq = [10; 8; 5; 25];
Bineq = [6; 5; 3; 12; 14; 3; 5; 8; 9; 14; 13; 17];
Aeq = [1 0 0 0 1 0 0 0 1 0 0 0;
0 1 0 0 0 1 0 0 0 1 0 0;
0 0 1 0 0 0 1 0 0 0 1 0;
0 0 0 1 0 0 0 1 0 0 0 1];
Aineq = [1 0 0 0 0 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 1] ;
ub = ones(3*4,1)*20;
lb = ones(3*4,1)*1;
options = optimset('Display','iter','Algorithm','interior-point');
[x,fval,exitflag,output] = fmincon(@(x) @objfun,x0,Aineq, Bineq, Aeq, Beq, lb, ub, [] ,options);
Thank you,

Answers (3)

John D'Errico
John D'Errico on 5 May 2015
So read the error message. you are returning a symbolic result from the objective function. fmincon does not solve symbolic problems.

Mohammed
Mohammed on 5 May 2015
Thank you for the hint.
I understand that fmincon does not solve symbolic problems. My described problem is a minimization problem in which I want to use generated symbolic variables because I don't know how many input variables I will have in advance. So the M and N parameters will define the number of needed variables.
I think that I missed to add the code to transform the symbolic variables into double variables.
  1 Comment
Alexi
Alexi on 28 Nov 2016
Hi, did you solve the problem? I have the same problem here. Does fmincon work for symbolic problems or not? Thanks.

Sign in to comment.


Walter Roberson
Walter Roberson on 28 Nov 2016
Your line
[x,fval,exitflag,output] = fmincon(@(x) @objfun,x0,Aineq, Bineq, Aeq, Beq, lb, ub, [] ,options);
says that your objective function is @(x) @objfun which is an anonymous function that takes a parameter and returns the handle to objfun instead of executing objfun . You need
[x,fval,exitflag,output] = fmincon(@objfun,x0,Aineq, Bineq, Aeq, Beq, lb, ub, [] ,options);

Tags

Community Treasure Hunt

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

Start Hunting!