MATLAB: Anonymous function with a variable number of input variables

anonymous functionfminuncMATLAB and Simulink Student Suite

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!

Best Answer

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