modify anonymous function using eval

9 visualizaciones (últimos 30 días)
Torsten
Torsten el 2 de Nov. de 2022
Comentada: Jan el 3 de Nov. de 2022
i have a question regarding the use of eval.
lets say i have an anonymous function:
fun1 = @(x, p1, p2, p3) x*p1*p2*p3;
which uses the parameters p1, p2, p3 as the multiplicators. iow i want to modify this anonymous function so that p1, p2, p3 are fixed to some values. I would do this by:
p1 = 1;
p2 = 1;
p3 = 2;
% fun2 = @(x) fun1(x*p1*p2*p3);
% edited:
fun2 = @(x) fun1(x,p1,p2,p3);
now i want to make it so that the decision variables and fixed parameters can be set by 2 arrays. these are given by previous steps.
decVars = {'x'};
fixPars = {'p1', 'p2', 'p3'};
fun3 = eval(sprintf('@( %s ) fun1( %s, %s )', strjoin(decVars, ', '), ...
strjoin(decVars, ', '), strjoin(fixPars,', ')));
i want to do this so that i can reduce the number of function inputs, which is required for further steps.
i know eval is deemed as evil, but i can not think of another way to do this. what do you recommend?
  3 comentarios
David Hill
David Hill el 2 de Nov. de 2022
Big picture, what are you trying to do?
Torsten
Torsten el 2 de Nov. de 2022
for context:
i am doing this as a pre-processing step before sending the equation to the solver. Rather a set of equations, but lets keep it simple. The eval command is therefore only ran before the main bulk of the computation.
depending on options for pre-processing, even p1, p2, p3 could become decision variables. or x, p1 are decision variables, but p2, p3 are fixed parameters. So this should be very flexible.
Using func2str & str2func seems like a good solution because it offers the most flexibility to modify the function while being less evil.

Iniciar sesión para comentar.

Respuestas (2)

Jan
Jan el 2 de Nov. de 2022
Editada: Jan el 2 de Nov. de 2022
Use a function instead:
function y = fun1(x, p1, p2, p3)
if nargin == 1
p1 = 1;
p2 = 1;
p3 = 2;
end
y = x*p1*p2*p3;
end
Do you have a good reason to use a less powerful anonymous function? Deciding for a standard function offers more possibilities for free without ugly eval constructions.
Note that eval dynamically creates entries in the lookuop table of functions or variables. This impedes the JIT acceleration and can slowdown the processing by a factor of 100 - even in parts of the code, which do not use the eval'ed anonymous function.
  2 comentarios
Torsten
Torsten el 2 de Nov. de 2022
the eval command is used only in the pre-processing step before the main bulk of the computation. after that the pre-processed functions are saved in a .mat file before being put into the solver. this shouldnt slow down the speed of the main computation, right?
Jan
Jan el 3 de Nov. de 2022
I prefer clean and clear code for productive work and avoid meta-programming by eval for good reasons.
I would not even store function handles in MAT files, if I can use functions as M-files instead. If you import a function dynamically from a MAT file, you cause exactly the same effect as using EVAL.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 2 de Nov. de 2022
I assume that you want to select from a larger set of different parameters. In that case, put them all into one cell array and then use indexing to select which ones the function is called with:
prm = { 13, 18, 23, 64, 99}; % all possible parameters
idx = [false,true,true,false,true] % corresponding indices (logical or linear)
fun1(x, prm{idx})

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by