It’s my first post about what i study in my college , it is about solving a system of equations using the two famous numerical methods of Jacobi and Gauss .
This post is not about these numerical methods themselves but it is about their PHP implementation .
This is the code for Jacobi :
<?php
$x = array(
array(0,-(1/3),-(1/3),-(8/3)),
array((1/3),0,-(1/3),(8/3)),
array(-(1/3),(1/3),0,-(8/3))
);
$s = array(
0,0,0
);
echo "<table border='1' style= 'width :90%'><tr>";
echo "<td> k = 0</td><td> 0</td><td> 0</td><td> 0</td></tr>";
for($i=1;$i<=30;$i++){
echo "<tr>";
$s0 = ( $s[0]*$x[0][0]) + ( $s[1]*$x[0][1])+($s[2]*$x[0][2]+$x[0][3]);
$s1 = ($s[0]*$x[1][0]) + ($s[1]*$x[1][1]) + ($s[2]*$x[1][2]+$x[1][3]);
$s2 = ( $s[0]*$x[2][0]) + ($s[1]*$x[2][1]) + ($s[2]*$x[2][2]+$x[2][3]);
$s[0] = $s0;
$s[1] = $s1;
$s[2] = $s2;
echo "<td> k = $i</td><td>".$s[0]."</td><td>".$s[1]."</td><td>".$s[2]."</td></tr>";
//echo $i." :: ".$s[0]." :: ".$s[1]." :: ".$s[2]."<br>";
}
echo "</table>";
?>
And this is the gauss-siedle
$x = array(
array(0,0.2,-0.6,-0.4),
array(-0.2,0,0.4,2),
array(-0.2,0.4,0,0.6)
);
$s = array(
0,0,0
);
echo "0 :: 0 :: 0 :: 0<br>";
for($i=1;$i<100;$i++){
$s[0] = $s[0]*$x[0][0] + $s[1]*$x[0][1] + $s[2]*$x[0][2]+$x[0][3];
$s[1] = $s[0]*$x[1][0] + $s[1]*$x[1][1] + $s[2]*$x[1][2]+$x[1][3];
$s[2] = $s[0]*$x[2][0] + $s[1]*$x[2][1] + $s[2]*$x[2][2]+$x[2][3];
echo $i." :: ".$s[0]." :: ".$s[1]." :: ".$s[2]."<br>";
}
?>
How to use it ?
just put the X coefficients into the $x array after putting it in the valid form !If you have a problem with approximation accuracy use the round() function
Advertisement


Numerical .. Allah yer7am Dr.Hani Hashesh ..
Nice post
الله يرحمه يا رب .. هو اللى كان هايدي لنا الكورس دة بس تعب فى اوله و جا بداله الدكتور مجدي العزب