views:

356

answers:

2

How do I find the derivative of sin(x) where x could be any value e.g. 1,2,3 using recursion?

+7  A: 

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. :-)

cletus
@cletus: I think you just did someone's homework for them! :)
Mitch Wheat
I think he knows that. I think he'll do just about anything for points.
Jonathan Feinberg
@Jonathon: I don't believe that is true. Cletus is a very helpful poster.
Mitch Wheat
What's wrong on solving the question? Above, @Jason commented "what makes you think that recursion is a feasible approach"...
Hilton Perantunes
I don't know, but is this really recursive? I think of recursion as dividing a problem into smaller versions of the same problem. This just looks iterative to me.
Jason
@Mitch: If there was any value for finding this answer, @osama simply had lost it. He will pay that price in the long run. I don't think we should act responsible for some unknown person's career. Let him ask nuclear power plant design strategy in SO next time. Let the diploma-affixed recruiters buy his high grades. I think @cletus is causing all good. Go cletus :)
ssg
yar first of all simply tell me how i take take the derivative of sinx
osama wahid
If it looks iterative to you then it should because every tail-recursive function does. That's kinda the point.
cletus
Jason: where'd you get that definition of recursion? And what does "smaller" mean in general? And is adding the next term of a convergent series not "smaller"?
Ken
@osama: http://tinyurl.com/yf73xao
Hilton Perantunes
actually, you very rarely want to evaluate cos(x) using taylor series using the formula you gave. That expansion is around 0, and is generally a good approximation for "small" x (i.e. |x|<1). If you try large x you will quickly notice that since you have only finitely many terms you will break the condition that |cosx|<=1. Usually, you want to use taylor series by expanding around a known easy point close to the actually value that you are looking for.
ldog
A: 

I think you need to look up the terms you are trying to use (derivative and recursion). If I read this question it sounds like you are asking how to calculate:

derivative of sin(3)

or

derivative of sin(2)

or

derivative of sin(1)

The answer is zero for all values of x, so that can't be what you are asking. Recursion from that as a starting point terminates immediately.

One can only assume that you want to evaluate the derivative of sin(x) [period]. What recursion would have to do with such a calculation is not clear. A possible interpretation is that you are looking for a numerical approximation of the sine derivative, and want to recursively narrow the interval over which you are calculating the slope.

Nobody is going to be able to read your mind well enough to answer your question as is, so it is time to rephrase it more precisely. Perhaps examples or a pseudocode fragement would be helpful to demonstrate your intentions or problem more clearly.

Peeter Joot