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

Thalassia Nouni
Thalassia Nouni 2018년 12월 14일
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.

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

답변 (2개)

Torsten
Torsten 2018년 12월 13일

0 개 추천

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

댓글 수: 11

Thalassia Nouni
Thalassia Nouni 2018년 12월 13일
편집: Thalassia Nouni 2018년 12월 13일
If i do so,then the fmincon gives me error that it requires double entries.
Torsten
Torsten 2018년 12월 13일
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 2018년 12월 13일
편집: Thalassia Nouni 2018년 12월 13일
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 2018년 12월 13일
편집: madhan ravi 2018년 12월 13일
Just post all your datas instead of puzzling everybody
Thalassia Nouni
Thalassia Nouni 2018년 12월 14일
편집: Thalassia Nouni 2018년 12월 14일
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 2018년 12월 14일
편집: Torsten 2018년 12월 14일
I don't understand your constraints. Could you write them down in a mathematical way, not as MATLAB code ?
Thalassia Nouni
Thalassia Nouni 2018년 12월 14일
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
Torsten
Torsten 2018년 12월 14일
This is the syntax for the constraint function:
function [c,ceq] = constraints(t)
Now what are your constraints on t ?
Thalassia Nouni
Thalassia Nouni 2018년 12월 14일
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)?
Walter Roberson
Walter Roberson 2018년 12월 14일
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) ?

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

Alan Weiss
Alan Weiss 2018년 12월 14일

0 개 추천

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

태그

질문:

2018년 12월 13일

댓글:

2018년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by