Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
最小二乗法による直線の当てはめ least square
// 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
You can’t perform that action at this time.