Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SOURCE CODE:
function []= gauss_elim(A,b)
[m n]=size(A);
[n p]=size(b);
if(m==n)
if(rank([A b])==rank(A))
f=1;
fprintf('The given system of linear equations is consistent');
else
fprintf('The given system of linear equations is inconsistent');
fprintf('Thus no solution');
end
if(f==1)
X = zeros(n,p);
for i=1:n-1
mult=-A(i+1:n,i)/A(i,i);
A(i+1:n,:)= A(i+1:n,:)+ mult*A(i,:); %Here mult is the multiplier
b(i+1:n,:)= b(i+1:n,:)+ mult*b(i,:);
end
for i = n-1:-1:1
X(i,:) = (b(i,:) - A(i,i+1:n)*X(i+1:n,:))/A(i,i);
end
else
fprintf('Gauss Elimination cannot be done');
end
OUTPUT :
>> A = [3 2 1 -4; 1 -5 2 1; 5 1 -3 2; 2 3 1 5]
A=
3 2 1 -4
1 -5 2 1
5 1 -3 2
2 3 1 5
>> b = [5; 18; -4; 11]
b=
5
18
-4
11
>> gauss_elim(A,b)
The given system of linear equations is consistentthe solution of the given system of
equations is:
2.0000
-1.0000
5.0000
1.0000
the number of iterations in which gauss elimination can be performed are: 10
GAUSS SEIDEL
SOURCE CODE:
clear;clc
format compact
%% Read or Input any square Matrix
A = [-6 2 1 2 1;
3 8 -4 1 0;
-1 1 4 10 1;
3 -4 1 9 2;
2 0 1 3 10];% coefficients matrix
C = [3;4;-2 ;12;1];% constants vector
n = length(C);
X = zeros(n,1);
Error_eval = ones(n,1);
iteration = 0;
while max(Error_eval) > 0.001
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) = (C(i) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xsolution(:,iteration) = X;
Error_eval = sqrt((X - Z).^2);
end
%% Display Results
GaussSeidelTable = [1:iteration;Xsolution]'
MaTrIx = [A X C]
OUTPUT :
GaussSeidelTable =
MaTrIx =
[m n]=size(A);
[n p]=size(b);
X=zeros(n,p);
Y=zeros(n,p);
L=eye(n);
if(m==n)
if(rank([A b])==rank(A))
f=1;
fprintf('The given system of linear equations is consistent \n');
else
fprintf('The given system of linear equations is inconsistent \n');
fprintf('Thus no solution');
end
if f==1
for i=1:n-1
mult=-A(i+1:n,i)/A(i,i);
L(i+1:n,i)=-mult;
A(i+1:n,:)=A(i+1:n,:)+ mult*A(i,:);
end
U=A;
fprintf('the unit lower triangulat matrix "L" is:\n');
disp(L);
fprintf('the upper triangulat matrix "U" is:\n');
disp(U);
%LUX=b , UX=Y ,LY=b
%to find Y from LY=b by forward substitution
Y(1,:)=b(1,:);
for i=2:n
Y(i,:)=b(i,:)-L(i,1:i-1)*Y(1:i-1,:);
end
%to find the the solution X of the given system of equations by backword
substitution
X(n,:)=Y(n,:)/U(n,n);
for i=n-1:-1:1
X(i,:)=(Y(i,:)-U(i,i+1:n)*X(i+1:n,:))/U(i,i);
end
fprintf('Hence the solution of the given system of equations is:\n');
disp(X);
end
end
itr=(n*(n+1)/2)+n;
fprintf('the number of iterations in which gauss elimination can be
performed are: %d\n',itr);
end
OUTPUT:
>> A = [3 2 1 -4; 1 -5 2 1; 5 1 -3 2; 2 3 1 5]
A=
3 2 1 -4
1 -5 2 1
5 1 -3 2
2 3 1 5
b=
5
18
-4
11
>> LU(A,b)
The given system of linear equations is consistent
the unit lower triangulat matrix "L" is:
Columns 1 through 3
1.0000 0 0
0.3333 1.0000 0
1.6667 0.4118 1.0000
0.6667 -0.2941 -0.1538
Column 4
0
0
0
1.0000
Column 4
-4.0000
2.3333
7.7059
9.5385
SOURCE CODE:
function []=powereigen(Y0,A,tolr)
% Power Method
% To find largest and smallest eigen values of given square matrix.
% Inputs -
% Y0 n * 1 matrix (Initial Guess)
% A n * n matrix (Given Matrix)
% tolr (Tolerance)
%**************** Largest Eigen-Value *****************%
dd = 1;
x = Y0;
n = 10;
while dd > tolr
y = A * x;
dd = abs(norm(x) - n);
n = norm(x);
x = y ./ n;
end
lvalue = n
%*************** Smallest Eigen-Value *****************%
dd = 1;
x = Y0;
n = 10;
while dd>tolr
y = A \ x;
dd = abs(norm(x) - n);
n = norm(x);
x = y ./ n;
end
if n == 0
sprintf('Smallest eigen value not exist.');
else
svalue = 1 / n
end
OUTPUT:
>> A = [5 -2 0; 1 2 -3; 1 -2 4]
A=
5 -2 0
1 2 -3
1 -2 4
>> x = [1; 1; 1]
x=
1
1
1
tolr =
1.0000e-03
>> powereigen(x,A,tolr)
lvalue =
5.9999
svalue =
1.0001