GAUSS SEİDAL METHOD İNPUT

3 Ansichten (letzte 30 Tage)
Ömer Faruk PARLAK
Ömer Faruk PARLAK am 28 Mai 2022
Beantwortet: Ravi am 21 Dez. 2023
function gauss_seidel_iterasyonu
clc;clear all;warning off;
A=[5 1 2;2 -4 1;1 -1 -3];b=[15;11;-6];
% A=[4 1 0 0 0 0 0; 1 4 1 0 0 0 0;0 1 4 1 0 0 0; 0 0 1 4 1 0 0; 0 0 0 1 4 1 0; 0 0 0 0 1 4 1 ; 0 0 0 0 0 1 4];
% b=[ 5;6;6;6;6;6;5];
[n m]=size(A);
L=tril(A,-1);U=triu(A,1);D=diag(diag(A)) ;
x0=zeros(n,1);x1=x0;%başlangıç iterasyonu
Nit=1000; % maksimum iterasyon
%%%%1.yol%%%%%
tic
for k=1:Nit
x1=inv(D)*(b-L*x1-U*x0);
if norm(x1-x0)<eps
break
else
x0=x1;
if k==Nit
disp('Yakınsak değildir')
end
end
end
toc
fprintf('%5s %3s\n','Çözüm','-x-');
fprintf('%12.8f\n',x1)
fprintf('İterasyon %6.0f\n', k)
x0=zeros(n,1);x2=x0;%başlangıç iterasyonu
%%%%2.yol%%%%%
tic
for k=1:Nit
for i=1:n
x2(i)=(b(i)-A(i,1:i-1)*x2(1:i-1)-A(i,i+1:n)*x0(i+1:n))/A(i,i);
end
if norm(x2-x0)<eps
break
else
x0=x2;
if k==Nit
disp('Yakınsak değildir')
end
end
end
toc
fprintf('%5s %3s\n','Çözüm','-x-');
fprintf('%12.8f\n',x2)
fprintf('İterasyon %6.0f\n', k)
end
HOW CAN I INPUT THE INPUT OF A- B AND ITERATION COMMANDS IN THE GAUSS SEİDAL METHOD GIVEN IN THE CODE, CAN YOU HELP? I would be very pleased if you could send it as a direct code.
  1 Kommentar
Torsten
Torsten am 28 Mai 2022
A, b and Nit are prescribed at the beginning of the code. You can change them there. Or you can call the function with input arguments A, b and Nit and comment out the lines where they are set.
I recommend also to make "eps" a variable to be prescribed.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ravi
Ravi am 21 Dez. 2023
Hi Ömer Faruk Parlak,
I assume you are facing an issue in providing matrix input to the Gauss Siedel method dynamically instead of hardcoding the matrices.
Observe the following cases, low dimension matrices and large matrices.
In case of low-dimension matrices, the user can read the entries of the matrices interactively by prompting on the screen.
rows = input('Enter the number of rows: ');
cols = input('Enter the number of columns: ');
% Initialize the matrix with zeros
matrix = zeros(rows, cols);
% Prompt the user to enter the matrix elements row by row
for i = 1:rows
for j = 1:cols
prompt = sprintf('Enter the element at (%d, %d): ', i, j);
matrix(i, j) = input(prompt);
end
end
In case of large matrices, it would be difficult to read the matrix by prompting to the screen. In such cases, we can load the matrix from a text file, csv file, or a mat file.
Let us assume the matrix is present in a file named “data.txt”, then we can read the matrix as follows.
matrix = load('data.txt');
If the matrix is present in a .mat file, then we can input the matrix as follows.
% Assume 'data.mat' is a MATLAB file containing a variable 'A' which is the matrix
load('data.mat', 'matrix');
After the matrix input, number of iterations can be taken as input using the “input” function.
To learn more about the above-mentioned functions, please refer to the following links.
Hope this explanation clears the issue you are facing.
Thanks,
Ravi Chandra

Kategorien

Mehr zu Statistics and Machine Learning Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by