本文介绍了Jacobi迭代到高斯-赛德尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Jacobi方法编写了以下函数,需要对其进行修改以执行Gauss-Seidel

I have the following function written for the Jacobi method and need to modify it to perform Gauss-Seidel

function [ x,iter] = jacobi( A,b,tol,maxit )
%jacobi iterations
%

x=zeros(size(b));

[n,m]=size(A);

i=1;

iter=maxit;

for i=1:maxit

    for j=1:n

        y(j)=(b(j)-A(j,1:j-1)*x(1:j-1)-A(j,j+1:n)*x(j+1:n))/A(j,j)

    end

    if max(abs(A*y'-b))<tol
        iter=i;
        break;
    end
    x=y';

end

我知道我需要获取 x(1:j-1)进行更新,但是不确定如何编写,谢谢

I know I need to get x(1:j-1) to update but am unsure of how to write it, thanks

推荐答案

您只需要摆脱y并将任何出现的y替换为x.

You just simply have to get rid of y and replace any occurrence of y with x.

for j=1:n

    x(j)=(b(j)-A(j,1:j-1)*x(1:j-1)-A(j,j+1:n)*x(j+1:n))/A(j,j)

end

if max(abs(A*x-b))<tol
    iter=i;
    break;
end

Jacobi从旧计算出一个新向量,然后立即替换所有变量.

Jacobi computes a new vector from the old and then replaces all variables at once.

Gauß-Seidel进行就地计算并始终使用最新的值.

Gauß-Seidel computes in-place and uses always the most current values.

这篇关于Jacobi迭代到高斯-赛德尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 19:41