Recursive sinusoidal function

I am writing a sine function that should be recursive. I wrote a sinusoidal function, but I'm not sure how to do this recursively. Can someone explain how to start with this?

This is what I still have:

/*--------------------------------------------------------------
Name: sine( double X );

Return: Function "sine" will return the 
sine of X, where X is measured in radians.
--------------------------------------------------------------*/

double sine(double X)
{
  double result = 0;
  double term;
  int k;
  double lim;

  k = 0;
  lim = power(10, -8); 
  term = power(-1, k)*power(X, ((2*k) + 1)) / (factorial((2*k)+1));  
  result = term;
  while (absolute(term) > lim)  
  {
    k += 1;
    term = power(-1, k)*power(X, ((2*k) + 1)) / (factorial((2*k)+1));  
    result += term;
  }


  return result;
}

EDIT: I used the wrapper function to solve this problem. Basically created another function called

double sine_rec(double X, double k)

and changed around the current code to fit this.

+3
source share
2 answers

, , sine(double X, int n), - . - [nth term in series] + sine(X, n - 1) ( if n = 1).

+4

while :

double sine(double X, int k = 0)
{
  double result = 0;
  double term;
  double lim;

  lim = power(10, -8); 
  term = power(-1, k)*power(X, ((2*k) + 1)) / (factorial((2*k)+1));  
  if (absolute(term) > lim)  
  {
    return sine(X, k+1) + term; 
  }
  else
  {
    return term;
  }
}

. ( , )

0

All Articles