问题描述
我需要在Matlab中实现Jacobi和Guass Seidel方法.
I need to implement the Jacobi and Guass Seidel, methods in Matlab.
我发现了此链接,其中包含可产生正确结果的代码(在我尝试过的一个样本上).
I found this link which has code that produces correct results (on the one sample I tried) for each.
请参阅帖子3.
My problem is that the implementation is different to that described here, and here
我很高兴使用别人的代码(实际上,我更喜欢经过尝试和测试的东西),但是我想确切地了解它的工作原理.
I'm happy enough using someone else's code (indeed I prefer something tried and tested) but I want to understand exactly how it works.
有人可以指出这篇文章中使用的实现吗?
Can someone point me to a description of the implementations used in the post?
或者这些算法的其他实现可能在Matlab中被视为基准?
Alternately are there other implementations of these algortihms that might be considered benchmarks in Matlab?
推荐答案
我想向您展示Seidel上的代码如何工作,希望您可以自己对Jacobi进行相同的分析.
I would like to show you how the code on Seidel works, hopefully you can do the same analysis on Jacobi yourself.
Q=tril(A); % Q == L
r=b-A*x;
dx=Q\r;
这部分在数学上表示x(:,k+1) = inv(L) * (b - A*x(:,k)) = inv(L) * (b - L*x(:,k) - U*x(:,k));
在您提供的Wikipedia页面中,它需要inv(L) * (b - U*x(:,k));
while in the wikipedia page you provides, it requires inv(L) * (b - U*x(:,k));
,但它们与inv(L) * (b - L*x(:,k) - U*x(:,k)) = inv(L) * (b - U*x(:,k)) - x(:,k);
起等效,因此,如果您遵循Wikipedia中的公式,则迭代更新应为:x(:,k+1)=(dx + x(:,k));
,而与您提供的代码相同:x(:,k+1) = x(:,k) + lambda * dx;
but they are equivalent since inv(L) * (b - L*x(:,k) - U*x(:,k)) = inv(L) * (b - U*x(:,k)) - x(:,k);
so if you follow the formula in wikipedia, the iteration update should be: x(:,k+1)=(dx + x(:,k));
while it is the same in the code you provided :x(:,k+1) = x(:,k) + lambda * dx;
请注意,λ是一个松弛系数,主要取决于收敛速度的判断.您可以在代码中将1
设置为1
,这使其与Wikipeida中的公式完全相同.
Please note that lambda is a relaxation coefficient, mainly functions on the convergence speed adjuestment. You can set to 1
in the code, which makes it exactly the same as the formula in wikipeida.
这篇关于Matlab中的Jacobi/Gauss Seidel方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!