Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Lab 4

Uploaded by

2023uma0234
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)
4 views3 pages

Lab 4

Uploaded by

2023uma0234
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

Lab Session 4

1. Write a code to decompose a matrix into L and U as outlined in the ppt and print the
resulting L and U.
c ***** initialise all el and u to 0 except for diagonal of u to 1 *******
do i=1,n
do j=1,n
el(i,j)=0.
if (i.eq.j) then
u(i,j)=1
else
u(i,j)=0.
endif
enddo
enddo

c ***** Decomposition operation begins *******


Do i = 1,n
el(i,1)=a(i,1)
enddo
do j = 2,n
u(1,j)=a(1,j)/el(1,1)
enddo

C*** El Operation
do j = 2,n-1
do i = j,n
el(i,j)=a(i,j)
do k=1,j-1
el(i,j)=el(i,j)-el(i,k)*u(k,j)
enddo
enddo

C*** U Operation

do i = j+1,n
u(j,i)=a(j,i)
do k=1, j-1
u(j,i)=u(j,i)-el(j,k)*u(k,i)
enddo
u(j,i)=u(j,i)/el(j,j)
enddo
enddo

C *** el(n,n) Operation

el(n,n)= a(n,n)
do k = 1,n-1
el(n,n)=el(n,n)-el(n,k)*u(k,n)
enddo
c ***** This completes Decomposition Operations
C ***** Write el and u

write(16,*)'el- matrix'
do i = 1,n
write(16,*)(el(i,j),j=1,N)
Enddo

write(16,*)'u- matrix'
do i = 1,n
write(16,*)(u(i,j),j=1,N)
Enddo

C ****** computation of y vector begins (forward substitution) ***


y(1)=b(1)/el(1,1)
do 50 i = 2,n
sum=0.
do 55 J = 1, i-1
55 sum=sum+el(i,j)*y(j)
50 y(i)=(b(i)-sum)/el(i,i)

c ****** computation of x vector (back substitution)


x(n)=y(n)
do 60 I= n-1,1,-1
sum=0.
do 70 J=i+1,n
sum=sum+u(i,j)*x(j)
Enddo
x(i)=(y(i)-sum)
Enddo
write(16,*)'solution vector'
write(16,*)(x(i),i=1,N)
Return
end
c *****************************************************************
Use the following data as a check;

3 −1 2 𝑥1 12
𝑥2 11
[1 2 3 ] {𝑥 } = { }
2 −2 −1 3 2

The correct solution is {3,1,2}𝑇


The L and U Matrices are shown below:
30 0
7 1 2
3 −1 2 1 −
1 0 3 3]
[1 2 3 ]= 3 [
4 0 1 1
2 −2 −1
[ 2 − −1] 0 0 1
3
2. Write the code for solving Tri-diagonal Matrix using Thomas algorithm as discussed in
the ppt and solve the following equation

1 0 0 0 𝑥1 0
1 −2 1 0 𝑥2 0
[ ]{ } = { }
0 1 −2 1 𝑥3 0
0 0 0 1 𝑥4 1

1 2 𝑇
The correct solution is {0, 3 , 3 , 1}

Step-1 Read the matrix correctly


c *** n = number of unknowns
c a(i,1) = sub diagonal
c a(i,2) = diagonal
c a(i,3) = super diagonal
c r(i) = right hand side
c x(i) = solution
read(12,*)n
do i=1,n
read(12,*)(a(i,j),j=1,3),r(i)
enddo
call thomas(n,a,r,x)
write(13,*)(x(i),i=1,n)
stop
end

Subroutine Thomas (N,AA,RHS, Sol)


C Step- Perform Upper Triangulation
AA(1,3)=AA(1,3)/AA(1,2)
RHS(1)=RHS(1)/AA(1,2)
Do I=2,N
DEL=AA(I,2)-AA(I,1)*AA(I-1,3)
IF(I.NE.N) Then
AA(I,3)=AA(I,3)/DEL
Else
RHS(I)=(RHS(I)-AA(I,1)*RHS(I-1))/DEL
Endif
Enddo

C Step-Back substitution

SOL(N)=RHS(N)
DO 150 I=N-1,1,-1
150 SOL(I)=RHS(I)-AA(I,3)*SOL(I+1)
RETURN
END

You might also like