I have a question relate to Gauss seidel method

3 ビュー (過去 30 日間)
Kumuthu
Kumuthu 2024 年 9 月 16 日
コメント済み: Kumuthu 2024 年 9 月 17 日
Implement the Gauss-Seidel method for solving a system of linear equations from scratch in MATLAB and walk me through your thought process in constructing the code. Additionally, demonstrate that your implementation works by applying it to the following system. 2x +3y −z =7 , x −2y+4z =1 , 3x +y+2z =8
%Gauss seidel
A=input('Enter a co-efficient matrix A: ');
B=input('Enter source vector B: ');
P=input('Enter initial guess vector: ');
n=input('Enter number of iterations: ');
e=input('Enter tolerance: ');
N=length(B);
X=zeros(N,1);
Y=zeros(N,1); %for stopping criteria
for j=1:n
for i=1:N
x(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
P(i)=X(i);
end
fprintf('Iterations no %d \ n' ,j)
X
if abs (Y-X)<e
break
end
Y=X;
end
This is my code is this correct? please help me
  2 件のコメント
Torsten
Torsten 2024 年 9 月 16 日
編集済み: Torsten 2024 年 9 月 16 日
Adapt the inputs (A,B,P,n,e) to your actual problem so that we can run the code.
You won't get convergence with Gauss-Seidel:
D = diag([2,-2,2]);
U = [0,3,-1;0,0,4;0,0,0];
L = [0,0,0;1,0,0;3,1,0];
M = -inv(L+D)*U;
norm(M,2)
ans = 4.0790
Kumuthu
Kumuthu 2024 年 9 月 17 日
Thank you i'll try

サインインしてコメントする。

回答 (1 件)

John D'Errico
John D'Errico 2024 年 9 月 16 日
編集済み: John D'Errico 2024 年 9 月 16 日
Updating the vector x (small x) and then looking to see if the vector X (capital X) is converging seems like a bad idea. You did this:
x(i)=(B(i)/A(i,i))-(A(i,[1:i-1,i+1:N])*P([1:i-1,i+1:N]))/A(i,i);
But you never use the vector x again. Instead all comparisons are done with X.
Yes, I suppose one day, after running for so many years, your computer may be so old it does not recognize the difference. Maybe then it might do something. Who knows? ;-)
Case matters in MATLAB.
  1 件のコメント
Kumuthu
Kumuthu 2024 年 9 月 17 日
Thank you i'll try

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by