0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ This is an overly-broad question. You really need to study the theory of Taylor series. The main intuitive point is that for any x, the factorials in the denominator eventually dwarf the powers of x. $\endgroup$ – John Coleman Jul 15 '16 at 11:06
  • 1
    $\begingroup$ looking at the edit history it appears your question was lost a little bit in the edit. If your last paragraph meant to say that your code output a large value, then there is something wrong with your code (or you just didn't evaluate enough terms for the error to be sufficiently small, it kinda depends on what your $x$ is) $\endgroup$ – bthmas Jul 15 '16 at 11:12
  • $\begingroup$ @JohnColeman: My education has been fairly void of discrete mathematics. That's why I'm asking here. Maybe you could be more specific, such as explaining this problem only? $\endgroup$ – CodesInTheValley Jul 15 '16 at 11:12
  • $\begingroup$ @bthmas: I calculated the series up to n = angle in degrees. For example for angle = 45, n = 45. I also debugged the code. Factorial works fine. And the series is just an arithmetic expression $\endgroup$ – CodesInTheValley Jul 15 '16 at 11:15
  • 1
    $\begingroup$ Another problem (besides radians/degrees) is that the cosine is not cosine = 1 - sine. You can compute $\cos x = \pm\sqrt{1-\sin^2 x }$ but then you must either restrict the range of $x$ or compute the correct sign with other means. $\endgroup$ – gammatester Jul 15 '16 at 11:22
2
$\begingroup$

Your code is buggy. You have:

def sine(d):
    serial = 0
    for i in (0, d):
        serial += ((-1)**i / (factorial((2 * i) + 1))) * d ** ((2*i) + 1)
    return serial 

You can only evaluate partial sums. You need to have a parameter corresponding to how many terms to take. Something like:

def sine(x,k):
    serial = 0
    for i in range(k):
        serial += ((-1)**i / (factorial((2 * i) + 1))) * x ** ((2*i) + 1)
    return serial 

For example:

>>> sine(math.pi/4,10)
0.7071067811865475
>>> 1/math.sqrt(2)
0.7071067811865475

In your original code, you seem to misunderstand how for-loops work. The line

for i in (0,d):

loops through the 2-element tuple consisting of 0 and d. 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 term d(which would only make sense when d is an int). Thus, when you evaluated sine(45) you were simply evaluating 2 nonadjacent terms of sin(45 radians), which is why you saw -20481491060.906067. Note that even though the series for sine converges for all x, the farther away from the origin x is the more terms you need. For x = 45 you 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:

def sine(x,k):
    return sum(((-1)**i / (factorial((2 * i) + 1))) * x ** ((2*i) + 1) for i in range(k))

is a 1-line definition which is equivalent to the code I gave above.

$\endgroup$
  • $\begingroup$ I can't upvote you so thank you. For loops in Python are a little bit tricky. I used to play around with C# and it had expressions after for and a different loop called foreach. In Python they're the same, I should have used range(). Your answer really helped me and I'm glad. Please visit my collection of mathematical (mostly) codes here $\endgroup$ – CodesInTheValley Jul 15 '16 at 12:49
  • $\begingroup$ @CodesInTheValley Interesting github. Python for loops take some getting use to but are fairly powerful. If you get into the object-oriented part of Python, I like how you can create custom iterators for container classes which specify what it would mean to use a for loop to iterate over objects of your class. You might like the book "Think Complexity" by Allen Downey for some neat examples of mathematical computing in Python. $\endgroup$ – John Coleman Jul 15 '16 at 14:20
0
$\begingroup$

Taylor Series is a representation of a function as an infinite sum over some interval, centered at some point. The interval (Radius of Convergence) for which the representation is valid depends from function to function and is the interval on which the infinite sereis converges.

To see why the inifnite series converges on $\mathbb{R}$, note that although some of the summands might initially be bigger than $1$ we also have some negative terms in the sequence. Eventually as the factorial grows much faster than the exponential function the summands will quickly start approaching to zero. This is just a intuitive way to see why the sequence always converges on $\mathbb{R}$, but it's not very difficult to turn it into a rigorous proof.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.