Skip to content

Commit 553375c

Browse files
authored
feat(analysis): create solver analysis script
- Implements 'analyzeSolvers' function to run benchmarks. - Loops through specified matrix sizes and types. - Generates matrices using 'utils.generateMatrices'. - Runs each specified solver using 'tic'/'toc' for timing. - Calls 'linearSolvers.checkConvergence' for iterative methods to skip non-convergent cases (based on spectral radius). - Records time, iteration count (for iterative), convergence status, and any errors. - Stores results in a MATLAB table. - Optionally saves the results table to a .mat file.
1 parent 612319a commit 553375c

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

analyzeSolvers.m

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
% analyzeSolvers.m
2+
% Runs timing and iteration analysis for linear solvers.
3+
4+
function results_table = analyzeSolvers(sizes, matrix_types, methods_to_test, tol, max_iter, results_file)
5+
% Add package path
6+
addpath(genpath(pwd));
7+
8+
% Define which methods are iterative
9+
iterative_methods = {'Jacobi', 'GaussSeidel', 'SOR'};
10+
11+
results = []; % Use a cell array to accumulate results
12+
13+
for n = sizes
14+
fprintf('Analyzing matrix size n = %d...\n', n);
15+
for m_type = matrix_types
16+
fprintf(' Matrix Type: %s...\n', m_type);
17+
18+
% Generate matrix and vector
19+
try
20+
A = utils.generateMatrices(n, m_type);
21+
b = rand(n, 1);
22+
catch ME
23+
fprintf(' Skipping matrix generation error: %s\n', ME.message);
24+
continue;
25+
end
26+
27+
for method_name_cell = methods_to_test
28+
method_name = method_name_cell{:}; % Get string from cell
29+
fprintf(' Testing Method: %s...\n', method_name);
30+
31+
time_taken = NaN;
32+
iterations = NaN; % Only for iterative methods
33+
converged = true; % Assume success unless failure occurs
34+
error_msg = '';
35+
36+
try
37+
% Check applicability/convergence for iterative methods
38+
is_iterative = ismember(method_name, iterative_methods);
39+
can_run = true;
40+
if is_iterative
41+
% Use spectral radius check for convergence guarantee
42+
% Using omega=1.2 as a default for SOR check if needed
43+
omega_check = 1.2;
44+
if ~linearSolvers.checkConvergence(A, method_name, omega_check)
45+
warning('Skipping %s for n=%d (%s matrix): Convergence condition (spectral radius >= 1) not met.', method_name, n, m_type);
46+
can_run = false;
47+
error_msg = 'Convergence Fail';
48+
end
49+
end
50+
51+
if can_run
52+
tic; % Start timer
53+
switch method_name
54+
case 'GaussianElimination'
55+
x = linearSolvers.gaussianElimination(A, b);
56+
case 'GaussJordanElimination'
57+
x = linearSolvers.gaussJordanElimination(A, b);
58+
case 'LUDecomposition'
59+
x = linearSolvers.luDecomposition(A, b);
60+
case 'Jacobi'
61+
[x, iterations, final_error] = linearSolvers.jacobi(A, b, [], tol, max_iter);
62+
if final_error >= tol; converged = false; error_msg = sprintf('Did not converge (err=%e)', final_error); end
63+
case 'GaussSeidel'
64+
[x, iterations, final_error] = linearSolvers.gaussSeidel(A, b, [], tol, max_iter);
65+
if final_error >= tol; converged = false; error_msg = sprintf('Did not converge (err=%e)', final_error); end
66+
case 'SOR'
67+
omega = 1.2; % Example omega, could be optimized
68+
[x, iterations, final_error] = linearSolvers.sor(A, b, omega, [], tol, max_iter);
69+
if final_error >= tol; converged = false; error_msg = sprintf('Did not converge (err=%e)', final_error); end
70+
case 'MATLAB Backslash'
71+
x = A \ b;
72+
end
73+
time_taken = toc; % Stop timer
74+
end
75+
76+
catch ME
77+
warning('Error running %s for n=%d (%s matrix): %s', method_name, n, m_type, ME.message);
78+
error_msg = ME.identifier; % Record error type
79+
converged = false;
80+
end
81+
82+
% Append result
83+
results = [results; {method_name, n, m_type, time_taken, iterations, converged, error_msg}];
84+
end % End methods loop
85+
end % End matrix types loop
86+
end % End sizes loop
87+
88+
% Convert cell array to table
89+
results_table = cell2table(results, ...
90+
'VariableNames', {'Method', 'Size', 'MatrixType', 'Time_s', 'Iterations', 'Converged', 'Error'});
91+
92+
% Save results
93+
if nargin >= 6 && ~isempty(results_file)
94+
try
95+
save(results_file, 'results_table');
96+
fprintf('\nResults saved to %s\n', results_file);
97+
catch ME
98+
fprintf('\nError saving results: %s\n', ME.message);
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)