Polynomial regression: Difference between revisions

Content added Content deleted
(Added 11l)
Line 11: Line 11:


This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].

=={{header|11l}}==
{{trans|Swift}}

<lang 11l>F average(arr)
R sum(arr) / Float(arr.len)

F poly_regression(x, y)
V xm = average(x)
V ym = average(y)
V x2m = average(x.map(i -> i * i))
V x3m = average(x.map(i -> i ^ 3))
V x4m = average(x.map(i -> i ^ 4))
V xym = average(zip(x, y).map((i, j) -> i * j))
V x2ym = average(zip(x, y).map((i, j) -> i * i * j))
V sxx = x2m - xm * xm
V sxy = xym - xm * ym
V sxx2 = x3m - xm * x2m
V sx2x2 = x4m - x2m * x2m
V sx2y = x2ym - x2m * ym
V b = (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
V c = (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
V a = ym - b * xm - c * x2m

F abc(xx)
R (@a + @b * xx) + (@c * xx * xx)
print("y = #. + #.x + #.x^2\n".format(a, b, c))
print(‘ Input Approximation’)
print(‘ x y y1’)

L(i) 0 .< x.len
print(‘#2 #3 #3.1’.format(x[i], y[i], abc(i)))

V x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
V y = [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
poly_regression(x, y)</lang>

{{out}}
<pre>
y = 1 + 2x + 3x^2

Input Approximation
x y y1
0 1 1.0
1 6 6.0
2 17 17.0
3 34 34.0
4 57 57.0
5 86 86.0
6 121 121.0
7 162 162.0
8 209 209.0
9 262 262.0
10 321 321.0
</pre>


=={{header|Ada}}==
=={{header|Ada}}==