Program to Find Normal and Trace of a Matrix in PHP using For loop
ADVERTISEMENTS
Program to find normal and trace of a matrix in PHP using for loop. In this article, you will learn how to make program to find normal and trace of a matrix in php using for loop.
What is the Normal of a square matrix?
The square root of the sum of the squares of each element of the matrix.
What is the Trace of a square matrix?
The sum of the diagonal elements of the square matrix.
Source Code
<?php
// Program to Find Normal and Trace of a Matrix in PHP using For loop
$x = array(
array(43, 54, 4),
array(3, 54, 23),
array(54, 23, 5)
);
// $x - To store the matrix elements of 3 x 3 dimension
$sum = 0;
$sum1 = 0;
$a = 0;
for ($i = 0; $i < count ($x); $i++) {
for ($j = 0; $j < count ($x[$i]); $j++) {
$a = $x[$i][$j] * $x[$i][$j];
$sum1 += $a;
}
$sum = $sum + $x[$i][$i];
}
$normal = sqrt ($sum1);
echo "The normal of the given matrix is: ".$normal;
echo "\n\nThe Trace of the given matrix is: ".$sum."\n";
?>
Output
The normal of the given matrix is: 108.18964830334
The Trace of the given matrix is: 102