How do I find the derivative of sin(x) using recursion?
How do I find the derivative of sin(x) where x c开发者_JAVA百科ould be any value e.g. 1,2,3 using recursion?
Firstly, the derivative of sin(x) is cos(x) or, to put it more formally:
f(x) = sin(x)
f'(x) = cos(x)
I guess you could solve sin(x) using the Taylor series for cos(x):
cos(x) = 1 - x^2/2| + x^2/4! + ...
with recursion. In Java:
public double cos(double x) {
return 1 + next(-x*x/2, x, 3);
}
public double next(double term, double x, int i) {
double next = -term * x * x / (i * (i + 1));
return term + next(term, x, i + 2);
}
Of course you'll need to put some limiter in to exit the recursion otherwise you'll get a stack overflow error eventually, which is left as an exercise for the reader.
Oh and I see the question is tagged as C not Java, but it is homework. :-)
精彩评论