Polynomial regression: Difference between revisions
Content deleted Content added
added Julia example |
|||
Line 606:
<lang j> Y (%. (i.3) ^/~ ]) X
1 2 3</lang>
=={{header|Julia}}==
The least-squares fit problem for a degree <i>n</i> can be solved with the built-in backslash operator: <lang julia>function polyfit(x, y, n)
A = [ float(x[i])^p for i = 1:length(x), p = 0:n ]
A \ y
end</lang>
Example output:<lang julia>julia> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
julia> y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
julia> polyfit(x, y, 2)
3-element Array{Float64,1}:
1.0
2.0
3.0
<lang>
(giving the coefficients in increasing order of degree).
=={{header|Mathematica}}==
|