Least-squares line fitting: Difference between revisions
From JSXGraph Wiki
| A WASSERMANN (talk | contribs) No edit summary | A WASSERMANN (talk | contribs) 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, x_1, y_1 | ||
| //   x_2, y_2 | //   1, x_2, y_2 | ||
| //      ... | //      ... | ||
| //   x_n, y_n | //   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 | ||
| //  | // y is equal to the zero vector. | ||
| var M = [], y= [0.0,0.0,0.0], MT, B, c, z, 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([ |      M.push([1.0, p[i].X(), 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);   | |||
| </jsxgraph> | </jsxgraph> | ||
