You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用norm()函数时出现矩阵维度错误:Richardson迭代求解线性系统问题

Troubleshooting the 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). The norm() error almost always stems from a mismatch in these dimensions somewhere in your iteration loop.

Common Causes & Fixes

  • Initial vector x0 is a row vector instead of column vector
    MATLAB treats row and column vectors as distinct, so if x0 is a row vector (e.g., [0 0 0]), multiplying by your n×n matrix A will result in a row vector—but b is likely a column vector. Subtracting these (b - A*x0) creates a dimension mismatch, and feeding that messed-up result into norm() throws the error.
    Fix: Force x0 (and b) to be column vectors at the start of your function with:

    x0 = x0(:);
    b = b(:);
    
  • Your alpha parameter a is not a scalar
    If you accidentally passed a vector or matrix for a, multiplying it by (b - A*xk) will trigger broadcasting issues, leading to a non-vector result that norm() can’t handle correctly.
    Fix: Double-check that a is a single numerical value (e.g., 0.2 instead of [0.2] or a larger array).

  • Incorrect matrix multiplication order
    If you wrote xk*A instead of A*xk, you’ll end up with a row vector (if xk is column) or a column vector (if xk is row) that doesn’t match b’s dimension. This breaks the subtraction step, and norm() chokes on the invalid result.
    Fix: Stick to the standard order: A*xk to get a vector of the same dimension as b.

  • Unchecked input dimensions
    It’s easy to pass an A that doesn’t match the length of x0 or b (e.g., a 3×3 matrix with a 2-element x0). This will immediately cause a multiplication error that propagates to norm().
    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:40:36