Gauss-Seidel Method in MATLAB
431 次查看(过去 30 天)
显示 更早的评论
I have to write two separate codes for the Jacobi method and Gauss-Seidel
The question exactly is: "Write a computer program to perform jacobi iteration for the system of equations given. Use x1=x2=x3=0 as the starting solution. The program should prompt the user to input the convergence criteria value, number of equations and the max number of iterations allowed and should output the solution along with the number of iterations it took for the solution to convergence to the user specified value."
Gauss-Seidel:
%*************************Eric Douglas*****************************%
%***********************August 30th, 2013**************************%
%This code is used to compute the Jacobi Method of a certain matrix.%
%Input:
% C_n = convergence criteria value
% N = number of equations in the matrix
% Imax = the maximum number of iterations
%Output:
% S = the solution( M x 1 matrix ; jacobi approximation)
% j = the number of iterations it took to
% converge to the user inputed value
% R = Residual Values
%establishes the variables needed
%B is an M x 1 matrix
%A is an M x M matrix
%P is the initial M x 1 matrix
%Z = remembering matrix
%Ask the user for each input statement required
Imax = input('What do you want the maximum iteration to be? ');
N = input('How many equations do you want? ');
C_n = input('What number do you want to converge to? ');
%Assigns the values inputed by the user into the matrices
for x=1:1:N
for y=1:1:N
strA = ['What do you desire your numbers in the matrix to be? ' num2str(x) 'Row: ' num2str(y) 'Column: '];
A = input(strA);
end
end
for l=1:1:N
strB = ('What do you desire the Solution matrix to be? ');
B = input(strB);
end
n = length(B);
X = zeros(n,1);
e = ones(n,1);
%%Check if the matrix A is diagonally dominant
for i = 1:n
j = 1:n;
j(i) = [];
C = abs(A(i,j));
Check(i,1) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
Main function for Gauss-Seidel
iteration = 0;
while max(e) > C_n%check error
iteration = iteration + 1;
Z = X; % save current values to calculate error later
for i = 1:N
j = 1:N; % define an array of the coefficients' elements
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients
Xtemp = X; % copy the unknows to a new variable
Xtemp(i) = []; % eliminate the unknown under question from the set of values
X(i,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xs = X;
e = sqrt((X - Z).^2);
end
%%Display Results
Xs
iteration
There is an error at the line ("X(i,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);") and it says "exceeds matrix index". I dont understand how to change this.
Also, if you see anything else wrong then I am open to comments/hints.
2 个评论
Maria Clara Carvalho Rameiro
2018-2-21
移动:DGM
2024-10-29
I get the same error and I don't know how to fix it.
回答(3 个)
Walter Roberson
2013-9-1
The line
A = input(strA);
overwrites all of A each time through the loop, so A is going to end up as a scalar.
0 个评论
Muthu
2024-10-16
编辑:DGM
2024-10-29
% Define parameters for Gauss-Seidel method
maxIter = 100; % Maximum number of iterations
tol = 1e-6; % Tolerance for convergence
n = length(b); % Number of equations
x = zeros(n, 1); % Initial guess for the solution
for iter = 1:maxIter
x_old = x;
for i = 1:n
sum1 = A(i, 1:i-1) * x(1:i-1);
sum2 = A(i, i+1:end) * x_old(i+1:end);
x(i) = (b(i) - sum1 - sum2) / A(i, i);
end
% Check for convergence
if norm(x - x_old, inf) < tol
break;
end
end
% Display the results
disp('Solution for X, Y, Z using Gauss-Seidel method:');
disp(x);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!