Paul Johnston paul.a.johnston at manchester.ac.uk
Thu Oct 16 04:57:39 EDT 2008

```Bit of basic maths.
You are using a power series to approximate sine
This works by taking an expansion about a fixed point, usually zero.
It only works well around that point.
If you get far away it works badly.
You need to exploit the cyclic nature of the trignometrical functions i.e.
Sin x = sin ((2 * pi) + x) = sin ((4 * pi) + x)
Essentially consider the shift in multiples of 2 * pi and calculate the value of x nearest to zero.

See
http://en.wikipedia.org/wiki/Taylor_series
The diagram on the top right is very instructive.

Paul

-----Original Message-----
From: beginners-bounces at haskell.org [mailto:beginners-bounces at haskell.org] On Behalf Of Jeffrey Drake
Sent: Thursday, October 16, 2008 9:47 AM

I have defined myself a set of functions to test:

fact 1 = 1
fact n = n * (fact \$ n - 1)

sine x = x - (x^3/(fact 3)) + (x^5/(fact 5)) - (x^7/(fact 7))

Where my code is 'sine' and the prelude's is sin:
*Main> sine 1
0.841468253968254
*Main> sin 1
0.8414709848078965

*Main> sine 2
0.9079365079365079
*Main> sin 2
0.9092974268256817

*Main> sine 3
9.107142857142847e-2
*Main> sin 3
0.1411200080598672

*Main> sine 4
-1.3841269841269837
*Main> sin 4
-0.7568024953079282

After 2 they seem to diverge rather rapidly, and I am not sure why. Any ideas?

I would have thought that 4 terms would have been enough.

- Jeff.

_______________________________________________
Beginners mailing list