

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


% To solve the n by n linear system % % E1: A(1,1) X(1) + A(1,2) X(2) +...+ A(1,n) X(n) = A(1,n+1) % E2: A(2,1) X(1) + A(2,2) X(2) +...+ A(2,n) X(n) = A(2,n+1) % : %. % EN: A(n,1) X(1) + A(n,2) X(2) +...+ A(n,n) X(n) = A(n,n+1) % % INPUT: number of unknowns and equations n; augmented % matrix A = (A(I,J)) where 1<=I<=n and 1<=J<=n+1. % % OUTPUT: solution x(1), x(2),...,x(n) or a message that the % linear system has no unique solution. syms('AA', 'NAME', 'INP', 'OK', 'N', 'I', 'J', 'A', 'NN', 'M'); syms('ICHG', 'IP', 'JJ', 'C', 'XM', 'K', 'X', 'SUM'); syms('KK', 'FLAG', 'OUP'); TRUE = 1; FALSE = 0; fprintf(1,'This is Gaussian Elimination to solve a linear system.\n'); fprintf(1,'The array will be input from a text file in the order:\n'); fprintf(1,'A(1,1), A(1,2), ..., A(1,N+1), \n'); fprintf(1,'A(2,1), A(2,2), ..., A(2,N+1),\n'); fprintf(1,'..., A(N,1), A(N,2), ..., A(N,N+1)\n\n'); fprintf(1,'Place as many entries as desired on each line, but separate '); fprintf(1,'entries with\n'); fprintf(1,'at least one blank.\n\n\n'); fprintf(1,'Has the input file been created? - enter Y or N.\n'); AA = input(' ','s'); if AA == 'Y' | AA == 'y' fprintf(1,'Input the file name in the form - drive:\name.ext\n'); fprintf(1,'for example: A:\DATA.DTA\n'); NAME = input(' ','s'); INP = fopen(NAME,'rt'); OK = FALSE; while OK == FALSE fprintf(1,'Input the number of equations - an integer.\n'); N = input(' '); if N > 0 A = zeros(N,N+1); X = zeros(1,N); for I = 1:N for J = 1:N+ A(I,J) = fscanf(INP, '%f',1); end; end; OK = TRUE; fclose(INP); else fprintf(1,'The number must be a positive integer.\n'); end; end; else
fprintf(1,'The program will end so the input file can be created.\n'); end; if OK == TRUE % STEP 1 % Elimination Process NN = N-1; M = N+1; ICHG = 0; I = 1; while OK == TRUE & I <= NN % STEP 2 % use IP instead of p IP = I; while abs(A(IP,I)) <= 1.0e-20 & IP <= N IP = IP+1; end; if IP == M OK = FALSE; else % STEP 3 if IP ~= I for JJ = 1:M C = A(I,JJ); A(I,JJ) = A(IP,JJ); A(IP,JJ) = C; end; ICHG = ICHG+1; end; % STEP 4 JJ = I+1; for J = JJ:N % STEP 5 % use XM in place of m(J,I) XM = A(J,I)/A(I,I); % STEP 6 for K = JJ:M A(J,K) = A(J,K) - XM * A(I,K); end; % Multiplier XM could be saved in A(J,I). A(J,I) = 0; end; end; I = I+1; end; if OK == TRUE % STEP 7 if abs(A(N,N)) <= 1.0e- OK = FALSE; else % STEP 8 % start backward substitution X(N) = A(N,M) / A(N,N); % STEP 9 for K = 1:NN