Least-squares line fitting: Difference between revisions

From JSXGraph Wiki
No edit summary
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 = 3.5;
var i, p = [], angle, xr, yr, delta = 0.1;


// Random points are constructed which lie roughly on a line
// Random points are constructed which lie roughly on a line
Line 14: Line 14:
brd.suspendUpdate();
brd.suspendUpdate();
for (i=0;i<5;i++) {
for (i=0;i<5;i++) {
   xr = 10*(Math.random()-0.5);
   yr = 10*(Math.random()-0.5);
   yr = 0.*xr+delta*(Math.random()-0.5) +1.0;
   xr = 0.*xr+delta*(Math.random()-0.5) +1.0;
   p.push(brd.create('point',[xr, yr], {withLabel:false}));
   p.push(brd.create('point',[xr, yr], {withLabel:false}));
}
}

Revision as of 11:15, 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...