How to insert values into parameter and not function

Hi everybody,
I hope somebody can help me out: I want to create a constraint function for fmincon with an input x which comprises of six symbolic variables x1 - x6. The variable 'parameter' depends on x which is why I want to insert their values into 'parameter'. The only way I found out is to create a function as shown below:
function [c, ceq] = constraints(x)
load ('File.mat','parameter');
parameter_function = matlabFunction(parameter);
c(1) = abs(parameter_function(x)); %Constraint
ceq = [];
end
This, however, takes a lot of time to run the code and I am quite sure because of the function creation. For this reason I would like to ask if anybody knows a way to insert the x values into 'parameter' without needing to create the function.
Thanks in advance!
Nicolas

6 Commenti

Adam
Adam il 18 Ago 2017
Modificato: Adam il 18 Ago 2017
f = @(x) parameter_function(x)
or more simply
f = @parameter_function
will, in general, give a function handle that takes x as input and then passes it to another function. You should be able to pass f into anywhere that expects a function of 1 argument which will be x.
Stephen23
Stephen23 il 18 Ago 2017
Modificato: Stephen23 il 18 Ago 2017
The whole concept is slow: every time the optimizer calls this function (and that will be many times) then the function load-s some data and converts this using matlabFunction: that is going to be a total waste of time. Much more efficient would be to simply load it once and add it as an input parameter to the function.
If that data is changing then it will still be faster to pass it as a parameter, rather than using a slow file I/O call.
Does parameter_function really need to be generated anew on each call?
You should probably square c(1) to keep things differentiable
c(1) = parameter_function(x).^2;
Thank you for all your answers! @stephen: No, I was trying to avoid re-generating the same function with each iteration, but Matlab does not allow me to load the pre-created function into the contraint-function.
@Nicolas Ochmann: function handles can be passed as arguments, so there is no reason why you cannot generate that function and pass it as a parameter (see the links I gave).
Perfect, thank you very much!!

Accedi per commentare.

 Risposta accettata

Matt J
Matt J il 18 Ago 2017
Modificato: Matt J il 18 Ago 2017
load ('File.mat','parameter');
parameter_function = matlabFunction(parameter);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub, @(x) deal(parameter_function(x)^2 , [] ) )

2 Commenti

You could also modify your constraints() function as below and call fmincon as follows:
load ('File.mat','parameter');
parameter_function = matlabFunction(parameter);
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub, @(x) constraints(x,parameter_function))
function [c, ceq] = constraints(x,phandle)
c=phandle(x).^2;
ceq=[];

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by