For the following two problems, create two codes for each. The first way is to u
ID: 3678442 • Letter: F
Question
For the following two problems, create two codes for each. The first way is to use the simple Matlab matrix algebra rules, i.e. if you're multiplying a matrix L by a vector xi, it's just L*xi. For the second way, create for loops that perform the same multiplication old-school style
A = [2 3 -1; 1 -2 1; 1 -1 1];b = [3 1 1]';
A = [1 -2 1; 2 3 -1; 1 -1 1];b = [1 3 1]';
A
U = triu(A,1)L = tril(A,-1)D = diag(diag(A))
Di = inv(D);
eps = 1e-3;err = 1;i = 1;
x = [0 0 0]';
while err > eps xn = Di*(b-(L+U)*x); err = max(abs(xn-x));
disp(['Iteration ' num2str(i) ' error ' num2str(err)]); i = i + 1; x = xn;end
Use the above code to solve a large matrix. Return the iteration count to solve this problem and a plot of the solution.
Modify the Jacobi method to implement the Gauss-Seidel method) and solve the same matrix from above. Again include the iteration count and solution plot.
Explanation / Answer
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Jacobi_Matrix
{
public static final int MAX_ITERATIONS = 100;
private double[][] M;
public Jacobi_Matrix(double [][] matrix) { M = matrix; }
public void print()
{
int n = M.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n + 1; j++)
System.out.print(M[i][j] + " ");
System.out.println();
}
}
public boolean trans_domiant(int r, boolean[] V, int[] R)
{
int n = M.length;
if (r == M.length) {
double[][] T = new double[n][n+1];
for (int i = 0; i < R.length; i++) {
for (int j = 0; j < n + 1; j++)
T[i][j] = M[R[i]][j];
}
M = T;
return true;
}
for (int i = 0; i < n; i++) {
if (V[i]) continue;
double sum = 0;
for (int j = 0; j < n; j++)
sum += Math.abs(M[i][j]);
if (2 * Math.abs(M[i][r]) > sum)
{
V[i] = true;
R[r] = i;
if (trans_domiant(r + 1, V, R))
return true;
V[i] = false;
}
}
return false;
}
public boolean Dominant()
{
boolean[] visited = new boolean[M.length];
int[] rows = new int[M.length];
Arrays.fill(visited, false);
return trans_domiant(0, visited, rows);
}
public void solve()
{
int iterations = 0;
int n = M.length;
double epsilon = 1e-15;
double[] X = new double[n];
double[] P = new double[n];
Arrays.fill(X, 0);
Arrays.fill(P, 0);
while (true) {
for (int i = 0; i < n; i++) {
double sum = M[i][n]; // b_n
for (int j = 0; j < n; j++)
if (j != i)
sum -= M[i][j] * P[j];
X[i] = 1/M[i][i] * sum;
}
System.out.print("X_" + iterations + " = {");
for (int i = 0; i < n; i++)
System.out.print(X[i] + " ");
System.out.println("}");
iterations++;
if (iterations == 1) continue;
boolean stop = true;
for (int i = 0; i < n && stop; i++)
if (Math.abs(X[i] - P[i]) > epsilon)
stop = false;
if (stop || iterations == MAX_ITERATIONS) break;
P = (double[])X.clone();
}
}
public static void main(String[] args) throws IOException
{
int n;
double[][] M;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out, true);
n = Integer.parseInt(reader.readLine());
M = new double[n][n+1];
for (int i = 0; i < n; i++) {
StringTokenizer strtk = new StringTokenizer(reader.readLine());
while (strtk.hasMoreTokens())
for (int j = 0; j < n + 1 && strtk.hasMoreTokens(); j++)
M[i][j] = Integer.parseInt(strtk.nextToken());
}
Jacobi_Matrix Jacobi_Matrix = new Jacobi_Matrix(M);
if (!Jacobi_Matrix.Dominant()) {
writer.println("The system isn't diagonally dominant: " +
"The method cannot guarantee convergence.");
}
writer.println();
Jacobi_Matrix.print();
Jacobi_Matrix.solve();
}
}
In the Jacobi method the values of the approximations xi are used up to the next iteration of the method while in the Gauss-Seidel version each approximation xi is used
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.