Solving System of Equations with Fortran Code

  • Context: Fortran 
  • Thread starter Thread starter just physics
  • Start date Start date
  • Tags Tags
    Code Fortran
Click For Summary
SUMMARY

This discussion focuses on solving a system of equations using Fortran code, specifically through the implementation of a tridiagonal matrix method. The user successfully created a program that computes solutions but requires assistance in verifying the results by checking if the computed values satisfy the original equations. The proposed solution involves modifying the print statement to display the actual values of the variables instead of their symbolic representations.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with matrix operations and tridiagonal matrices
  • Knowledge of forward and back substitution methods for solving linear equations
  • Experience with array manipulation in Fortran
NEXT STEPS
  • Implement a verification function in Fortran to check computed solutions against original equations
  • Explore advanced matrix factorization techniques in Fortran
  • Learn about error handling in Fortran for numerical computations
  • Investigate optimization strategies for solving larger systems of equations
USEFUL FOR

Fortran developers, numerical analysts, and students studying linear algebra who are interested in solving systems of equations efficiently.

just physics
Messages
4
Reaction score
0
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 can't make this step can any1 help me please :(
 

Attachments

Technology news on Phys.org
just physics said:
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 can't 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"
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
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K