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

使用Scipy无穷范数的高斯-赛德尔迭代次数偏多问题咨询

高斯-赛德尔法迭代次数异常问题排查

我用Numpy和Scipy实现了求解线性方程组的高斯-赛德尔法,参考书籍为《Numerical Analysis: Burden and Faires》中的示例。目前能得到精确解,但迭代次数偏多:容差设为0.0000001时需要10次迭代,而书中在容差0.001时仅用6次迭代就得到解。我认为问题出在使用Scipy计算无穷范数的误差判断逻辑上——当代码中不加入误差判断、仅固定迭代次数时,结果与书籍完全一致。

以下是我的Python代码:

import numpy as np
import scipy as scp


def gauss_seidel(A, b, x_0, max_iterations=15, tolerance=0.0000001):
    L = -np.tril(A, -1)
    U = -np.triu(A, 1)
    v = np.diagonal(A)
    D = np.diag(v)
    DL = D - L
    Hg = np.linalg.inv(DL)
    Tg = Hg @ U
    Cg = Hg @ b

    n = A.shape[0]
    x = np.zeros(n)
    diff = np.zeros(n)
    error = 0.0
    k = 1

    while k <= max_iterations:
        x = Tg @ x_0 + Cg
        diff = x - x_0
        error = scp.linalg.norm(diff, ord=np.inf, axis=None) / \
            scp.linalg.norm(x, ord=np.inf)
        x_0 = x
        k += 1
        if(error < tolerance):
            break
    return x, k


A = np.matrix([
    [10, -1, 2, 0],
    [-1, 11, -1, 3],
    [2, -1, 10, -1],
    [0, 3, -1, 8]
])

b = np.array([6, 25, -11, 15])
x_0 = np.array([0, 0, 0, 0])
solution = gauss_seidel(A, b, x_0, tolerance=0.001)
print('WITH TOLERANCE = 0.001')
print(
    f'Solution = {solution[0]} with {solution[1]} iterations')
solution = gauss_seidel(A, b, x_0)
print('WITH TOLERANCE = 0.0000001')
print(
    f'Solution = {solution[0]} with {solution[1]} iterations')

终端输出:

WITH TOLERANCE = 0.001
Solution = [ 1.00009128 2.00002134 -1.00003115 0.9999881 ] with 6 iterations
WITH TOLERANCE = 0.0000001 Solution = [ 1. 2. -1. 1.] with 10 iterations

内容的提问来源于stack exchange,提问作者Tobal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 04:16:09