<?php
|
/*---------------------------------------------------------------*/
|
/*
|
Titre : Régression Polynomiale de degré 4
|
|
URL : https://phpsources.net/code_s.php?id=891
|
Date édition : 14 Fév 2019
|
*/
|
/*---------------------------------------------------------------*/
|
|
function regression_poly_4($x , $y){
|
// Les abscisses et ordonnées sont envoyées
|
// sous forme de 2 tableaux
|
|
// $x[]=0;
|
// $x[]=1;
|
// $x[]=2;
|
// $x[]=3;
|
// $x[]=4;
|
// $x[]=5;
|
|
// $y[]=0;
|
// $y[]=2;
|
// $y[]=4;
|
// $y[]=5;
|
// $y[]=4;
|
// $y[]=6;
|
|
|
// Cette fonction requiert les fonctions suivantes :
|
// (disponibles sur nexen.net )
|
|
// Calcul d'une matrice mineure de Pierre Beaumadier
|
// minor_matrix($m, $i, $j)
|
|
function minor_matrix($m, $i, $j){
|
if (!(is_array($m)))
|
return ("Veuillez passer une matrice");
|
$nbl = sizeof($m);
|
if ($nbl < 1)
|
return ("Veuillez passer une vraie matrice");
|
$nbc = sizeof($m[0]);
|
for ($cpt = 1; $cpt < $nbl; $cpt++)
|
if (sizeof($m[$cpt]) != $nbc)
|
return ("Veuillez passer une vraie matrice");
|
if ($nbl == 1)
|
return ("Veuillez passer une matrice de taille >= 2");
|
if (($i < 1) || ($j < 1) || ($i > $nbl) || ($j > $nbc))
|
return ("Veuillez passer des valeurs correctes pour les 2 et 3e parametres");
|
$ret = array();
|
for ($cptl = 0, $a = 0; $cptl < $nbl; $cptl++)
|
{
|
for ($cptc = 0, $b = 0; $cptc < $nbc; $cptc++)
|
{
|
if (($cptl != $i - 1) && ($cptc != $j - 1))
|
$ret[$a][$b] = $m[$cptl][$cptc];
|
if ($cptc != $j - 1)
|
$b++;
|
}
|
if ($cptl != $i - 1)
|
$a++;
|
}
|
return ($ret);
|
}
|
|
// Calcul du déterminant d'une matrice de Pierre Beaumadier
|
// det_matrix($m)
|
|
function det_matrix($m){
|
if (!(is_array($m)))
|
return ("Veuillez passer une matrice");
|
$nbl = sizeof($m);
|
if ($nbl < 1)
|
return ("Veuillez passer une vraie matrice");
|
$nbc = sizeof($m[0]);
|
for ($i = 1; $i < $nbl; $i++)
|
if (sizeof($m[$i]) != $nbc)
|
return ("Veuillez passer une vraie matrice");
|
if ($nbl == 1)
|
return $m[0][0];
|
if ($nbl == 2)
|
return ($m[0][0]*$m[1][1] - $m[1][0]*$m[0][1]);
|
if ($nbl == 3)
|
return ($m[0][0]*$m[1][1]*$m[2][2] + $m[0][1]*$m[1][2]*$m[2][0] + $m[0][2]*$m[
|
1][0]*$m[2][1] - $m[0][1]*$m[1][0]*$m[2][2] - $m[0][0]*$m[1][2]*$m[2][1] - $m[0]
|
[2]*$m[1][1]*$m[2][0]);
|
$det = 0;
|
for ($i = 0; $i < $nbl; $i++)
|
$det += pow(-1, $i+1) * $m[$i][0] * det_matrix(minor_matrix($m, $i + 1, 1));
|
return ($det);
|
}
|
|
//Fonction qui permet d'afficher correctement
|
//le signe des coefficients de la régression
|
|
function signe($nbr){
|
|
$tmp=explode('-', $nbr);
|
|
if(empty($tmp[1])){
|
|
$end = "+".number_format($nbr, 3, ', ', ' ');
|
return $end ;
|
|
}else{
|
|
$end = number_format($nbr, 3, ', ', ' ');
|
return $end ;
|
}
|
|
}
|
|
echo "<font color=\"#666666\" size=\"1\" face=\"Tahoma\">";
|
|
//Nombre d'éléments
|
|
$nbx=count($x);
|
|
//Calcul des Ex...
|
|
$i=0;
|
|
while($i<$nbx){
|
|
$Ex1+=$x[$i];
|
$Ey1+=$y[$i];
|
|
$Ex2+=pow($x[$i], 2);
|
$Ex3+=pow($x[$i], 3);
|
$Ex4+=pow($x[$i], 4);
|
$Ex5+=pow($x[$i], 5);
|
$Ex6+=pow($x[$i], 6);
|
$Ex7+=pow($x[$i], 7);
|
$Ex8+=pow($x[$i], 8);
|
|
$Ex1y1+=$x[$i]*$y[$i];
|
$Ex2y1+=pow($x[$i], 2)*$y[$i];
|
$Ex3y1+=pow($x[$i], 3)*$y[$i];
|
$Ex4y1+=pow($x[$i], 4)*$y[$i];
|
|
$i++;
|
}
|
echo "<b>Calculs Prélimminaires des Sommes</b><br>";
|
echo "<br>";
|
echo "N = ".$nbx."<br><br>";
|
echo "Ex1 : ".$Ex1."<br>";
|
echo "Ey1 : ".$Ey1."<br><br>";
|
echo "Ex2 : ".$Ex2."<br>";
|
echo "Ex3 : ".$Ex3."<br>";
|
echo "Ex4 : ".$Ex4."<br>";
|
echo "Ex5 : ".$Ex5."<br>";
|
echo "Ex6 : ".$Ex6."<br>";
|
echo "Ex7 : ".$Ex7."<br>";
|
echo "Ex8 : ".$Ex8."<br><br>";
|
echo "Ex1y1 : ".$Ex1y1."<br>";
|
echo "Ex2y1 : ".$Ex2y1."<br>";
|
echo "Ex3y1 : ".$Ex3y1."<br>";
|
echo "Ex4y1 : ".$Ex4y1."<br>";
|
|
//Construction des matrices
|
|
//Matrice Principale
|
|
$m4p[0][0]=$Ex8; $m4p[1][0]=$Ex7; $m4p[2][0]=$Ex6; $m4p[3][0]=$Ex5;
|
$m4p[4][0]=$Ex4;
|
$m4p[0][1]=$Ex7; $m4p[1][1]=$Ex6; $m4p[2][1]=$Ex5; $m4p[3][1]=$Ex4;
|
$m4p[4][1]=$Ex3;
|
$m4p[0][2]=$Ex6; $m4p[1][2]=$Ex5; $m4p[2][2]=$Ex4; $m4p[3][2]=$Ex3;
|
$m4p[4][2]=$Ex2;
|
$m4p[0][3]=$Ex5; $m4p[1][3]=$Ex4; $m4p[2][3]=$Ex3; $m4p[3][3]=$Ex2;
|
$m4p[4][3]=$Ex1;
|
$m4p[0][4]=$Ex4; $m4p[1][4]=$Ex3; $m4p[2][4]=$Ex2; $m4p[3][4]=$Ex1;
|
$m4p[4][4]=$nbx;
|
|
//Matrice 1
|
|
$m41[0][0]=$Ex4y1; $m41[1][0]=$Ex7; $m41[2][0]=$Ex6; $m41[3][0]=$Ex5;
|
$m41[4][0]=$Ex4;
|
$m41[0][1]=$Ex3y1; $m41[1][1]=$Ex6; $m41[2][1]=$Ex5; $m41[3][1]=$Ex4;
|
$m41[4][1]=$Ex3;
|
$m41[0][2]=$Ex2y1; $m41[1][2]=$Ex5; $m41[2][2]=$Ex4; $m41[3][2]=$Ex3;
|
$m41[4][2]=$Ex2;
|
$m41[0][3]=$Ex1y1; $m41[1][3]=$Ex4; $m41[2][3]=$Ex3; $m41[3][3]=$Ex2;
|
$m41[4][3]=$Ex1;
|
$m41[0][4]=$Ey1; $m41[1][4]=$Ex3; $m41[2][4]=$Ex2; $m41[3][4]=$Ex1;
|
$m41[4][4]=$nbx;
|
|
//Matrice 2
|
|
$m42[0][0]=$Ex8; $m42[1][0]=$Ex4y1; $m42[2][0]=$Ex6; $m42[3][0]=$Ex5;
|
$m42[4][0]=$Ex4;
|
$m42[0][1]=$Ex7; $m42[1][1]=$Ex3y1; $m42[2][1]=$Ex5; $m42[3][1]=$Ex4;
|
$m42[4][1]=$Ex3;
|
$m42[0][2]=$Ex6; $m42[1][2]=$Ex2y1; $m42[2][2]=$Ex4; $m42[3][2]=$Ex3;
|
$m42[4][2]=$Ex2;
|
$m42[0][3]=$Ex5; $m42[1][3]=$Ex1y1; $m42[2][3]=$Ex3; $m42[3][3]=$Ex2;
|
$m42[4][3]=$Ex1;
|
$m42[0][4]=$Ex4; $m42[1][4]=$Ey1; $m42[2][4]=$Ex2; $m42[3][4]=$Ex1;
|
$m42[4][4]=$nbx;
|
|
//Matrice 3
|
|
$m43[0][0]=$Ex8; $m43[1][0]=$Ex7; $m43[2][0]=$Ex4y1; $m43[3][0]=$Ex5;
|
$m43[4][0]=$Ex4;
|
$m43[0][1]=$Ex7; $m43[1][1]=$Ex6; $m43[2][1]=$Ex3y1; $m43[3][1]=$Ex4;
|
$m43[4][1]=$Ex3;
|
$m43[0][2]=$Ex6; $m43[1][2]=$Ex5; $m43[2][2]=$Ex2y1; $m43[3][2]=$Ex3;
|
$m43[4][2]=$Ex2;
|
$m43[0][3]=$Ex5; $m43[1][3]=$Ex4; $m43[2][3]=$Ex1y1; $m43[3][3]=$Ex2;
|
$m43[4][3]=$Ex1;
|
$m43[0][4]=$Ex4; $m43[1][4]=$Ex3; $m43[2][4]=$Ey1; $m43[3][4]=$Ex1;
|
$m43[4][4]=$nbx;
|
|
//Matrice 4
|
|
$m44[0][0]=$Ex8; $m44[1][0]=$Ex7; $m44[2][0]=$Ex6; $m44[3][0]=$Ex4y1;
|
$m44[4][0]=$Ex4;
|
$m44[0][1]=$Ex7; $m44[1][1]=$Ex6; $m44[2][1]=$Ex5; $m44[3][1]=$Ex3y1;
|
$m44[4][1]=$Ex3;
|
$m44[0][2]=$Ex6; $m44[1][2]=$Ex5; $m44[2][2]=$Ex4; $m44[3][2]=$Ex2y1;
|
$m44[4][2]=$Ex2;
|
$m44[0][3]=$Ex5; $m44[1][3]=$Ex4; $m44[2][3]=$Ex3; $m44[3][3]=$Ex1y1;
|
$m44[4][3]=$Ex1;
|
$m44[0][4]=$Ex4; $m44[1][4]=$Ex3; $m44[2][4]=$Ex2; $m44[3][4]=$Ey1;
|
$m44[4][4]=$nbx;
|
|
//Matrice 5
|
|
$m45[0][0]=$Ex8; $m45[1][0]=$Ex7; $m45[2][0]=$Ex6; $m45[3][0]=$Ex5;
|
$m45[4][0]=$Ex4y1;
|
$m45[0][1]=$Ex7; $m45[1][1]=$Ex6; $m45[2][1]=$Ex5; $m45[3][1]=$Ex4;
|
$m45[4][1]=$Ex3y1;
|
$m45[0][2]=$Ex6; $m45[1][2]=$Ex5; $m45[2][2]=$Ex4; $m45[3][2]=$Ex3;
|
$m45[4][2]=$Ex2y1;
|
$m45[0][3]=$Ex5; $m45[1][3]=$Ex4; $m45[2][3]=$Ex3; $m45[3][3]=$Ex2;
|
$m45[4][3]=$Ex1y1;
|
$m45[0][4]=$Ex4; $m45[1][4]=$Ex3; $m45[2][4]=$Ex2; $m45[3][4]=$Ex1;
|
$m45[4][4]=$Ey1;
|
|
echo "<br>";
|
echo "<b>Régression polynomiale de degré 4</b><br>";
|
echo "<br>";
|
echo "Det Mp : ".$det_m4p = det_matrix($m4p);
|
echo "<br>";
|
echo "Det M1 : ".$det_m41 = det_matrix($m41);
|
echo "<br>";
|
echo "Det M2 : ".$det_m42 = det_matrix($m42);
|
echo "<br>";
|
echo "Det M3 : ".$det_m43 = det_matrix($m43);
|
echo "<br>";
|
echo "Det M4 : ".$det_m44 = det_matrix($m44);
|
echo "<br>";
|
echo "Det M5 : ".$det_m45 = det_matrix($m45);
|
echo "<br>";
|
|
echo "<br>";
|
echo "a4 : ".$a4[4] = $det_m41/$det_m4p;
|
echo "<br>";
|
echo "a3 : ".$a4[3] = $det_m42/$det_m4p;
|
echo "<br>";
|
echo "a2 : ".$a4[2] = $det_m43/$det_m4p;
|
echo "<br>";
|
echo "a1 : ".$a4[1] = $det_m44/$det_m4p;
|
echo "<br>";
|
echo "a0 : ".$a4[0] = $det_m45/$det_m4p;
|
echo "<br><br>";
|
|
//Affichage de l'équation
|
|
echo "<b>Y = ".signe($a4[4])." X4 ".signe($a4[3])." X³ ".signe($a4[2]).
|
" X²".signe($a4[1])." X".signe($a4[0])."</b>";
|
|
return $a4;
|
}
|
|
?>
|
|
|