Jump to content

Polynomial regression: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 803:
3.0</pre>
(giving the coefficients in increasing order of degree).
 
=={{header|Kotlin}}==
{{trans|REXX}}
<lang scala>// version 1.1.51
 
fun polyRegression(x: IntArray, y: IntArray) {
val n = x.size
val r = 0 until n
val xm = x.average()
val ym = y.average()
val x2m = r.map { it * it }.average()
val x3m = r.map { it * it * it }.average()
val x4m = r.map{ it * it * it * it }.average()
val xym = x.zip(y).map { it.first * it.second }.average()
val x2ym = x.zip(y).map{ it.first * it.first * it.second }.average()
 
val sxx = x2m - xm * xm
val sxy = xym - xm * ym
val sxx2 = x3m - xm * x2m
val sx2x2 = x4m - x2m * x2m
val sx2y = x2ym - x2m * ym
 
val b = (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
val c = (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
val a = ym - b * xm - c * x2m
 
fun abc(xx: Int) = a + b * xx + c * xx * xx
 
println("y = $a + ${b}x + ${c}x^2\n")
println(" Input Approximation")
println(" x y y1")
for (i in 0 until n) {
System.out.printf("%2d %3d %5.1f\n", x[i], y[i], abc(x[i]))
}
}
 
fun main(args: Array<String>) {
val x = IntArray(11) { it }
val y = intArrayOf(1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321)
polyRegression(x, y)
}</lang>
 
{{out}}
<pre>
y = 1.0 + 2.0x + 3.0x^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|Mathematica}}==
9,490

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.