How to write the summation of the maclaurin series of cos(x)?

18 views (last 30 days)
How to write the summation of Maclaurin series of cos(x)?
I tried:
>> syms k x
>> SUM = symsum((-1).^(k+1) * x.^2*(k+1) / factorial(2*(k+1)), k, 0, (k+1))
% I used S = symsum(f, k, a, b) formula
I need to calculate the sum of the k+1 terms with an input of k
(k is a script which has the expression: >>k = input('enter an integer: ')
I tried it and everything is working until the last step. When all the numbers are defined, the formula didn't give me a number. Instead, I got this:
>>SUM =
x^2*symsum(((-1)^(m + 1)*(m + 1))/factorial(2*m + 2), m, 0, m + 1)
Any thoughts on how can I change my code?
  2 Comments
Jan
Jan on 27 Oct 2017
Please post the original code and use the "{} Code" button for formatting. Currently it is not clear, if "m" is declared as symbolic variable also. Do you want a symbolic expression at all? What about calculating the sum numerically?
Ariel Chou
Ariel Chou on 27 Oct 2017
Edited: Ariel Chou on 27 Oct 2017
So the question asks us to:
1. generate a real number x in the range from 0 to 2π
2. prompt the user to enter a non-negative integer m
3. call a function fsum that will evaluate the sum of the m+1 terms of the maclaurin series of cos(x)
Right now I typed:
>> low=0;
>>high = 2*pi;
>> x = rand*(high-low)+low;
>> m = round(abs(input('Enter a non negative integer:')))
>> syms m x
>> fsum = symsum ((-1).^(m+1) * x.^2*(m+1) / factorial(2*(m+1)), m, 0, m+1))

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Oct 2017
Edited: Jan on 27 Oct 2017
The MacLaurin series of cos() is:
cos(x) := Sum(i = 0 to Inf) [((-1)^i / (2*i)!) * x ^ (2*i)]
[EDITED, "2*i!" replaced by "(2*i)!"]
If you want a numerical output, it is the direct approach to calculate it numerical directly:
function y = cosMacLaurin(x, k)
S = 0;
for i = 0:k
S = S + ...
end
end
I leave the "..." up to you, because I assume that this is a homework question.
  3 Comments
Ariel Chou
Ariel Chou on 27 Oct 2017
So to calculate the sum of k+1 terms of the series, do I just change k to k+1?
Jan
Jan on 27 Oct 2017
@Arial: You start at i = 0. The list 0,1,2,...,k has k+1 elements, such that the shown loop has k+1 terms already.

Sign in to comment.

More Answers (1)

Arun Kanchibotla
Arun Kanchibotla on 27 Oct 2017
Hey, I have been trying to do something similar and I am getting the same result. symsum(((-1)^m*(6407787516743315/1125899906842624)^(2*m))/factorial(2*m), m, 0, m) However, I am expecting an integer number. What am I supposed to do to correct this?
  3 Comments
Karan Gill
Karan Gill on 27 Oct 2017
Post your code and in a separate question. Without code, we can't help.
Jan
Jan on 27 Oct 2017
Please open a new thread for a new question. Then explain, which result you get, and which you expect. Perhaps only the expectation is the problem.

Sign in to comment.

Categories

Find more on Function Creation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!