MATLAB Answers

Anonymous function to calculate a sum

7 views (last 30 days)
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.

  0 Comments

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 9 Jan 2020
Edited: Stephen Cobeldick on 9 Jan 2020
>> 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 Comments

Stephen Cobeldick
Stephen Cobeldick on 9 Jan 2020
@Ellen De Jonghe: my pleasure! Please don't forget to accept my answer if it helped you.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by