0% found this document useful (0 votes)
85 views10 pages

Mathematics

The document describes the power method algorithm for finding the largest and smallest eigenvalues of a square matrix. It takes as input an initial vector, the matrix, and a tolerance value. It iteratively computes the matrix-vector product to converge on the eigenvector corresponding to the largest eigenvalue, and computes the inverse matrix-vector product to converge on the eigenvector for the smallest eigenvalue. It outputs the calculated largest and smallest eigenvalues.

Uploaded by

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

Mathematics

The document describes the power method algorithm for finding the largest and smallest eigenvalues of a square matrix. It takes as input an initial vector, the matrix, and a tolerance value. It iteratively computes the matrix-vector product to converge on the eigenvector corresponding to the largest eigenvalue, and computes the inverse matrix-vector product to converge on the eigenvector for the smallest eigenvalue. It outputs the calculated largest and smallest eigenvalues.

Uploaded by

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

GAUSS ELIMINATION

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

% Back Substitution to find the unknowns


X(n,:) = b(n,:)/A(n,n);

for i = n-1:-1:1
X(i,:) = (b(i,:) - A(i,i+1:n)*X(i+1:n,:))/A(i,i);
end

fprintf('the solution of the given system of equations is:\n');


disp(X);
end
itr=n*(n+1)/2;
fprintf('the number of iterations in which gauss elimination can be
performed are: %d\n',itr);

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);

%% Check if the matrix A is diagonally dominant


for i = 1:n
j = 1:n;
j(i) = [];
B = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than
the remaining row values combined?
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row
%2i\n\n',i)
end
end

%% Start the Iterative method

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 :

The matrix is not strictly diagonally dominant at row 3

The matrix is not strictly diagonally dominant at row 4

GaussSeidelTable =

1.0000 -0.5000 0.6875 -0.7969 1.8941 -0.2885

2.0000 0.1796 -0.2026 -5.0676 1.8106 0.0276

3.0000 -0.8040 -1.9586 -4.7448 1.2519 0.3597

4.0000 -1.4664 -1.4790 -3.7165 1.4978 0.3156

5.0000 -1.0605 -1.1478 -4.3017 1.5846 0.2669

6.0000 -1.0269 -1.4638 -4.4189 1.4567 0.3102

7.0000 -1.1871 -1.4463 -4.1545 1.4789 0.3092

8.0000 -1.1300 -1.3384 -4.2225 1.5156 0.2936

9.0000 -1.0957 -1.3898 -4.2890 1.4922 0.3004

10.0000 -1.1306 -1.4070 -4.2365 1.4888 0.3031

11.0000 -1.1283 -1.3813 -4.2346 1.4987 0.2995

12.0000 -1.1167 -1.3859 -4.2543 1.4958 0.3000

13.0000 -1.1224 -1.3932 -4.2467 1.4934 0.3011

14.0000 -1.1242 -1.3885 -4.2428 1.4955 0.3005

15.0000 -1.1214 -1.3878 -4.2472 1.4955 0.3004

16.0000 -1.1219 -1.3898 -4.2468 1.4947 0.3006

17.0000 -1.1227 -1.3892 -4.2454 1.4950 0.3006

18.0000 -1.1222 -1.3887 -4.2461 1.4952 0.3005

MaTrIx =

-6.0000 2.0000 1.0000 2.0000 1.0000 -1.1222 3.0000

3.0000 8.0000 -4.0000 1.0000 0 -1.3887 4.0000

-1.0000 1.0000 4.0000 10.0000 1.0000 -4.2461 -2.0000

3.0000 -4.0000 1.0000 9.0000 2.0000 1.4952 12.0000

2.0000 0 1.0000 3.0000 10.0000 0.3005 1.0000


LU DECOMPOSITION
SOURCE CODE:
function []=ludec(A,b)

[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]

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

the upper triangulat matrix "U" is:


Columns 1 through 3

3.0000 2.0000 1.0000


0 -5.6667 1.6667
0 0 -5.3529
0 0 0

Column 4

-4.0000
2.3333
7.7059
9.5385

Hence the 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: 14


GRAM SCHMIDT
SOURCE CODE:
% the Gram–Schmidt process is a method for orthonormalising
% a set of vectors in an inner product space
%input:
%A: input matrix
%m,n: size of matrix A
%output:
%R: matrix obtained by GramSchmidt Method
clear all,close all,clc;
%input:
A=input('Enter matrix you want to apply Gram Schmidt: ');
[m,n]=size(A);
R=zeros(m,n);
Q=A;
%solution
for j=1:n
R(:,j)=Q(:,j);
if(j>1)
for i=1:j-1
R(:,j)=R(:,j)-sum(Q(:,j).*R(:,i))*R(:,i)/norm(R(:,i))^2;%Applying Formula
end
end
end
%output
disp('Matrix obtained by Gram Schmidt method is :');
disp(' ');
disp(R);
OUTPUT 4:

Enter matrix you want to apply Gram Schmidt: [1 2 3 ; -1 0 -3; 0 -2 3]


Matrix obtained by Gram Schmidt method is :

1.0000 1.0000 1.0000


-1.0000 1.0000 1.0000
0 -2.0000 1.0000
POWER METHOD

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 = 0.001

tolr =

1.0000e-03

>> powereigen(x,A,tolr)
lvalue =

5.9999

svalue =

1.0001

You might also like