Least-squares line fitting: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 14: Line 14:
}
}
brd.unsuspendUpdate();
brd.unsuspendUpdate();
/*
 
// Having constructed the points, we can fit a circle  
// Having constructed the points, we can fit a circle  
// through the point set, consisting of n points.
// through the point set, consisting of n points.
// The (n times 3) matrix consists of
// The (n times 3) matrix consists of
//  x_1, y_1, 1
//  1, x_1, y_1
//  x_2, y_2, 1
//  1, x_2, y_2
//      ...
//      ...
//  x_n, y_n, 1
//  1, x_n, y_n
// where x_i, y_i is the position of point p_i
// where x_i, y_i is the position of point p_i
// The vector y of length n consists of
// y is equal to the zero vector.
//    x_i*x_i+y_i*y_i
var M = [], y= [0.0,0.0,0.0], MT, B, c, z, n;
// for i=1,...n.
var M = [], y = [], MT, B, c, z, n;
n = p.length;
n = p.length;
for (i=0;i<n;i++) {
for (i=0;i<n;i++) {
     M.push([p[i].X(), p[i].Y(), 1.0]);
     M.push([1.0, p[i].X(), p[i].Y()]);
    y.push(p[i].X()*p[i].X() + p[i].Y()*p[i].Y());
}
}


Line 45: Line 42:
// Finally, we can read from the solution vector z the coordinates [xm, ym] of the center
// Finally, we can read from the solution vector z the coordinates [xm, ym] of the center
// and the radius r and draw the circle.
// and the radius r and draw the circle.
var xm = z[0]*0.5;
//var xm = z[0]*0.5;
var ym = z[1]*0.5;
//var ym = z[1]*0.5;
var r = Math.sqrt(z[2]+xm*xm+ym*ym);
//var r = Math.sqrt(z[2]+xm*xm+ym*ym);
 
brd.create('line',z);  


brd.create('circle',[ [xm,ym], r]);
*/
</jsxgraph>
</jsxgraph>



Revision as of 19:21, 5 November 2010