Numeric Analysis (PHP implementation) One: Lagrange interpolation
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.
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->ge T_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->d ot[' 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 ();? >
1/F luozhong915127 2011-11-17
It's all code, no explanation.