Learning numerical analysis, try to implement with PHP, interested friends can stick down to try
Lagrange interpolation is to give n known points, to fit the polynomial function, to find out the fitted function, and then to estimate the value of an unknown point function interpolation process
The interpolation function is f (x) = Xigma (sum) Lk*yk
This class implements the function of displaying function images after interpolation.
/*
* Lagrange interpolation
* @wangbin
* */
Class lagrange{
Private $dot = Array ();
Public function __construct ($array) {
$this->dot = $array;
}
/*
* Calculate LK
* */
Private Function Get_lk ($x, $k) {
$num = count ($this->dot[' x ');
$omiga _xk = 1;
$omiga _x = 1;
for ($i = 0; $i < $num; $i + +) {
if ($i! = $k) {
$omiga _xk *= ($this->dot[' x ' [$k]-$this->dot[' x ' [$i]);
$omiga _x *= ($x-$this->dot[' x ' [$i]);
}
}
$lk = $omiga _x/$omiga _xk;
return $lk;
}
Public Function get_y ($x) {
$num = count ($this->dot[' x ');
$L = 0;
for ($k = 0; $k < $num; $k + +) {
$lk = $this->get_lk ($x, $k);
$L + = ($this->dot[' y '] [$k]* $lk);
}
return $L;
}
Public Function Draw () {
$img = Imagecreate (500,500);
Imagecolorallocate ($img, 255,255,2);
$num = count ($this->dot[' x ');
$max = 0;
for ($i = 0; $i < $num; $i + +) $max = $max < $this->dot[' x ' [$i]? $this->dot[' x ' [$i]: $max;
$r = radius of the 7;//point
$step = 0.1;//Step
for ($i = 0; $i < $num; $i + +) {
Imagefilledellipse ($img, $this->dot[' x ' [$i], $this->dot[' y ' [$i], $r, $r, 1);
}
for ($i = $this->dot[' x '][0]-10; $i < $max +; $i + = $step) {
$current _x = $i;
$current _y = $this->get_y ($current _x);
$next _x = $i + $step;
$next _y = $this->get_y ($next _x);
Imageline ($img, $current _x, $current _y, $next _x, $next _y,1);
}
Header ("Content-type:image/png");
Imagepng ($IMG);
Imagedestroy ($IMG);
}
}
$x = new Lagrange (Array (' X ' =>array (10,30,100,120,220), ' Y ' =>array (20,40,240,220,340)));
$x->draw ();
?>
Excerpted from Pcenshao
http://www.bkjia.com/PHPjc/478527.html www.bkjia.com true http://www.bkjia.com/PHPjc/478527.html techarticle Learning numerical analysis, try to implement with PHP, interested friends can stick down to try Lagrange interpolation is to give n known points, with polynomial function fitting, to find out the function of fitting, into ...