[Haskell-beginners] Mathematical Blundering
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.
The diagram on the top right is very instructive.
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
To: Haskell Beginners
Subject: [Haskell-beginners] Mathematical Blundering
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
*Main> sin 1
*Main> sine 2
*Main> sin 2
*Main> sine 3
*Main> sin 3
*Main> sine 4
*Main> sin 4
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.
Beginners mailing list
Beginners at haskell.org
More information about the Beginners