Thread: A Recurrence Problem

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    30

    A Recurrence Problem

    Was wondering if someone could offer me some advice..

    I am trying to evaluate sin(x) using the recurrence relation sin(ny) = 2cos(y)sin((n-1)y)-sin((n-2)y).

    The problem states that i take y = 0.01 and cos(y) = 1 - x^2/2, and obviously for a small x, sinx = x.

    I have so far attempted to do this using the function below, however the results it provides are either wrong, or the program gets stuck repeatedly calling itself. I think the problem may well lie in my criteria for if x is small, but i can't figure out a another way.
    So i ask: Does this seem likely? Should i start over? What stupid thing(s) am i doing?

    thanks a lot
    Code:
    (meanwhile in main...)
    result = sin_recur(n, y);
    ...
    
    
    double sin_recur(double n, double y)
    {
        double x = n*y;
    
        if( x <= 0.0001 && x >= -0.0001)     //for small x, sin(x) = x
            return x;
    
        else if(x == 0.0)                               //sin(0) = 0
            return 0;
    
        else
            return  (2 - (x*x))*(sin_recur(n-1, y)) - sin_recur(n-2, y) ;  //use recurrence relation
    }

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    The infinite loop probably has to do with
    Code:
    sin_recur(n-2, y)
    If n is ever odd and y is nonzero, your return x/0 statements will never be true.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Quote Originally Posted by homer_3 View Post
    The infinite loop probably has to do with
    Code:
    sin_recur(n-2, y)
    If n is ever odd and y is nonzero, your return x/0 statements will never be true.
    Thanks, i will look at this part.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    30
    Im still working on this one, with not much luck.

    (assuming even values of n only) I now get the correct answer using values of x=0.1 and 0.2 with a y value 0.01. Any x > 0.2 causes the program to get stuck in calculation. Increasing the value of y means i can get (not entirely accurate) values for larger x.

    Is it that the program is just too computationally long for a large n value? Is there some way to simplify the program that i am not aware of?

    thanks for looking (and btw my original post said to take cos(y) = 1 - x^2/2, but it is infact y^2/2 rather than x.
    Code:
    float sin_recur(float n, float y, float result)
    {	
    
    	float x = n*y;
    
    	if(x==0)
    		return 0;
    	else if(x <= 0.00001)
    		return x;
    	else
    		{
    		printf("%f\n", n);
    		return result = (2 - y*y)*sin_recur((n-1), y, result) - sin_recur((n-2), y, result);
    
    		}
    	
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM