fmincon requires only double

10 ビュー (過去 30 日間)
Thalassia Nouni
Thalassia Nouni 2018 年 12 月 13 日
コメント済み: Walter Roberson 2018 年 12 月 14 日
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 日
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 月 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 日
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

カテゴリ

Help Center および File ExchangeSolver-Based Nonlinear Optimization についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by