program final_project
implicit none
!
!Solve the code in the general case for n equations
! and allow the user to enter the values of B(i) for AX=B
!
real,dimension(:,:),allocatable::system
real,dimension(:,:),allocatable::m,lower,upper,c,x
real,dimension(:),allocatable::b,y,solution
real::sum
integer::i,j,n,k,p,h
print*," this program is used to solve tridiagonal matrix with 7 unknowns"
print*,"enter the dimension of the matrix == # of equations u want to solve"
read*,n
allocate(system(n,2*n))
allocate (m(n,n))
allocate(lower(n,n))
allocate(upper(n,n))
allocate(c(n,n))
allocate(x(n,n))
allocate (b(n))
allocate(y(n))
allocate(solution(n))
!
! now to print the matrix before the factorization :
do i=1,n
do j=1,2*n
system(i,j)=0
if(j<=n.and.(i>j.or.i<j)) system(i,j)=0
if(j<=n.and.i==j) system(i,j)=1
if (j>n .and. (j==n+i+1 .or. j==n+i-1)) system(i,j)=1
if (j==n+i) system(i,j)= 2
end do
end do
print*,"the matrix befor the factorization is"
do i=1,n
write(*,'(1x,20f6.1)') system(i,:)
end do
print*,"____________________________________________________________"
!
! now we want to enter the elements of matrix (B) where AX=B
print*,"please enter the values of the matrix B :"
do i=1,n
read*,b(i)
end do
!
print*," THE EQUATIONS U WANT TO SOLVE WILL BE :" !inner loop writes one coefficient and variable at a time, without carriage return
do i = 1, n !inner loop writes left-hand-side of equal sign
do j = 1, n !'sp' format spec indicates to explicitly show the positive or negative sign of the r coefficient
write(*,"(sp,f5.1,'X',ss,i1)", advance='no') system(i,j), j !'ss' format spec turns off the previous 'sp'
end do
write(*,"(' = ' , f5.2)") b(i)
end do
!
! now we want to start the factorization :
do p=1,2*n
do i=p+1,n
m(i,p+n)=system(i,p+n)/system(p,p+n)
do j=p,2*n
if(j>n) system(i,j)=system(i,j)-m(i,p+n)*system(p,j)
if(j<=n) system(i,j)=system(i,j)+m(i,p+n)*system(p,j)
end do
end do
end do
print*,"the matrix after the factorization is"
do i=1,n
write(*,'(1x,20f6.1)') system(i,:)
end do
print*,"_________________________________________________"
!
! now want to find the lower matrix of factorization :
do i=1,n
do j=1,n
lower(i,j)=system(i,j)
end do
end do
print*,"The Lower Matrix Of The Factorization Is"
do i=1,n
write(*,'(5x,20f9.4)') lower(i,:)
end do
print*,"_____________________________________________________"
!
! want to find the upper matrix:
do i=1,n
do j=1,n
upper(i,j)=system(i,j+n)
end do
end do
print*," the upper Matrix of Factorization :"
do i=1,n
write(*,'(5x,20f9.4)') upper(i,:)
end do
print*,"________________________________________________________"
!
! now using the lower matrix we will get the matrix y :[[ LY=B ]]
y(1)=b(1)/lower(1,1)
do k=2,n
sum=0
do h=1,k-1
sum=sum+lower(k,h)*y(h) ! forward substitution
end do
y(k)=(b(k)-sum)/lower(k,k)
end do
print*,"__________________________________________________________"
!
! now using the upper matrix we will get the solutions for x : [[UX=Y]]
solution(n)=y(n)/upper(n,n)
do k=n-1,1,-1
sum=0
do h=k+1,n
sum=sum+upper(k,h)*solution(h) ! back substitution
end do
solution(k)=(y(k)-sum)/upper(k,k)
end do
print*," the values of X1,X2,...,Xn , THAT SATISFY THE EQUATIONS WILL BE :"
print*,""
do i=1,n
print*,solution(i)
end do
!
print*," to check the answers :"
! what I am going to do here ?!
end program final_project