PHP Classes

File: libs/Exel/PHPExcel/Shared/JAMA/examples/polyfit.php

Recommend this page to a friend!
  Classes of william amed   Raptor 2   libs/Exel/PHPExcel/Shared/JAMA/examples/polyfit.php   Download  
File: libs/Exel/PHPExcel/Shared/JAMA/examples/polyfit.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Raptor 2
Framework that takes routes from annotations
Author: By
Last change:
Date: 8 years ago
Size: 1,814 bytes
 

Contents

Class file image Download
<?php
require_once "../Matrix.php";
/*
* @package JAMA
* @author Michael Bommarito
* @author Paul Meagher
* @version 0.1
*
* Function to fit an order n polynomial function through
* a series of x-y data points using least squares.
*
* @param $X array x values
* @param $Y array y values
* @param $n int order of polynomial to be used for fitting
* @returns array $coeffs of polynomial coefficients
* Pre-Conditions: the system is not underdetermined: sizeof($X) > $n+1
*/
function polyfit($X, $Y, $n) {
    for (
$i = 0; $i < sizeof($X); ++$i)
        for (
$j = 0; $j <= $n; ++$j)
           
$A[$i][$j] = pow($X[$i], $j);
    for (
$i=0; $i < sizeof($Y); ++$i)
       
$B[$i] = array($Y[$i]);
   
$matrixA = new Matrix($A);
   
$matrixB = new Matrix($B);
   
$C = $matrixA->solve($matrixB);
    return
$C->getMatrix(0, $n, 0, 1);
}

function
printpoly( $C = null ) {
    for(
$i = $C->m - 1; $i >= 0; --$i) {
       
$r = $C->get($i, 0);
        if (
abs($r) <= pow(10, -9) )
           
$r = 0;
        if (
$i == $C->m - 1)
            echo
$r . "x<sup>$i</sup>";
        else if (
$i < $C->m - 1)
            echo
" + " . $r . "x<sup>$i</sup>";
        else if (
$i == 0)
            echo
" + " . $r;
    }
}

$X = array(0,1,2,3,4,5);
$Y = array(4,3,12,67,228, 579);
$points = new Matrix(array($X, $Y));
$points->toHTML();
printpoly(polyfit($X, $Y, 4));

echo
'<hr />';

$X = array(0,1,2,3,4,5);
$Y = array(1,2,5,10,17, 26);
$points = new Matrix(array($X, $Y));
$points->toHTML();
printpoly(polyfit($X, $Y, 2));

echo
'<hr />';

$X = array(0,1,2,3,4,5,6);
$Y = array(-90,-104,-178,-252,-26, 1160, 4446);
$points = new Matrix(array($X, $Y));
$points->toHTML();
printpoly(polyfit($X, $Y, 5));

echo
'<hr />';

$X = array(0,1,2,3,4);
$Y = array(mt_rand(0, 10), mt_rand(40, 80), mt_rand(240, 400), mt_rand(1800, 2215), mt_rand(8000, 9000));
$points = new Matrix(array($X, $Y));
$points->toHTML();
printpoly(polyfit($X, $Y, 3));
?>