fmincon requires only double

I want to use fmincon to minimize a function. Then Matlab returns that fmincon requires only double inputs. Even thought I transform the objective function to a double, it still gives me error, without any further explanation. Could someone help?
syms t;
f = @(t) (1-t)a + tb; %a,b given constants
objective = double(f(t))
t = fmincon(objective,0,[],[],[],[],0,10,constraints)

1 Comment

function [f1,f2,f3,f4,f5] = constraints(x,y)
syms x; y = 0:0.01:0.9;
f1 = @(x) 2*x;
f2 = @(y) 5*y;
f3 = @(x) f1(x)^2;
f4 = integral(f3,0,1);
f5 = @(x) f1(x) + f1(x).^3;
if f4>0
f2 = f2 +1;
else f2 = f2 -1;
end
end
So , these are my constraints. The control variable t though, as seen in the constraints, is not included in them (nonlcon must contain the varialble to be optimized). Therefore
t = fmincon(objective,0,[],[],[],[],0,10,constraints)
doesn't work. A more appropriate syntax would be
t = fmincon(problem)
but there are not enough information for this syntax, or I haven't found anything useful.
If someone has any example would be great.
Thank you for your answers so far.

Sign in to comment.

Answers (2)

Torsten
Torsten on 13 Dec 2018
objective= @(t) (1-t)*a + t*b; %a,b given constants
without the lines
syms t
and
objective = double(f(t))

11 Comments

Thalassia Nouni
Thalassia Nouni on 13 Dec 2018
Edited: Thalassia Nouni on 13 Dec 2018
If i do so,then the fmincon gives me error that it requires double entries.
What is "ut" ? Of course, a and b also must be of type double, not of type syms.
If the error persists, please show the code you are using.
The first input of fmincon must be a function handle, which double(f(t)) is never going to be.
objective = @(t) double(f(t));
would be a valid objective function. However if there is a need for a conversion to double, that would because a or b in the original function is not double to start with, so rather than going the roundabout way, it would be better to ensure that a and b are double. In which, case:
%make sre that a and b are double, certainly not symbolic
f(t) = @(t) (1-t)*a + t*b;
t = fmincon(f,0,[],[],[],[],0,10,constraints)
Thalassia Nouni
Thalassia Nouni on 13 Dec 2018
Edited: Thalassia Nouni on 13 Dec 2018
Even though a and b are as double saved, it still gives an error. I have also transformed the constraints into double and then I receive that f(t) needs to be double.
madhan ravi
madhan ravi on 13 Dec 2018
Edited: madhan ravi on 13 Dec 2018
Just post all your datas instead of puzzling everybody
Thalassia Nouni
Thalassia Nouni on 14 Dec 2018
Edited: Thalassia Nouni on 14 Dec 2018
Here are my constrains
function [f1,f2,f3,f4,f5] = constraints(x,y)
syms x; y = 0:0.01:0.9;
f1 = @(x) 2.*x;
f2 = @(y) 5.*y;
f3 = @(x) f1(x).^2;
f4 = integral(f3,0,1);
f5 = @(x) f1(x) + f1(x).^3;
if f4>0
f2 = f2 +1;
else f2 = f2 -1;
end
end
and then the objective function is:
f(t) = @(t) (1-t)*a + t*b; %a = 0.2 b=0.1
t = fmincon(f,0,[],[],[],[],0,10,constraints)
Torsten
Torsten on 14 Dec 2018
Edited: Torsten on 14 Dec 2018
I don't understand your constraints. Could you write them down in a mathematical way, not as MATLAB code ?
f1(x) = 2x, where x is a symbolic variable
f2(y) = 5y, where y belongs to a range [0,0.9]
f3(x) = (f1(x))^2
f4 = constant = integral of f3(x) from 0 to 1
f5(x) = f1(x) + (f1(x))^3
This is the syntax for the constraint function:
function [c,ceq] = constraints(t)
Now what are your constraints on t ?
Yes, I just realized my mistake. My constraints do not include the control variable t, so I guess a more appropriate syntax would be fmincon(problem)?
your constraint function ignores its inputs and so always computes the same output . What is it constraining? What is supposed to happen with those 5 outputs? Is there a relationship between the f of your function name and the f1 f2 f3 f4 f5 output by your constraining function , such as are you intending the f1 output to be aa function handle that acts to constrain the first element of the results of f(t) ?

Sign in to comment.

Alan Weiss
Alan Weiss on 14 Dec 2018
To transform symbolic variables into MATLAB functions, use matlabFunction, as shown in the examples Symbolic Math Toolbox Calculates Gradients and Hessians or Using Symbolic Mathematics with Optimization Toolbox Solvers.
But you really might do better without using symbolic variables at all. It would amply repay you to learn how to use function handles instead. See, for example, Solve a Constrained Nonlinear Problem.
Alan Weiss
MATLAB mathematical toolbox documentation

Asked:

on 13 Dec 2018

Commented:

on 14 Dec 2018

Community Treasure Hunt

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

Start Hunting!