PHP Classes
elePHPant
Icontem

File: examples/TestPoly.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jose Gomez  >  Linear Fit  >  examples/TestPoly.php  >  Download  
File: examples/TestPoly.php
Role: Example script
Content type: text/plain
Description: Polynomial fitting example
Class: Linear Fit
Perform linear regression on a set of data values
Author: By
Last change:
Date: 1 year ago
Size: 1,995 bytes
 

Contents

Class file image Download
<?
   
require('../PolyFit.php');

   
$oFit=new PolyFit;
   
$oFit->SetData(array(0,1,2,3), array(0,1,2,3));
   
$oFit->AddData(4, 4);
   
$oFit->SetDegree(2);
   
$aCoeffs=$oFit->Fit();
   
$aConf=$oFit->ConfInterval(0.95);
   
$aRes=$oFit->GetValues(5);
    if (
php_sapi_name()=='cli')
    {
        echo
"Equation: y=a0";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a$i*x^$i";
        }
        echo
"\n";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a$i=" . $aCoeffs[$i] . "±" . $aConf[$i]. "\n";
        }
        echo
"r2=" . $oFit->R2() . "\n";
        echo
"x=5 -> y=" . $aRes . "\n";
    }
    else
    {
        echo
"Equation $iEq: y=a<sub>0</sub>";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a<sub>$i</sub>*x<sup>$i</sup>";
        }
        echo
"<br>";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a<sub>$i</sub>=" . $aCoeffs[$i] . "&#177;" . $aConf[$i] . "</br>";
        }
        echo
"r<sup>2</sup>=" . $oFit->R2() . "</br>";
        echo
"x=5 -> y=" . $aRes . "</br>";
    }

   
$oFit->SetData(array(0,1,2,3), array(array(0,1),array(1,2),array(2,3),array(3,4)));
   
$oFit->AddData(4, array(4,5));
   
$oFit->SetDegree(1);
   
$aCoeffs=$oFit->Fit();
   
$aConf=$oFit->ConfInterval(0.95);
   
$aRes=$oFit->GetValues(5);
    if (
php_sapi_name()=='cli')
    {
        for (
$iEq=0; $iEq<count($aCoeffs[0]);$iEq++)
        {
        echo
"Equation $iEq: y=a0";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a$i*x^$i";
        }
        echo
"\n";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a$i=" . $aCoeffs[$i][$iEq] . "±" . $aConf[$i][$iEq]. "\n";
        }
        echo
"r2=" . $oFit->R2()[$iEq] . "\n";
        echo
"x=5 -> y=" . $aRes[$iEq] . "\n";
        }
    }
    else
    {
        for (
$iEq=0; $iEq<count($aCoeffs[0]);$iEq++)
        {
        echo
"Equation $iEq: y=a<sub>0</sub>";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a<sub>$i</sub>*x<sup>$i</sup>";
        }
        echo
"<br>";
        for (
$i=0; $i<count($aCoeffs); $i++)
        {
            echo
"a<sub>$i</sub>=" . $aCoeffs[$i][$iEq] . "&#177;" . $aConf[$i][$iEq] . "</br>";
        }
        echo
"r<sup>2</sup>=" . $oFit->R2()[$iEq] . "</br>";
        echo
"x=5 -> y=" . $aRes[$iEq] . "</br>";
        }
    }

?>