How do i change my code that can have n starting guess variables in a matrix X in Gauss Seidel program?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am working on Gauss Seidel method to solve any set of n linear equations. Please help!!! 1) I tried to change my code that can input multi starting guesses Xn in matrix form but I am stuck in changing the iterative method. How can I change that? 2) I want to stop the loop until the aprroximate percentage error in each of the n variables is less than Es% and no more than the maximum number of iterations. How can I change that? 3) I want to display all the approximate relative % error at the end. How can I add the code to display that? Here is my code:
if true
% code
end
A = input('Enter the coefficient matrix A = ');
B = input('Enter the right hand matrix B = ');
nmax = input('Enter the maximum number of iteration nmax = ');
Es = input('Enter the percentage error = ');
x = input('Enter the starting guess of x in matrix form = ');
n = length(B);
X = zeros(n,1);
iteration = 0;
Error_eval=ones(n,1);
%The iterative method start
while max(Error_eval)>0.001
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) = (B(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,iteration) = X;
Error_eval = sqrt((X - Z).^2);
end
GaussSeidelTable = [1:iteration;Xsolution]'
MaTrIx = [A X B]
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Numerical Integration and Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!