anonymous function with a variable number of input variables

10 ビュー (過去 30 日間)
dario
dario 2020 年 5 月 10 日
編集済み: Ameer Hamza 2020 年 5 月 11 日
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
Ameer Hamza 2020 年 5 月 10 日
編集済み: Ameer Hamza 2020 年 5 月 11 日
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
  7 件のコメント
dario
dario 2020 年 5 月 10 日
Thanks Ameer. Very Helpful.
Ameer Hamza
Ameer Hamza 2020 年 5 月 10 日
I am glad to be of help.

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2020 年 5 月 10 日
編集済み: per isakson 2020 年 5 月 10 日
"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
  3 件のコメント
per isakson
per isakson 2020 年 5 月 10 日
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
dario 2020 年 5 月 10 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSolver Outputs and Iterative Display についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by