Gauss Seidel Method matrix form

3 visualizzazioni (ultimi 30 giorni)
Shakil Rahman
Shakil Rahman il 23 Nov 2016
Modificato: Jan il 23 Nov 2016
Trying to change my iteration method to a matrix form that uses the "tril" method instead of what I have previously written. I have 2 files, one is a function file that stores the values.
1: Script
%Creates the function necessary for the assignment containing the variable %tolerance and the matrices.
function [X,Error] = GSSolve(A,b,tol,x_0,debug) X = NaN; %assigns no value to X Error = NaN; %assigns no value to Error hrow = size(b,1); %b rows lcol = size(b,2); %b columns
%if statement that tells the program that when debug is true open a .txt %file called 'GSDebug' and write to it. if debug == true fileID = fopen('GSDebug.txt','w'); fprintf(fileID,'===Gauss-Seidel Method===\r\n'); end
%%Checks the parameters of the matrix where pp1 and pp2 are used to %%evaluate the size of the matrix in question. pp1 = size(A,1); pp2 = size(A,2); %If parameters are not equal to each other then write to the file if pp1 ~= pp2 fprintf(['Matrix A is not a square matrix. It is a %2i x %2i matrix therefore cannot be used in the \r\nGuass-Seidel algorithm' ... 'please use a %2i x %2i matrix instead!'], pp1,pp2,pp1,pp1) fprintf('\nPress any key to terminate') pause return end
%This piece of code checks if matrix b is a column matrix. It needs to be %otherwise the below text will display in the command window if lcol ~= 1 fprintf('Matrix b is not a column matrix. Please reduce the number of columns to 1 i.e. try a 1 x %2i matrix',lcol) fprintf('\nPress any key to terminate') pause return end
%This checks if the column matrix b matches the number of rows in Matrix A. %If it does not match up correctly it will write the following error to the %file. %If pp1 is not equal to hrow then write to the file if pp1 ~= hrow fprintf('Matrix b does not have the same number of rows as Matrix A, therefore have %2i number in b',pp1) fprintf('\nPress any key to terminate') pause return end
%Check if matrix is strictly diagonally dominant. In other words, the %diagonal value of each row should be greater than the sum of the 2 values %on that row for i = 1:pp2 j = 1:pp2; j(i) = []; B = abs(A(i,j)); Check(i) = abs(A(i,i)) -sum(B); if Check(i) < 0
fprintf(['The matrix is not strictly diagonally dominant at row %2i make sure that the absolute value of the %2i of that' ... 'row\nis greater than the sum of the absolute value of the other values in that row.' ],i,i)
end end %Checks if the number of 0 elements exceeds 50 percent and if so, it uses a %sparse version of matrix A if sum(A(:)==0) > 0.5 *numel(A) A = sparse(A); %Produces sparse version of Matrix A end
%%Starts the iteration method required Error = ones(1,1); %Initial iteration is at 0 iteration = 0; %While the error is great than the tolerance and while the iteration is %less than 100 while Error > tol && iteration < 100 iteration = iteration +1; %Adds 1 to the iteration count Z = x_0; %save current values to calculate error later
for i = 1:pp2 j = 1:pp2; %define an array of the coefficents elements j(i) = []; %eliminate the unknown coefficient from the remaining coefficient Xtemp = x_0; %copy the unknown to a new variable Xtemp(i) = []; %Eliminate the unknown under question from the set of values x_0(i) = b(i) -sum(A(i,j) * Xtemp)/A(i,i); end
%Defines X and Error X = x_0; Error = max(sqrt(x_0 - Z).^2);
%This if statement presents an error if the solution does not converge %at the 100th iteration. The conclusion is drawn that it does not converge %at any particular known point if iteration == 100 X = NaN; Error = NaN; fprintf('The solution does not converge') end
%If debug is true then write the iteration number and the error of that %iteration alongside the value of x during each loop of the code if debug == true fprintf(fileID, '\r\nIteration:%d Error:%1.3e\r\n',iteration, Error); fprintf(fileID, '%2.5f ',X); end end
if debug == true %Closes the file at the end of the iteration fclose (fileID); end
2:script that calls the one above
%This file is used as a demo to test the Gauss-Seidel method for the given %matrices. As we can see Matrix A is diagonally dominant and the matrix b %has the same number of rows as matrix A. We have also set the debug file %equal to true in this case as that is what we want. We also call the %function in the main script so that the below values are assigned in the %calculation %clears workspace and command window clear, clc A = [4,1,-1;2,7,1;1,-3,12 ]; %coefficient matrix b = [3;19;31]; %constant vector tol = 0.001; %Specific tolerance value debug = true; %Sets debug to true x_0 = [0;0;0]; [X,Error] = GSSolve(A,b,tol,x_0,debug) %Allows these values to be called into the function in the main script
Any help? thanks
  1 Commento
Jan
Jan il 23 Nov 2016
Modificato: Jan il 23 Nov 2016
Your code is not readable. Please use the "{} Code" button to format it.
What is your question?

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by