Fortran code problem

• Fortran

Main Question or Discussion Point

we have to write a code to solve system of equations by writing them in matricies (tridiagonal matrix) form .....
the first step is to enter the coefficients of the variables (r1,r2,r3,...) and then the answers to the equations (b1,b2,b3,...) by using array , and print the equations in the form:
r1X1+r2X2=b1
r1X1+r2X2+r3X3=b2
i made the code and i get the answers but i have to insert a code to check the answers that i get i.e: if x1=1 ,x2=6 ,r1=1,r2=2,b1=13
then the program print
1*1+2*6=13
i cant make this step can any1 help me please :(

Attachments

• 4.8 KB Views: 340

Related Programming and Computer Science News on Phys.org
Mark44
Mentor
we have to write a code to solve system of equations by writing them in matricies (tridiagonal matrix) form .....
the first step is to enter the coefficients of the variables (r1,r2,r3,...) and then the answers to the equations (b1,b2,b3,...) by using array , and print the equations in the form:
r1X1+r2X2=b1
r1X1+r2X2+r3X3=b2
i made the code and i get the answers but i have to insert a code to check the answers that i get i.e: if x1=1 ,x2=6 ,r1=1,r2=2,b1=13
then the program print
1*1+2*6=13
i cant make this step can any1 help me please :(
Here is the code...
Code:
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"

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
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 im gonna do here ???!!!

end program final_project

You can use the same nested do-loops that I gave you before...the one under your
print*, " THE EQUATIONS U WANT TO SOLVE WILL BE : "
but instead of printing literal 'X' followed by 'j' number, you need to print the value of such Xj (is it in solution(j) ?)
You need to slightly modify just one line, the one in the inner loop....I won't do it for you, this time.