sum of function handles

Hi, i want to sum two function handle. the functions that i want to sum are following;
f1=0;
for i=1:20
f2=f2(x(1),x(2),x(3))
f1=f1+f2
end
how can i do by using function handle? the following is true?
f1=@(x)(0);
for i=1:20
f2=@(x)(f2(x(1),x(2),x(3)))
f1=@(x)(f1(x)+ f2(x));
end
Can anyone help me? Because i use this last summation function for fminsearch

3 件のコメント

Jing
Jing 2013 年 1 月 16 日
Hi, I don't quite understand your functions. In the sum function(not function handle) you wrote, if f2 is a predefined function, it will be reassigned as an variable after first iteration and f1 is a double variable.
BTW, what is the for-loop used for?
Jan
Jan 2013 年 1 月 16 日
@dert: Please do not cross post. Posting a question in several forums at the same time wastes the time of the voluntary helpers.
Walter Roberson
Walter Roberson 2013 年 1 月 16 日

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

回答 (2 件)

Thorsten
Thorsten 2013 年 1 月 16 日

0 投票

Do you want to evaluate f2 20 times and store the summed result in f1? Than you can use this:
f1 = 0;
for i=1:20
f1 = f1 + f2(x(1),x(2),x(3));
end
Otherwise, please explain what you want to do.
Umar Ceres
Umar Ceres 2023 年 11 月 20 日

0 投票

So this thread is quite old and seems nobody has posted a method to sum function handles and have a resulting anonymous function. See the following:
a1 = @(x) x(1).^2 + x(2).^2;
a2 = @(x) x(1) + x(2);
syms x [1 2] matrix
a = a1(x) + a2(x)
a = 
a_syms = symmatrix2sym(a)
a_syms = 
a_anon = matlabFunction(a_syms)
a_anon = function_handle with value:
@(x1_1,x1_2)x1_1+x1_2+x1_1.^2+x1_2.^2

1 件のコメント

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 20 日
@Umar Ceres, How will you proceed with the same method if a1 and a2 are defined as follows -
a1 = @(x) sum(x(1:randi(numel(x))).^2);
a2 = @(x) sum(x(1:randi(numel(x))));

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

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

質問済み:

2013 年 1 月 16 日

コメント済み:

2023 年 11 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by