0% found this document useful (0 votes)
92 views5 pages

Matlab Coooode

The document contains code for solving systems of linear equations using different numerical methods: 1) Gaussian elimination is used to solve the system defined by the coefficient matrix Info and right hand side vector b. Partial pivoting is performed during Gaussian elimination. 2) LU decomposition is performed on the matrix A to decompose it into lower and upper triangular matrices L and U, which are then used to solve the system defined by A and B. 3) Cholesky decomposition is performed on the symmetric positive definite matrix A to decompose it into a lower triangular matrix L, which is then used to solve the system.

Uploaded by

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

Matlab Coooode

The document contains code for solving systems of linear equations using different numerical methods: 1) Gaussian elimination is used to solve the system defined by the coefficient matrix Info and right hand side vector b. Partial pivoting is performed during Gaussian elimination. 2) LU decomposition is performed on the matrix A to decompose it into lower and upper triangular matrices L and U, which are then used to solve the system defined by A and B. 3) Cholesky decomposition is performed on the symmetric positive definite matrix A to decompose it into a lower triangular matrix L, which is then used to solve the system.

Uploaded by

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

GAUSS:

% Define the coefficient matrix 'Info' and the right-hand side vector 'b'
Info = [2, -1, 1; 3, 2, -1; 1, 1, 3];
b = [8; 5; 10];

% Create the augmented matrix 'A' by concatenating 'Info' and 'b'


A = [Info, b];

% Perform Gaussian elimination with partial pivoting


n = size(A, 1); % Number of rows/columns in 'A'
x = zeros(n, 1);

for i = 1:n
% Partial Pivoting: Find the row with the maximum value in the current column
[~, pivot_row] = max(abs(A(i:n, i)));
pivot_row = pivot_row + i - 1;

% Swap rows to bring the pivot element to the current row


A([i, pivot_row], :) = A([pivot_row, i], :);

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

% Back-substitution
x(n) = A(n, n+1) / A(n, n);

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

% Display the solution


fprintf('Solution is x = \n');
disp(x);

Doolitle:
clc
clear
A = [2 7 1;3 -2 0; 1 5 3];
B = [8 6 -2 ];
[L, U] = MyDecompositionLU_Fun(A);
Y = MyCalculyFun(L, B);
X = MyCalculxFun(U, Y);

function [L, U] = MyDecompositionLU_Fun(A)


[N, ~] = size(A);
L = eye(N); % Initialise L comme une matrice identité
U = zeros(N, N);

for i = 1:N
for j = i:N
U(i, j) = A(i, j);
for k = 1:i-1
U(i, j) = U(i, j) - L(i, k) * U(k, j);
end
end
for j = i+1:N
L(j, i) = A(j, i);
for k = 1:i-1
L(j, i) = L(j, i) - L(j, k) * U(k, i);
end
L(j, i) = L(j, i) / U(i, i);
end
end
end

function Y = MyCalculyFun(L, B)
N = length(L);
Y = zeros(1, N);
for i = 1:N
Y(i) = B(i);
for j = 1:i-1
Y(i) = Y(i) - L(i, j) * Y(j);
end
end
end

function X = MyCalculxFun(U, Y)
N = length(U);
X = zeros(1, N);
for i = N:-1:1
X(i) = Y(i);
for j = i+1:N
X(i) = X(i) - U(i, j) * X(j);
end
X(i) = X(i) / U(i, i);
end
end

CROOT:
clear all
clc
A = [1 3 4 8
2 1 2 3
4 3 5 8
9 2 7 4];

B = [1
1
1
1];

matrixSize=length(A);
Lower=zeros(size(A));
Upper=zeros(size(A));

Lower(:,1)= A(:,1); %Set the first column of L to the frist column of A


Upper(1,:)=A(1,:)/Lower(1,1); % Create the first row of upper, divide by L(1,1)

Upper(1,1)=1; % Start the identity matrix

for k=2:matrixSize
for j=2:matrixSize
for i=j:matrixSize
Lower(i,j)=A(i,j) - dot(Lower(i,1:j-1),Upper(1:j-1,j));
end
Upper(k,j)=(A(k,j)-dot(Lower(k,1:k-1),Upper(1:k-1,j)))/Lower(k,k);
end
end

