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)
回答 (2 件)
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
2018 年 12 月 13 日
編集済み: Thalassia Nouni
2018 年 12 月 13 日
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.
Guillaume
2018 年 12 月 13 日
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
2018 年 12 月 13 日
編集済み: Thalassia Nouni
2018 年 12 月 13 日
madhan ravi
2018 年 12 月 13 日
編集済み: madhan ravi
2018 年 12 月 13 日
Just post all your datas instead of puzzling everybody
Thalassia Nouni
2018 年 12 月 14 日
編集済み: Thalassia Nouni
2018 年 12 月 14 日
Thalassia Nouni
2018 年 12 月 14 日
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
2018 年 12 月 14 日
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
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
カテゴリ
ヘルプ センター および File Exchange で Choose a Solver についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!