Anonymous function to calculate a sum

14 ビュー (過去 30 日間)
Ellen De Jonghe
Ellen De Jonghe 2020 年 1 月 9 日
コメント済み: Stephen23 2020 年 1 月 9 日
Hye
I have to write an anonymous function which calculates the following sum for a given real number x and a positive integer:
x + (x^2)/2! + (x^4)/4! + ... + (x^2n)/(2n)!
I started off with this:
ch = @(x,n) ...
But I don't know how I can calculate the sum with only the anonymous function.

採用された回答

Stephen23
Stephen23 2020 年 1 月 9 日
編集済み: Stephen23 2020 年 1 月 9 日
>> fun = @(x,n) x + sum(x.^(2:2:2*n) ./ factorial(2:2:2*n));
>> fun(3,5)
ans = 12.066
Compare against your expansion:
>> x = 3;
>> fun(x,2)
ans = 10.875
>> x + (x^2)/factorial(2) + (x^4)/factorial(4)
ans = 10.875
  2 件のコメント
Stephen23
Stephen23 2020 年 1 月 9 日
Ellen De Jonghe's "Answer" moved here:
Thanks a lot!
Stephen23
Stephen23 2020 年 1 月 9 日
@Ellen De Jonghe: my pleasure! Please don't forget to accept my answer if it helped you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by