A medium-complex Java program, that is designed and built for the purpose of Solving a System of Linear Equations.
The way this program works is as followed:
Suppose that you are given a linear equation and you are required to find all the unknown x values as follow:
A system of linear equations with m equations and n unknowns if and only if n=m is expressed as follows,
The above system is represented in matrix form as Ax= b, as follows,
To find the unknown variables x the following steps are being accomplished:
- Convert the linear equation to the matrix format:
The system takes TWO dimensional array Matrix A and a single dimensional array Vector X, and another single dimensional array Vector b
- The system will then check if the matrix A is diagonally dominant and Make the matrix diagonally dominant, if it is not.
Description: A square matrix A is said to be diagonally dominant if the absolute value of its diagonal element in each row is greater than the sum of the absolute value of all other elements in the same row.
Hint: The matrix A is not diagonally-dominant but becomes diagonally-dominant by exchanging the rows. In the example, we bring the row 4 to the first row and shifting all the other rows downward. Note that the vector b is also changed according to their corresponding rows in the matrix A. Therefore, the linear equation in the form of matrix will look like the followings:
- Find the x for each row. I have implemented the loop at this step and iteratively calculated the unknown variable x until the Greatest Absolute Value of each unknown x (Error) subtracted by the Greatest Absolute Value from the previous step is smaller than the epsilon (0.005. The initial vector x (at iteration k=0) is given as x-[0,0,0,0].
The definition of notations are as follows:
The variable X𝑟 (x for each row) at each iteration k, is written in terms of other variables as follows:
Where i and j is the index of row and column at each iteration.
The initial vector x is given as x= [0, 0, 0, 0]=> 𝑥1 = 0, 𝑥2 = 0, 𝑥3 = 0, 𝑥4 = 0.
Formula for calculating the unknown x:
General formula:
Example:
When k=0:
The error is greater than the tolerance. Therefore, the iterations continue with k=1. Continuing the iteration produces the results as shown in table below.
The pseudocode of the algorithm is as follows:
- Make sure you have successfully installed the Latest Version of Java JDK. You may download it here.
- Download the Repo source code files.
- If you have completed the above steps correctly, then you should be able to run it successfully.
[NOTE]
All the comments and the explaination of the Methods and the Classes has been added to the source code.
- Getting Dimensions (Success):
- Getting Dimensions (Unsuccessful):
- Getting Elements of Matrix, A and Vector B:
- Checking Diagonal Dominance (Diagonally Dominant):
- Checking Diagonal Dominance (Not Diagonally Dominant & Dominance is Possible):
- Checking Diagonal Dominance (Not Diagonally Dominant & Dominance is Impossible):
If you have suggestions, please feel free to contribute, looking forward to practice and learn more. Thanks!