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 ();