Using fprintf to format a diagonally dominant matrix

2 views (last 30 days)
I have a code that uses the jacobi method to solve an equation system. For instance:
clear all
clc
Error = 1;
Matrix = input('Insert the dimension of the matrix (2x2 = 2, 3x3 = 3): ')
M_Equations = input('Insert the coeficients of the equations in order[a1 a2 an ; b1 b2 bn ; n1 n2 nn]: ')
M_Results = input('Insert the results of the equations in order [r1 ; r2 ; rn]: ')
Error_Permit = input('Write the error in decimals (.5): ')
for i=1:Matrix
x(i)=0;
end
x
while Error>=Error_Permit
for i=1:Matrix
Const =0;
for j=1:Matrix
if j==i
Const = Const;
else
Const =Const + M_Equations(i,j)*x(j);
Const
end
x_New(i)= (M_Results(i,1)- Const)/(M_Equations(i,i));
x_New(i)
Error_Ind(i)=abs((x_New(i)-x(i))/x_New(i));
end
Error = sum(Error_Ind(1:Matrix))/Matrix;
for i=1:Matrix
x(i)=x_New(i);
end
end
I want to sort the sequence of steps performed in the algorithm and send them to a diagonally dominant matrix. I was thinking of using fprintf but could think of a way to make it.
Help is greatly appreciated

Answers (1)

Himanshu
Himanshu on 10 Dec 2024
Hey,
To sort the sequence of steps performed in the Jacobi method and output them, you can surely use fprintf to print detailed information about each iteration. Ensuring the matrix is diagonally dominant before applying the Jacobi method is crucial for convergence. I have made some modifications to your code to achieve the same:
clear all;
clc;
% Input section
Matrix = input('Insert the dimension of the matrix (2x2 = 2, 3x3 = 3): ');
M_Equations = input('Insert the coefficients of the equations in order [a1 a2 an; b1 b2 bn; n1 n2 nn]: ');
M_Results = input('Insert the results of the equations in order [r1; r2; rn]: ');
Error_Permit = input('Write the error in decimals (.5): ');
% Initialize variables
x = zeros(Matrix, 1);
Error = 1;
% Check for diagonal dominance
for i = 1:Matrix
diag_elem = abs(M_Equations(i, i));
off_diag_sum = sum(abs(M_Equations(i, :))) - diag_elem;
if diag_elem <= off_diag_sum
fprintf('Warning: Row %d is not diagonally dominant.\n', i);
end
end
fprintf('\nStarting Jacobi Iteration:\n');
fprintf('Iteration\tSolution\tError\n');
iteration = 0;
while Error >= Error_Permit
iteration = iteration + 1;
for i = 1:Matrix
Const = 0;
for j = 1:Matrix
if j ~= i
Const = Const + M_Equations(i, j) * x(j);
end
end
x_New(i) = (M_Results(i) - Const) / M_Equations(i, i);
Error_Ind(i) = abs((x_New(i) - x(i)) / x_New(i));
end
Error = sum(Error_Ind) / Matrix;
% Print current iteration details
fprintf('%d\t\t', iteration);
fprintf('%.4f ', x_New);
fprintf('\t%.4f\n', Error);
% Update x for the next iteration
x = x_New;
end
fprintf('\nFinal Solution:\n');
disp(x);
Before starting the iterations, the above code checks for diagonal dominance in each row of the matrix, printing a warning if any row is not dominant. During each iteration, 'fprintf' outputs the iteration number, current solution, and error, allowing you to trace the computation step-by-step. The solution vector x is initialized to zeros and updated with new estimates x_New in each iteration, while the error is calculated as the average of relative errors across variables to determine convergence.

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!