# anonymous function with a variable number of input variables

5 views (last 30 days)
dario on 10 May 2020
Edited: Ameer Hamza on 11 May 2020
Hi!
I have an anonymous function:
ht = @(A1,A2,A3,....) ...
meaning that I don't know in advance how many variables Ai are included in the definition of ht. Then I need to minimise ht in respect of the variables Ai. For example, for 2 indipendent variables I get a result with:
ht2 = @(A)ht(A(1),A(2));
A0 = [1,1];
[res,fval] = fminunc(ht2,A0)
How can I define ht2 dinamically with a variable number of unknown Ai?
Thanks!

Ameer Hamza on 10 May 2020
Edited: Ameer Hamza on 11 May 2020
See nargin()
f = @(x,y,z) x+y+z;
n = nargin(f)
Output
n =
3
Edit 2:
To avoid helper functions, you can use the following code.
Create the ht2 with all variables.
ht = @(x1,x2,x3) x1.^2+x2.^2+x3.^2;
ht2 = @(x) ht(struct('x', num2cell(x)).x);
x_sol = fmincon(ht2, rand(1,3))
Result:
x_sol =
1.0e-08 *
-0.7647 -0.6963 -0.3199
Create ht2 for single variable
ht = @(x1,x2,x3) x1.^2+x2.^2+x3.^2;
i = 2;
xs = [1 2];
ht2 = @(x) ht(struct('x', num2cell(xs(1:i-1))).x, x, struct('x', num2cell(xs(i:end))).x);
x_sol = fmincon(ht2, rand(1))
Result:
>> x_sol
x_sol =
-1.1660e-09
Edit 1:
To create ht2 on runtime, you will need to define a helper function like this
function y = helperFun(x, fun_handel)
x = num2cell(x);
y = fun_handel(x{:});
end
Then you can use it to create ht2 which is compatible for optimization functions, e.g., fmincon
ht = @(x1,x2,x3) x1.^2+x2.^2+x3.^2;
ht2 = @(x) helperFun(x, ht);
x_sol = fmincon(ht2, rand(1,3))
Result
x_sol =
1.0e-08 *
-0.4592 -0.8045 0.0830
If you want to optimize a particular variable (say x2), then you can modify the helper function like this. You will need to specify the value of variables you don't want to optimize
function y = helperFun(x, fun_handel, i, val)
% x: optimization variable
% fun_handel: function to optimize
% i: variable number to optimize, e.g., i=2 if x2 is optimization
% variable
% val: values of variables other then i-th variable
val = num2cell(val);
y = fun_handel(val{1:i-1}, x, val{i:end});
end
Then run it like this
ht = @(x1,x2,x3) x1.^2+x2.^2+x3.^2;
ht2 = @(x) helperFun(x, ht, 3, [1 2]);
x_sol = fmincon(ht2, rand(1))
Result
x_sol =
-5.3326e-09

Ameer Hamza on 10 May 2020
It is possible, but it can get a bit messy. Check the code in my updated answer.
dario on 10 May 2020
Ameer Hamza on 10 May 2020
I am glad to be of help.

per isakson on 10 May 2020
Edited: per isakson on 10 May 2020
"anonymous function with a variable number of input variables"
Something like this?
>> ht(1,2,3)
ans =
6
>> ht(1,2,3,4)
ans =
10
>> ht(1,2,3,4,5)
ans =
15
>>
where
ht = @(varargin) dario( varargin{:} );
and
function out = dario( varargin )
out = sum([ varargin{:} ]);
end

dario on 10 May 2020
Thaks.
What if:
ht(A1, A2)
or
ht(A1, A2, A3)
and I need to find the values of Ai that minimise ht?
per isakson on 10 May 2020
I assume that ht shall returns a scalar numerical output. That output could readily be calculated in dario, since all inputs are available. The variables, Ai, what type and size are they?
dario on 10 May 2020
Ai are scalar and yes, ht returns a scalar. I'm calculating Ai as
ht2 = @(A)ht(A(1),A(2));
A0 = [1,1];
[res,fval] = fminunc(ht2,A0)
but I need to know in advance how many Ai are included in the definition of ht2