Last active
August 29, 2015 14:13
-
-
Save sasekazu/dce32323ad04a0f60c9c to your computer and use it in GitHub Desktop.
最小二乗法による直線の当てはめ least square
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// numeric.jsを使用 | |
// データ点を配列で渡す 例: points = [[x0,y0],[x1,y1],[x2,y2],[x3,y3]]; | |
// y=c[0]*x+c[1] で近似し係数cを配列で返す | |
function leastSquare(points) { | |
var A=numeric.rep([2, 2], 0); | |
var b=numeric.rep([2], 0); | |
for(var i=0; i<points.length; ++i) { | |
A[0][0]+=points[i][0]*points[i][0]; | |
A[0][1]+=points[i][0]; | |
A[1][0]+=points[i][0]; | |
A[1][1]+=1; | |
b[0]+=points[i][1]*points[i][0]; | |
b[1]+=points[i][1]; | |
} | |
var c=numeric.solve(A, b) | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment