Improving Cosine Approximation Using Taylor Series in Matlab

In summary, the conversation discusses writing a user-defined function using Taylor Series expansion to determine cos(x) and stopping the addition of terms when the estimated error is less than or equal to 0.000001. The conversation also mentions the use of the formula for sum Sn = Sn-1 + an and the calculation of the estimated error using the formula E = | (Sn - Sn-1)/Sn-1 |. The attempt at a solution involves starting n as 2, which results in values that are too large compared to the expected values. There is a suggestion to change the starting value of n to 1 to fix this issue.
  • #1
StaloyT
1
0

Homework Statement



Write a user-defined function that determines cos(x) using Taylor Series expansion
Stop adding terms when estimated error, E<=.000001

Homework Equations



sum Sn = Sn-1 + an
E = | (Sn - Sn-1)/Sn-1 |

The Attempt at a Solution



function y = cosTaylor(x)
Sn=1;
Snm1=0;
n=2;
xr=x*pi/180;
E=1;
while E >= .000001
an=(-1)^n*xr^(2*n)/(factorial(2*n));
Snm1=Sn;
Sn=Snm1+an;
n=n+1;
E=abs((Sn-Snm1)/Snm1);
end


This gives values too large compared to what they should be.
I really don't understand why this doesn't work.
Any help is appreciated.
Thanks in advance!
 
Physics news on Phys.org
  • #2
Why did you start n as 2? It should be 1, since in your formula for an you use 2*n.
Starting with n = 2, your first term is added to 1, making the cosine greater than unity. The following terms have absolute values smaller than the first, so you will have always too large results.
 

1. What is a Taylor Series?

A Taylor Series is a mathematical representation of a function as an infinite sum of terms. It is used to approximate the value of a function at a given point by using its derivatives.

2. How is a Taylor Series useful for cos(x)?

A Taylor Series for cos(x) can be used to approximate the value of the cosine function at any point. This is particularly useful when the value of cosine cannot be calculated directly, such as for non-integer values of x.

3. How is a Taylor Series for cos(x) calculated?

The Taylor Series for cos(x) is calculated by taking the derivatives of the cosine function and evaluating them at a specific point. These derivatives are then multiplied by the corresponding powers of x and summed together to form the series.

4. What is the difference between a Taylor Series and a Maclaurin Series?

A Maclaurin Series is a special case of a Taylor Series where the series is centered at x = 0. This means that the derivatives are evaluated at x = 0 and the powers of x are raised to non-negative integer values.

5. How accurate is a Taylor Series for cos(x)?

The accuracy of a Taylor Series for cos(x) depends on the number of terms used in the series. The more terms that are included, the closer the approximation will be to the actual value of cos(x). However, as the number of terms approaches infinity, the series becomes an exact representation of the cosine function.

Similar threads

Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
3
Views
793
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
6
Views
2K
  • Quantum Physics
Replies
3
Views
981
  • Calculus and Beyond Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top