Numerical analysis (PHP redemption) Two: two solutions to the linear equation group

Source: Internet
Author: User
Numerical Analysis (PHP implementation) Two: two solutions to the linear equation group
In fact, this program is written well, but there is no time to write up.
These two programs are the method of solving the solution of linear equations by iterative method, one is Gaussian-Sidell iterative method, one is Jacobian iterative method
 Matrix = $array;        Public function Solve () {$preX = array ();        $nowX = Array ();        $cishu = 17;        $delta = 0.0001;        $matN = count ($this->matrix);        for ($i = 0; $i < $matN; $i + +) {$preX [$i] = 1;        } $min = 100000000;                for ($n = 0; $n < $cishu; $n + +) {for ($i = 0; $i < $matN; $i + +) {//xi $sum 1 = 0;                $sum 2 = 0;                    for ($j = 0; $j < $matN; $j + +) {if ($j < $i) $sum 1 + = ($this->matrix[$i] [$j] * $nowX [$j]);                if ($j > $i) $sum 2 + = ($this->matrix[$i] [$j] * $preX [$j]);                } $nowX [$i] = ($this->matrix[$i] [$matN]-$sum 1-$sum 2)/$this->matrix[$i] [$i]; $tempMin = $nowX [$i] > $preX [$i]?                $nowX [$i]-$preX [$i]: $preX [$i]-$nowX [$i];            if ($min > $tempMin) $min = $tempMin;            } $preX = $nowX;            $str = Implode (",", $nowX); Echo ($n + 1). ":( $str)"." 
"; if ($min < $delta) break; }}} $a = Array (array (5,2,1,-12), Array ( -1,4,2,20), Array (12,-3,10,3)), $x = new Gs ($a); $x->solve (); >

Next is the Jacobian method,
!--? phpclass yacobi{private $matrix;    Public function __construct ($array) {$this--->matrix = $array;        Public function Solve () {$preX = array ();        $nowX = Array ();        $cishu = 17;        $matN = count ($this->matrix);        for ($i = 0; $i < $matN; $i + +) {$preX [$i] = 1;                } for ($n = 0; $n < $cishu; $n + +) {for ($i = 0; $i < $matN; $i + +) {//xi $sum = 0;                for ($j = 0; $j < $matN; $j + +) {if ($j! = $i) $sum + = ($this->matrix[$i] [$j] * $preX [$j]);            } $nowX [$i] = ($this->matrix[$i] [$matN]-$sum)/$this->matrix[$i] [$i];            } $preX = $nowX;            $str = Implode (",", $nowX); Echo ($n + 1). ":( $STR) "."        
"; }}} $a = Array (array (5,2,1,-12), Array ( -1,4,2,20), Array (12,-3,10,3)), $x = new Yacobi ($a); $x->solve ();
  • 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.