# A Recurrence Problem

• 11-29-2010
euclid
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 }```
• 11-29-2010
homer_3
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.
• 11-29-2010
euclid
Quote:

Originally Posted by homer_3
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.
• 12-02-2010
euclid
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);                 }         }```