PHP Classes
elePHPant
Icontem

File: examples/TestFunc.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/TestFunc.php  >  Download  
File: examples/TestFunc.php
Role: Example script
Content type: text/plain
Description: Functional lineal fitting example
Class: Linear Fit
Perform linear regression on a set of data values
Author: By
Last change:
Date: 1 year ago
Size: 2,269 bytes
 

Contents

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

   
$oFit=new FunctionFit;
   
$oFit->SetData(array(0,1,2,3), array(0,1,2,3));
   
$oFit->AddData(4, 4);
   
$aF=array('_x', '_x2');
   
$oFit->SetFunctions($aF);
   
$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*" . $aF[$i-1] . "(x)";
        }
        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: y=a<sub>0</sub>";
        for (
$i=1; $i<count($aCoeffs); $i++)
        {
            echo
"+a<sub>$i</sub>*" . $aF[$i-1] . "(x)";
        }
        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));
   
$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*" . $aF[$i-1] . "(x)";
        }
        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>*" . $aF[$i-1] . "(x)";
        }
        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>";
        }
    }
    function
_x($adX)
    {
        return
$adX;
    }

    function
_x2($adX)
    {
        if (!
is_array($adX))
        {
           
$adXr= $adX*$adX;
        }
        else
        {
           
$adXr=array();
            foreach (
$adX as $dX )
               
$adXr[]=$dX*$dX;

        }
        return
$adXr;
    }
?>