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 6: | Line 6: | ||
<jsxgraph width="600" height="600"> | <jsxgraph width="600" height="600"> | ||
var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-5,5,5,-5], keepaspectratio:true, axis:true}); | var brd = JXG.JSXGraph.initBoard('jxgbox',{boundingbox:[-5,5,5,-5], keepaspectratio:true, axis:true}); | ||
var i, p = [], angle, xr, yr, delta = 0. | var i, p = [], angle, xr, yr, delta = 0.00; | ||
// Random points are constructed which lie roughly on a line | // Random points are constructed which lie roughly on a line | ||
Line 23: | Line 23: | ||
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(), 1.0]); | ||
y.push(p[i].X()*p[i].X() + p[i].Y()*p[i].Y()); | y.push(1+p[i].X()*p[i].X() + p[i].Y()*p[i].Y()); | ||
} | } | ||
Line 39: | Line 39: | ||
// 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[ | var zm = z[0]*0.5; | ||
var ym = z[ | var xm = z[1]*0.5; | ||
var r = Math.sqrt(z[ | var ym = z[2]*0.5; | ||
var r = Math.sqrt(z[3]+zm*zm+xm*xm+ym*ym); | |||
brd.create('circle',[ [xm,ym], r]); | brd.create('circle',[ [zm,xm,ym], r]); | ||
//alert([xm,ym,r].toString()); | //alert([xm,ym,r].toString()); | ||
/* | /* |
Revision as of 11:48, 7 November 2010
This little JXSGraph application finds the line - described by homogeneous coordinates [a,b,c] - that minimizes
- [math]\displaystyle{ \sum_{i=1}^n (ax_i+by_i+cz_i)^2. }[/math]
Coming soon...