evaluating cos(x)

edited January 2011 in Science & Tech
need to evaluate it to whatever order term i want. I also need to display the relative error each time. My only problem is getting the sign to switch every other term. heres what I have:

[php]
function y=democos(x)
yexact=cos(x)
nmax=input('Enter highest order to which you want to evaluate:')
y=1
for i=1:nmax
y=y+(-1)(x^(i*2))/factorial(i*2)
error=((yexact-y)/yexact)*100
[/php]

Comments

  • edited January 2011
    hamlet wrote:
    need to evaluate it to whatever order term i want. I also need to display the relative error each time. My only problem is getting the sign to switch every other term. heres what I have:

    [php]
    function y=democos(x)
    yexact=cos(x)
    nmax=input('Enter highest order to which you want to evaluate:')
    y=1
    for i=1:nmax
    y=y+(-1)(x^(i*2))/factorial(i*2)
    error=((yexact-y)/yexact)*100
    [/php]

    updated code:
    function y=democos(x)
    yexact=cos(x)
    nmax=input('Enter highest order to which you want to evaluate:')
    y=1
    for i=1:nmax
    y=y+(-1)*(x^(i*2))/factorial(i*2);
    error=((yexact-y)/yexact)*100;
    disp([i,y,yexact,error])
    end
    

    I know the problem is somewhere between the = sign and first * in that 7th line. Obviously its just subtracting each term over and over instead of alternating +/-.

    Thanks for any help.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2011
    the easiest way to make the sign alternate is to replace -1 with -1^i.

    When i is even, -1^i = 1. When i is odd, -1^i = 1.

    You may need to add or subtract 1 from i to make sure it is even in the correct indicies.
  • edited January 2011
    shwaip wrote:
    the easiest way to make the sign alternate is to replace -1 with -1^i.

    When i is even, -1^i = 1. When i is odd, -1^i = 1.

    You may need to add or subtract 1 from i to make sure it is even in the correct indicies.


    Thanks.. don't know why I didn't think of that. What do you mean in the correct indicies? There is something still wrong I can't figure it out.. my answers aren't converging towards the true value..
    function y=democos(x)
    yexact=cos(x)
    nmax=input('Enter highest order to which you want to evaluate:')
    y=1
    for i=1:nmax
        y=y+(-1^i)*((x^(i*2))/factorial(i*2));
        error=((yexact-y)/yexact)*100;
        disp([i,y,yexact,error])
    end
    

    I am running it at cos(1.5) to 8 degrees.
    It's giving me this:
    y =
    
         1
    
        1.0000   -0.1250    0.0707  276.7104
    
        2.0000   -0.3359    0.0707  574.9092
    
        3.0000   -0.3518    0.0707  597.2741
    
        4.0000   -0.3524    0.0707  598.1727
    
        5.0000   -0.3524    0.0707  598.1952
    
        6.0000   -0.3524    0.0707  598.1956
    
        7.0000   -0.3524    0.0707  598.1956
    
        8.0000   -0.3524    0.0707  598.1956
    
    
    ans =
    
       -0.3524
    

    It is closer to yexact at the beginning? What the fuzzy no nosed chimp?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited January 2011
    you need parentheses around the -1

    it should be (-1)^i instead of -1^i
  • edited January 2011
    shwaip wrote:
    you need parentheses around the -1

    it should be (-1)^i instead of -1^i

    gives me the same answers?
  • edited January 2011
    nope never mind got it! Thanks a lot, appreciated.
Sign In or Register to comment.