Numerical Analysis (PHP implementation) One: Lagrange interpolation _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.