Upper
Lower

% L * Y = B
Y = zeros(matrixSize, 1);
% BASE CASE, SOLVE THE FIRST ONE
Y(1) = B(1);
for row = 2 : matrixSize %2 - number or rows
Y(row) = B(row);
for col = 1 : row - 1 %1 - row number
Y(row) = Y(row) - Lower(row, col) * Y(col);
end
Y(row) = Y(row) / Lower(row, row)
end
Y

% U * X = Y
X = zeros(matrixSize, 1);

X(matrixSize) = Y(matrixSize) / Upper(matrixSize,matrixSize);

for row = matrixSize - 1 : -1 : 1 %second to last row - 1


temp = 0;
for col = matrixSize : -1 : 1 %number of rows to row
temp = temp + Upper(row,col) * X(col);
end
X(row) = (Y(row) - temp) / Upper(row,row);
end

Rayleigh:

format short
clc
A= [2 -7 0; -1 2 -1; 0 -1 2];
x = [1; 1; 1];
choice1=menu('Pick the choice' , 'Larqest','Smallest','Near to 3');
if choice1==1
B=A;
elseif choice1==2
B=inv(A);
else
lam0 = input('Enter the value of LAMBDA which is near to \n ');
D= A-lam0.*eye(size(A));
B= inv(D) ;
end
iter=1;
maxerr= 2;
err = 10000;
lam1 = Inf;
fprintf('\t Iter \t Eigen Value \t EigenVector \n');
fprintf('\t ======================== \n');
while all (err>maxerr)
xold = x; Y = B*x;
eigval = max(abs (Y));
eigvec = Y./eigval;
x= eigvec;
err = abs (sum(xold-x));
laml= eigval;
disp([iter laml x']);
iter = iter+1;
end
fprintf('Method converge in %d iteration \n',iter-1);
disp('==============')
if choice1==1
fprintf('The greatest eigenvalue is %5.5f\n',laml);
elseif choice1==2
fprintf('The smallest eigenvalue is %5.5f\n',1/lam1);
elseif choice1==3
fprintf('Eigenvalue near to %5.3f is %5.5f\n', lam0,lam0+1/laml);
end
disp('The corresponding eigenvector is:');
fprintf('\t %5.5f \n',x);

cholsky:
A = [4 5 9; -2 6 0; 1/2 6 10];
L = Cholesky(A);
disp(L);
function L = Cholesky(A)
n = length(A);
L = zeros(n, n);

for i = 1:n
L(i, i) = sqrt(A(i, i) - sum(L(i, 1:i-1).^2));
for j = (i + 1):n
L(j, i) = (A(j, i) - sum(L(j, 1:i-1) .* L(i, 1:i-1))) / L(i, i);
end
end
end

Jacobi:
format short
clc
A=[82 6 -1;6 74 -2;1 1 54] ;
b=[35;72;110];
maxerr = 1e-5;
x=zeros(1,size(A,1));
n = size(A,1);
err = inf;
itr=0;
while all(err>maxerr)
xold = x;
for i=1:n
sum = 0;
for j=1:n
if j~=i
sum = sum+A(i,j)*xold(j);
end
end
x(i)=(1/A(i,i))*(b(i)-sum);
end
itr = itr+1;
y(itr,:)=x;
err=abs(xold-x);
end
fprintf('method converge in %d iteration\n',itr);
disp(x)

seidel:
format short
clc
A=[82 6 -1;6 74 -2;1 1 54]
b=[35;72;110];
maxerr= 1e-5;
x=zeros(1,size(A,1));
err1=Inf;
itr=0;
while all(err1>maxerr)
x_old=x;
for i=1:size(A,1)
sum=0;
for j=1:i-1
sum=sum+A(i,j)*x(j);
end
for j=i+1:size(A,1)
sum=sum+A(i,j)*x_old(j);
end
x(i)=(1/A(i,i))*(b(i)-sum);
end
itr=itr+1;
y(itr,:)=x;
err1=abs(x_old-x);
end
fprintf('Method Converge in %d iteration \n',itr);
disp(x)

You might also like