How do I find the derivative of sin(x) where x could be any value e.g. 1,2,3 using recursion?
-
3What makes you think that recursion is a feasible approach to evaluating a trigonometric function? – jason Nov 28 '09 at 15:53
-
Add some clarification in your description. Why do you need to use recursion? Are you not allowed to use the c standard math library functions? Does the answer need to be very precise? – A. Levy Nov 28 '09 at 15:55
-
@Jason, why would it not be feasible? You could write a recursive function to evaluate terms in the taylor series expansion of the cosine, and terminate after reaching a set precision. – A. Levy Nov 28 '09 at 15:58
-
9**please at least try** to use full English words. It's a matter of respect for your readers (and no, I'm not a native English speaker, but I do care about the quality of the language we use to communicate with each other). – Kris Nov 28 '09 at 16:02
-
3I fail to see how this isn't a real question (3 close votes for that), albeit poorly worded but since when did we punish people for not having English as their first language? – cletus Nov 28 '09 at 16:05
-
Oh and FYI I think I've demonstrated that recursion is a perfectly viable approach to solving this problem. – cletus Nov 28 '09 at 16:16
-
Recursion is not a good choice. I would see: http://stackoverflow.com/questions/523531/fast-transcendent-trigonometric-functions-for-java – Mitch Wheat Nov 28 '09 at 16:20
-
@Mitch: I think the point is that constraints are given on the solution. – cletus Nov 28 '09 at 16:37
2 Answers
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. :-)
- 599,013
- 161
- 897
- 938
-
-
I think he knows that. I think he'll do just about anything for points. – Jonathan Feinberg Nov 28 '09 at 16:07
-
7@Jonathon: I don't believe that is true. Cletus is a very helpful poster. – Mitch Wheat Nov 28 '09 at 16:08
-
1What's wrong on solving the question? Above, @Jason commented "what makes you think that recursion is a feasible approach"... – Hilton Perantunes Nov 28 '09 at 16:11
-
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 Nov 28 '09 at 16:17
-
1@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 :) – Sedat Kapanoglu Nov 28 '09 at 16:18
-
yar first of all simply tell me how i take take the derivative of sinx – osama wahid Nov 28 '09 at 16:22
-
1If it looks iterative to you then it should because every tail-recursive function does. That's kinda the point. – cletus Nov 28 '09 at 16:24
-
2Jason: 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 Nov 28 '09 at 16:26
-
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 Nov 29 '09 at 08:54
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.
- 7,376
- 7
- 46
- 74