I wish to calculate sine of any given an angle without using the functions that come with programming language and devices. I have written a small code in Python which can be found here. Using the sine, I calculate cosine, tangent and cotangent as well.
The code aside, I used Taylor Series for trigonometric calculations. I don't know LaTex so let me explain what I did:
$$\sin x = \sum_{n = 0}^{\infty} \frac{(-1)^n}{(2n+1)!}x^{2n+1}$$
It's all in the code... But how would this series work, even if the results is an extremely large number. Could you explain this series a little bit, and that how this large number is the sine of a given angle, which is supposed be smaller than 1?
Your code is buggy. You have:
You can only evaluate partial sums. You need to have a parameter corresponding to how many terms to take. Something like:
For example:
In your original code, you seem to misunderstand how for-loops work. The line
loops through the 2-element tuple consisting of
0andd. Thus -- your code never did anything other than add two terms, and not terms which were actually correct. You were adding the first term in all cases as well as the termd(which would only make sense whendis an int). Thus, when you evaluatedsine(45)you were simply evaluating 2 nonadjacent terms ofsin(45 radians), which is why you saw-20481491060.906067. Note that even though the series forsineconverges for allx, the farther away from the originxis the more terms you need. Forx = 45you need a fair number of terms to get good convergence. Just 2 (nonadjacent) terms are not enough.Finally, the most Pythonic way to evaluate partial sums is to use a comprehension:
is a 1-line definition which is equivalent to the code I gave above.