0% found this document useful (0 votes)
47 views3 pages

Main Linear Direct 2.m

This document contains code for solving linear systems using direct methods in MATLAB. It uses Gaussian elimination and Gauss-Jordan elimination to solve systems. It prompts the user to input the number of equations, coefficient matrix elements, and constant values. It then tests for diagonal dominance, performs Gaussian elimination and back substitution to solve the system, and performs Gauss-Jordan elimination to solve the system.

Uploaded by

Shubham Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Main Linear Direct 2.m

This document contains code for solving linear systems using direct methods in MATLAB. It uses Gaussian elimination and Gauss-Jordan elimination to solve systems. It prompts the user to input the number of equations, coefficient matrix elements, and constant values. It then tests for diagonal dominance, performs Gaussian elimination and back substitution to solve the system, and performs Gauss-Jordan elimination to solve the system.

Uploaded by

Shubham Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

21/1/19 12:10 PM C:\Users\admi...\main_linear_direct_2.

m 1 of 3

%Direct Method for Linear System - 2


clear all;
close all;
clc;
n = input("number of equations:");
for i = 1:n
for j = 1:n
A(i,j) = input("elements of coefficient matrix:");
end;
end;
A
A_mod = A;
for i = 1:n
B(i,1)=input("constant values:");
end;
B
C = [A,B]
C_mod = C;
%Test For Diagonal Dominance
disp('TEST FOR DIAGONAL DOMINANCE')
fprintf ('\n');
S = 0;
count = 0;
for i = 1:n
for j = 1:n
if (i~=j)
S = S+A(i,j);
end;
if ((j == n)&&(abs(A(i,i))>=abs(S)))
disp('Diagonally Dominant Row')
count = count + 1;
S = 0;
elseif ((j == n)&&(abs(A(i,i))<abs(S)))
disp('Not Diagonally Dominant Row')
S = 0;
end;
end;
end;
if ((i == n)&&(count == n))
disp('Diagonally Dominant Matrix')
elseif ((i == n)&&(count ~= n))
disp('Enter Diagonally Dominant Matrix')
elseif ((i ~= n)&&(count == n))
disp('Enter Diagonally Dominant Matrix')
end;
pause();
%Gauss Elimination
if (count == n)
fprintf ('\n\n');
disp('GAUSS ELIMINATION')
k = n+1;
counter = 1;
j = 1;
while (j<n)
for i = (j+1):n
21/1/19 12:10 PM C:\Users\admi...\main_linear_direct_2.m 2 of 3

C_mod(i,:) = C_mod(i,:)-(C_mod((i-counter),:)*(C_mod(i,(i-counter))/C_mod
((i-counter),(i-counter))));
counter = counter + 1;
end;
j = j+1;
counter = 1;
end;
C_mod
for i = 1:n
for j = 1:n
A_mod(i,j) = C_mod(i,j);
end;
end;
A_mod
Condition_Number = cond(A_mod)
S_b = 0;
x(n,1) = C_mod(n,k)/C_mod(n,n);
for i = (n-1):-1:1 %Backward Substitution
for j = n:-1:(i+1)
S_b = S_b + (C_mod(i,j)*x(j,1));
end;
x(i,1) = (C_mod(i,k)-S_b)/C_mod(i,i);
S_b = 0;
end;
x
A_mod = A;
pause();
%Gauss Jordan
fprintf ('\n\n');
disp('GAUSS JORDAN')
for i = 1:n
C_mod(i,:)= C_mod(i,:)/C_mod(i,i);
end;
C_mod
for i = 1:n
for j = 1:n
A_mod(i,j) = C_mod(i,j);
end;
end;
A_mod
Condition_Number = cond(A_mod)
k = n+1;
counter = 1;
j = n;
while (j>1)
for i = (j-1):-1:1
C_mod(i,:) = C_mod(i,:)-(C_mod((i+counter),:)*(C_mod(i,(i+counter))/C_mod
((i+counter),(i+counter))));
counter = counter + 1;
end;
j = j-1;
counter = 1;
end;
C_mod
for i = 1:n
21/1/19 12:10 PM C:\Users\admi...\main_linear_direct_2.m 3 of 3

for j = 1:n
A_mod(i,j) = C_mod(i,j);
end;
end;
A_mod
Condition_Number = cond(A_mod)
X(n,1) = C_mod(n,k)/C_mod(n,n);
for i = 1:n
X(i,1) = C_mod(i,k);
end;
X
A_mod = A;
end;

You might also like