Jump to content

Polynomial regression: Difference between revisions

Added AutoHotkey
(Added AutoHotkey)
Line 230:
</pre>
 
=={{header|AutoHotkey}}==
{{trans|Lua}}
<lang AutoHotkey>
regression(xa,ya){
n := xa.Count()
xm := ym := x2m := x3m := x4m := xym := x2ym := 0
loop % n {
i := A_Index
xm := xm + xa[i]
ym := ym + ya[i]
x2m := x2m + xa[i] * xa[i]
x3m := x3m + xa[i] * xa[i] * xa[i]
x4m := x4m + xa[i] * xa[i] * xa[i] * xa[i]
xym := xym + xa[i] * ya[i]
x2ym := x2ym + xa[i] * xa[i] * ya[i]
}
xm := xm / n
ym := ym / n
x2m := x2m / n
x3m := x3m / n
x4m := x4m / n
xym := xym / n
x2ym := x2ym / n
 
sxx := x2m - xm * xm
sxy := xym - xm * ym
sxx2 := x3m - xm * x2m
sx2x2 := x4m - x2m * x2m
sx2y := x2ym - x2m * ym
 
b := (sxy * sx2x2 - sx2y * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
c := (sx2y * sxx - sxy * sxx2) / (sxx * sx2x2 - sxx2 * sxx2)
a := ym - b * xm - c * x2m
result := "Input`tApproximation`nx y`ty1`n"
loop % n
i := A_Index, result .= xa[i] ", " ya[i] "`t" eval(a, b, c, xa[i]) "`n"
return "y = " c "x^2" " + " b "x + " a "`n`n" result
}
eval(a,b,c,x){
return a + (b + c*x) * x
}</lang>
Examples:<lang AutoHotkey>xa := [0, 1, 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10]
ya := [1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321]
MsgBox % result := regression(xa, ya)
return</lang>
{{out}}
<pre>y = 3.000000x^2 + 2.000000x + 1.000000
 
Input Approximation
x y y1
0, 1 1.000000
1, 6 6.000000
2, 17 17.000000
3, 34 34.000000
4, 57 57.000000
5, 86 86.000000
6, 121 121.000000
7, 162 162.000000
8, 209 209.000000
9, 262 262.000000
10, 321 321.000000</pre>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
299

edits

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