Polynomial regression I: Difference between revisions

From JSXGraph Wiki
No edit summary
No edit summary
Line 25: Line 25:
reg = brd.createElement('functiongraph',[regressionPolynomial],{strokeColor:'black'});     
reg = brd.createElement('functiongraph',[regressionPolynomial],{strokeColor:'black'});     
brd.createElement('glider',[100,30,reg],{strokeColor:'black'});     
brd.createElement('glider',[100,30,reg],{strokeColor:'black'});     
regD = brd.createElement('functiongraph',[brd.D(regressionPolynomial)],{strokeColor:'red'});   


t = brd.createElement('text',[20,40,
t = brd.createElement('text',[20,40,
Line 36: Line 35:
===The underlying JavaScript code===
===The underlying JavaScript code===
<source lang="xml">
<source lang="xml">
<jsxgraph width="700" height="500">
    data = "406.60 428.50 429.30 426.30 434.70 415.90 ... many points ... 521.80 524.40 526.80";
    data = data.split(' ');
    datax = [];
    for (i = 0; i < data.length; i++)  {
        data[i] = (parseFloat(data[i])-450.0);
        datax[i] = i;
        }
   
brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-2, 100, data.length+2, -70], grid: false, axis:true});
s = brd.createElement('slider',[[10,70],[100,70],[0,1,1]],{name:'scale'});   
deg = brd.createElement('slider',[[10,90],[100,90],[1,1,10]],{name:'degree',snapWidth:1});   
curve = brd.createElement('curve',[datax,[]],{strokeColor:'gray',dash:2});   
curve.updateDataArray = function() {
    var i, len = data.length;
    for(i=0;i<len;i++) { this.dataY[i] = s.Value()*data[i]; }
}
regressionPolynomial = JXG.Math.Numerics.regressionPolynomial(deg,datax,curve.dataY);  // generateFunction();
reg = brd.createElement('functiongraph',[regressionPolynomial],{strokeColor:'black'});   
brd.createElement('glider',[100,30,reg],{strokeColor:'black'});   
t = brd.createElement('text',[20,40,
            function(){ return "r(x) = " + regressionPolynomial.getTerm();}
            ],
            {strokeColor:'black',fontSize:'14px'});   
</jsxgraph>
</source>
</source>


[[Category:Examples]]
[[Category:Examples]]

Revision as of 15:11, 19 August 2009

Through the points defining the dashed line the regression polynomial of degree 'degree' is computed and displayed. The points can be scaled by adjusting the slider 'scale'. The degree of the regression polynomial can be changed by using the slider 'degree'.

The underlying JavaScript code

<jsxgraph width="700" height="500">

    data = "406.60 428.50 429.30 426.30 434.70 415.90 ... many points ... 521.80 524.40 526.80";
    data = data.split(' ');
    datax = [];
    for (i = 0; i < data.length; i++)  { 
        data[i] = (parseFloat(data[i])-450.0); 
        datax[i] = i;
        }
    

brd = JXG.JSXGraph.initBoard('jxgbox', {boundingbox:[-2, 100, data.length+2, -70], grid: false, axis:true});
s = brd.createElement('slider',[[10,70],[100,70],[0,1,1]],{name:'scale'});     
deg = brd.createElement('slider',[[10,90],[100,90],[1,1,10]],{name:'degree',snapWidth:1});     
curve = brd.createElement('curve',[datax,[]],{strokeColor:'gray',dash:2});     
curve.updateDataArray = function() {
    var i, len = data.length;
    for(i=0;i<len;i++) { this.dataY[i] = s.Value()*data[i]; }
}
regressionPolynomial = JXG.Math.Numerics.regressionPolynomial(deg,datax,curve.dataY);  // generateFunction();

reg = brd.createElement('functiongraph',[regressionPolynomial],{strokeColor:'black'});     
brd.createElement('glider',[100,30,reg],{strokeColor:'black'});     

t = brd.createElement('text',[20,40,
            function(){ return "r(x) = " + regressionPolynomial.getTerm();}
            ],
            {strokeColor:'black',fontSize:'14px'});     

</jsxgraph>