1. Limited time only! Sign up for a free 30min personal 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!

Homework Help: Help with matlab program for cos(x)

  1. Aug 30, 2010 #1
    1. The problem statement, all variables and given/known data

    The function cos(x) can be approximated using the following series expansion:

    Write a MATLAB function called seriesCos that takes a single scalar argument, x, uses the above formula to compute cos(x), and returns the result. Your function should use a while loop and continue adding terms to the series until the absolute value of the difference between successive iterations is less than 1.e-6. MATLAB functions that may prove useful include factorial() and abs().

    2. Relevant equations
    i can't get it to work and i'm not sure what I am doing wrong.It's not going through the loop or anything

    3. The attempt at a solution
    function y=seriesCos(x);
    %calculate cos(x) through a series
    %user call seriesCos(x,n)
    %x is number to evaluate n is desire cycles through series
    x=input ('Enter x to be evalute: ');
    while er>=1e-6
    disp (ser)
    disp (er)
  2. jcsd
  3. Aug 31, 2010 #2
    look at the line in which you start the while loop and you should see something wrong

    what is er defined to be?
  4. Aug 31, 2010 #3


    User Avatar
    Science Advisor
    Homework Helper

    You are right, it's not going through the loop, because you are telling it not to:

    Code (Text):

    while er>=1e-6
    I suggest setting er to something larger than 1e-6 to start with :P
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook