taylor series expansion of cosx

Hi everyone,
I'm struggling with a question below:
Write a script file that computes cos(x) for the given x using the first 20 terms of the power series. Use an array to store the successive terms but do not use the symbolic toolkit, or the factorial function built into Matlab.The code should not calculate each term from scratch but use the previous one to find the next.

1 Comment

Jiang - what have you tried so far? The Taylor Series definition can be found here and is straightforward. What exactly is preventing you from implementing your function?

Sign in to comment.

Answers (1)

Roger Stafford
Roger Stafford on 18 Nov 2014
Edited: Roger Stafford on 18 Nov 2014
Hint to Jiang: Note that in the successive terms of the series for cos(x), there is a rather simple arithmetic operation that can be done to each term to obtain the next term in the series. For example, what would you have to do to (+x^4/4!) to get (-x^6/6!)? That is what your assignment is asking you to do. That is also how you can avoid calling on the factorial function.

2 Comments

A nice trick that I used when I wrote HPF, if you will go out to the x^n term, is to factor out factorial(n)/x^n from the entire series. Then work backwards, starting with the last term in the series. This lets you sum the small terms first. It allows you to avoid almost all of the divides, but one at the very end.
I agree with you, John. For many such series if one knows how many terms are to be used, it is more accurate to work backwards in the series so that the terms become successively larger rather than smaller. However, I have the feeling that the instructor who devised this question did not expect this degree of sophistication from their students.

Sign in to comment.

Asked:

on 18 Nov 2014

Commented:

on 18 Nov 2014

Community Treasure Hunt

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

Start Hunting!