FMINCON requires all values returned by user functions to be of data type double.
17 views (last 30 days)
Show older comments
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,
0 Comments
Answers (3)
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.
0 Comments
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);
0 Comments
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!