How to write the summation of the maclaurin series of cos(x)?
18 views (last 30 days)
Show older comments
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
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?
Accepted Answer
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
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.
More Answers (1)
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
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.
See Also
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!