fmincon requires only double
Afficher commentaires plus anciens
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 commentaire
Thalassia Nouni
le 14 Déc 2018
Réponses (2)
Torsten
le 13 Déc 2018
0 votes
objective= @(t) (1-t)*a + t*b; %a,b given constants
without the lines
syms t
and
objective = double(f(t))
11 commentaires
Thalassia Nouni
le 13 Déc 2018
Modifié(e) : Thalassia Nouni
le 13 Déc 2018
Torsten
le 13 Déc 2018
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
le 13 Déc 2018
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
le 13 Déc 2018
Modifié(e) : Thalassia Nouni
le 13 Déc 2018
madhan ravi
le 13 Déc 2018
Modifié(e) : madhan ravi
le 13 Déc 2018
Just post all your datas instead of puzzling everybody
Thalassia Nouni
le 14 Déc 2018
Modifié(e) : Thalassia Nouni
le 14 Déc 2018
Thalassia Nouni
le 14 Déc 2018
Torsten
le 14 Déc 2018
This is the syntax for the constraint function:
function [c,ceq] = constraints(t)
Now what are your constraints on t ?
Thalassia Nouni
le 14 Déc 2018
Walter Roberson
le 14 Déc 2018
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
le 14 Déc 2018
0 votes
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
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!