ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Iterative Refinement for Linear Systems (LU Decomposition)

MATLAB

Goal -- WPM

Ready
Exercise Algorithm Area
1function [x_refined, num_iterations] = iterativeRefinementLU(A, b)
2% Iterative Refinement for solving Ax = b using LU decomposition.
3% Improves the accuracy of the solution by repeatedly correcting it.
4
5[m, n] = size(A);
6
7% Input validation: Ensure A is square and dimensions match b
8if m ~= n
9error('IterativeRefinementLU:MatrixNotSquare', 'Matrix A must be square.');
10end
11if size(b, 1) ~= m || size(b, 2) ~= 1
12error('IterativeRefinementLU:VectorDimensionMismatch', 'Vector b must have dimensions matching A (m x 1).');
13end
14
15% Tolerance for stopping criterion
16tol = 1e-10;
17max_iterations = 10;
18
19% Step 1: Compute initial solution using LU decomposition
20% Perform LU decomposition with partial pivoting for stability
21[L, U, P_perm] = lu(A);
22
23% Check for singularity (or near-singularity)
24if abs(det(A)) < eps('double') * max(m, n)
25warning('IterativeRefinementLU:MatrixSingular', 'Matrix A is singular or near-singular. Results may be inaccurate.');
26% Attempt to proceed, but results might be unreliable.
27end
28
29% Solve Ly = Pb for y (forward substitution)
30Pb = P_perm * b;
31y = linsolve(L, Pb);
32
33% Solve Ux = y for x (backward substitution)
34x_current = linsolve(U, y);
35
36% Initialize refined solution and iteration count
37x_refined = x_current;
38num_iterations = 0;
39
40% Step 2: Iterative Refinement Loop
41for iter = 1:max_iterations
42num_iterations = iter;
43
44% Calculate the residual: r = b - A * x_current
45% Use higher precision if available, or careful calculation
46residual = b - A * x_current;
47
48% Check for convergence
49residual_norm = norm(residual);
50if residual_norm < tol * norm(b)
51break; % Converged
52end
53
54% Step 3: Solve for the correction delta_x
55% Solve L * U * delta_x = residual (using the same LU factors)
56% Solve L * y_corr = residual (forward substitution)
57y_corr = linsolve(L, residual);
58
59% Solve U * delta_x = y_corr (backward substitution)
60delta_x = linsolve(U, y_corr);
61
62% Step 4: Update the solution
63x_current = x_current + delta_x;
64x_refined = x_current; % Update the best estimate
65end
66
67% Handle cases where max_iterations is reached without convergence
68if num_iterations == max_iterations && norm(b - A * x_refined) >= tol * norm(b)
69warning('IterativeRefinementLU:MaxIterationsReached', 'Maximum iterations reached without full convergence.');
70end
71
72end
73
74% Note: MATLAB's built-in lu() and linsolve() are highly optimized and numerically stable.
75% This implementation demonstrates the concept of iterative refinement.
76% For extremely ill-conditioned matrices, higher precision arithmetic (like symbolic math toolbox)
77% might be necessary for significant gains beyond standard double precision solvers.
Algorithm description viewbox

Iterative Refinement for Linear Systems (LU Decomposition)

Algorithm description:

This MATLAB function implements an iterative refinement procedure to enhance the accuracy of solutions to linear systems of equations (Ax = b). It starts with an initial solution obtained via LU decomposition and then iteratively refines this solution by calculating the residual and solving a modified system for a correction term. This method is particularly effective for ill-conditioned matrices where standard direct solvers might produce solutions with significant errors. Iterative refinement is widely used in numerical linear algebra to achieve high-precision solutions in scientific computing and engineering simulations.

Algorithm explanation:

Iterative refinement is a technique to improve the accuracy of a computed solution `x` to a linear system `Ax = b`. The process begins by obtaining an initial solution `x_0` using a direct method like LU decomposition. In each iteration, the residual `r = b - A*x_k` is computed. This residual is then used to find a correction `delta_x` by solving the system `A*delta_x = r` (often using the same factorization of A, e.g., LU). The solution is updated as `x_{k+1} = x_k + delta_x`. This process is repeated until the residual is sufficiently small or a maximum number of iterations is reached. The key to its effectiveness lies in computing the residual using higher precision arithmetic or by carefully managing precision to avoid catastrophic cancellation. This implementation uses MATLAB's double-precision arithmetic and its built-in `lu` and `linsolve` functions. The time complexity is dominated by the initial LU decomposition, which is O(n^3), and each refinement step is O(n^2) for residual calculation and O(n^2) for solving the correction system. The space complexity is O(n^2) for storing the LU factors. The algorithm includes checks for square matrices, dimension mismatches, and near-singularity, and uses a tolerance-based stopping criterion.

Pseudocode:

function iterativeRefinementLU(A, b):
  get dimensions m, n of A
  validate A is square and dimensions match b
  set tolerance `tol` and max_iterations
  
  // Step 1: Initial Solution
  compute LU decomposition of A with pivoting: [L, U, P] = lu(A)
  check if A is singular or near-singular
  Pb = P * b
  y = forward_substitution(L, Pb)
  x_current = backward_substitution(U, y)
  x_refined = x_current
  num_iterations = 0
  
  // Step 2: Iterative Refinement Loop
  for iter from 1 to max_iterations:
    num_iterations = iter
    
    // Calculate residual
    residual = b - A * x_current
    
    // Check for convergence
    if norm(residual) < tol * norm(b):
      break
      
    // Step 3: Solve for correction delta_x
    y_corr = forward_substitution(L, residual)
    delta_x = backward_substitution(U, y_corr)
    
    // Step 4: Update solution
    x_current = x_current + delta_x
    x_refined = x_current
    
  if max_iterations reached and not converged, issue warning
  
  return x_refined, num_iterations

// Helper functions forward_substitution and backward_substitution are assumed to exist and use L, U.