1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Taylor Series for cos

  1. Mar 16, 2010 #1
    1. The problem statement, all variables and given/known data

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

    2. Relevant equations

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

    3. 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!
     
  2. jcsd
  3. Mar 17, 2010 #2

    CEL

    User Avatar

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab Taylor Series for cos
Loading...