For evaluating the behavior of a function with multiple numerical inputs, I want to write a script which fixes all but one of the input values and iterates through the remaining one using a for loop.
When changing the input variable to be varied, I have to change the index variable of the for loop manually, as well as the range (which could be somewhere else in the script). To have all manually altered values at the same place, I used an eval statement, which is generally frowned upon. Another alternative would be to save all input variables in a struct and then access them, but that would complicate the code unneccessarily.
a = 1;
b = 2;
c = 3;
iter_var_name = 'a'
iter_var_values = [1:4];
all_costs = zeros(1,length(iter_var_values))
for k = 1:length(iter_var_values)
eval([iter_var_name, '= iter_var_values(k);'])
all_costs(k) = myfun(a,b,c)
function cost = myfun(a,b,c)
cost = a + b/c;
My question is - Is there a cleaner way to rethink this problem? Thank you in advance for your help.