使用norm()函数时出现矩阵维度错误:Richardson迭代求解线性系统问题
norm() Dimension Error in Your MATLAB Richardson Iteration Function Hey there, let’s work through that frustrating dimension error you’re hitting with norm() in your Richardson iteration code. I’ve dealt with this exact kind of issue when implementing iterative solvers in MATLAB, so let’s break down the most likely culprits and fix them.
First, a quick recap of the Richardson iteration core formula to ground us:
( x_{k+1} = x_k + \alpha(b - A x_k) )
All terms here need to be consistently dimensioned (either all column vectors or all row vectors, though column vectors are standard in linear algebra for this use case). Thenorm()error almost always stems from a mismatch in these dimensions somewhere in your iteration loop.
Common Causes & Fixes
Initial vector
x0is a row vector instead of column vector
MATLAB treats row and column vectors as distinct, so ifx0is a row vector (e.g.,[0 0 0]), multiplying by your n×n matrixAwill result in a row vector—butbis likely a column vector. Subtracting these (b - A*x0) creates a dimension mismatch, and feeding that messed-up result intonorm()throws the error.
Fix: Forcex0(andb) to be column vectors at the start of your function with:x0 = x0(:); b = b(:);Your alpha parameter
ais not a scalar
If you accidentally passed a vector or matrix fora, multiplying it by(b - A*xk)will trigger broadcasting issues, leading to a non-vector result thatnorm()can’t handle correctly.
Fix: Double-check thatais a single numerical value (e.g.,0.2instead of[0.2]or a larger array).Incorrect matrix multiplication order
If you wrotexk*Ainstead ofA*xk, you’ll end up with a row vector (ifxkis column) or a column vector (ifxkis row) that doesn’t matchb’s dimension. This breaks the subtraction step, andnorm()chokes on the invalid result.
Fix: Stick to the standard order:A*xkto get a vector of the same dimension asb.Unchecked input dimensions
It’s easy to pass anAthat doesn’t match the length ofx0orb(e.g., a 3×3 matrix with a 2-elementx0). This will immediately cause a multiplication error that propagates tonorm().
Fix: Add a quick check at the start of your function:n = length(x0); if size(A,1) ~= n || size(A,2) ~= n error('Matrix A must be n×n where n is the length of x0 and b'); end
Corrected Example Function
Here’s a revised version of your function that addresses all these points:
function [x, k] = richardson_iter(x0, L, A, b, a, tol) % Standardize all vectors to column vectors x0 = x0(:); b = b(:); n = length(x0); % Validate input dimensions if size(A,1) ~= n || size(A,2) ~= n error('Matrix A must match the dimension of x0 and b'); end if ~isscalar(a) error('Alpha parameter a must be a scalar value'); end x = x0; k = 0; while k < L % Compute residual (b - Ax) residual = b - A*x; % Check convergence using residual norm if norm(residual) < tol break; end % Richardson update step x = x + a*residual; k = k + 1; end % Warn if max iterations reached without convergence if k == L warning('Maximum iteration limit L reached; did not converge to specified tolerance'); end end
Test It Out
Try calling it with a simple valid case to verify:
A = [4 1; 1 4]; % 2×2 non-singular matrix b = [5; 5]; % 2D column vector x0 = [0; 0]; % Initial guess (column vector) L = 100; % Max iterations a = 0.2; % Alpha parameter (scalar) tol = 1e-6; % Convergence tolerance [x, k] = richardson_iter(x0, L, A, b, a, tol);
This should run without dimension errors and return the correct solution x = [1; 1] in just a few iterations.
内容的提问来源于stack exchange,提问作者Matt Robbins

