Anonymous user
Polynomial regression: Difference between revisions
added Haskell
m (Fixed lang tags.) |
(added Haskell) |
||
Line 378:
print sprintf("\n --- \n Polynomial fit: %.4f x^2 + %.4f x + %.4f\n", a, b, c)</lang>
=={{header|Haskell}}==
<lang haskell>import Data.List
import Data.Array
import Control.Monad
import Matrix.LU
ppoly p x = map (x**) p
polyfit d ry = elems $ solve mat vec where
mat = listArray ((1,1), (d,d)) $ liftM2 concatMap ppoly id [0..fromIntegral $ pred d]
vec = listArray (1,d) $ take d ry</lang>
Output in GHCi:
<lang haskell>*Main> polyfit 3 [1,6,17,34,57,86,121,162,209,262,321]
[1.0,2.0,3.0]</lang>
=={{header|J}}==
|