Problem 1:: MATLAB Code

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Problem 1:

MATLAB Code:
A=[3 -4 5 2;12 9 5 -3;4 1 1 2;3 3 9 10]; % Given Matrix A
n=length(A);
U=A;

% Order of Matrix A
% nXn matrix to save L and U

for i=1:n-1
for j=i+1:n
c=U(j,i)/U(i,i);
% Modification of j-th row using i-th row
U(j,i:end)=U(j,i:end)-c*U(i,i:end);
U(j,i)=c;
end
end

Output:

Problem 2:
MATLAB Code:
A=pascal(5);
n=length(A);
L=zeros(n,n);

%%% Given matrix A


%%% Order of A
%%% L matrix of demension nXn

L(1,1)=sqrt(A(1,1));

%%%% 1st element of L

for i=2:n
for j=1:i
if j==1
L(i,j)=A(i,j)/L(j,j);
elseif i==j
s=sum(L(i,1:i-1).^2);
L(i,j)=sqrt(A(i,i)-s);

%%% 1st column elements

%%% Diagonal elements

else
s=sum(L(i,1:j-1).*L(j,1:j-1));
L(i,j)=(A(i,j)-s)/L(j,j);
end
end
end

Output:

%%% Other elements

Problem 3:
MATLAB function:
function x=forward_backward_substitution(L,U,b)
n=length(b);
d=zeros(n,1);
x=d;

%%%% Total no. of Equations


%%%% Intermediate d vector

d(1)=b(1)/L(1,1);
for i=2:n
s=sum(L(i,1:i-1).*d(1:i-1)');
d(i)=(b(i)-s)/L(i,i);
end

%%%% Forward Substitution

x(end)=d(end)/U(n,n);
for i=n-1:-1:1
%%%% Backward Substitution
s=sum(U(i,i+1:end).*x(i+1:end)');
x(i)=(d(i)-s)/U(i,i);
end
end

Output:

Problem 4:
Plot:

Problem 5:
MATLAB Code:
A=[1
1
9
0
0

2
5
2
2
0

1
1
3
6
3

0
7
4
7
1

0;
0;
2;
2;
2];

n=length(A);
U=A;
k=2;

%%%% Given matrix A

%%%% Order of A
%%%% Bandwidth

for i=1:n-1
if i<n-1
for j=i+1:i+k
%%%% Calculation upto the i+k-th column
c=U(j,i)/U(i,i);
if i+k>n
%%%% i+k must be less than or equal to n
h=n;
else
h=i+k;
end
U(j,i:h)=U(j,i:h)-c*U(i,i:h);
U(j,i)=c;
end
else
j=i+1;
%%%% last column operation
c=U(j,i)/U(i,i);
U(j,i:end)=U(j,i:end)-c*U(i,i:end);
U(j,i)=c;
end
end

Output:

You might also like