Fortran Solving System of Equations with Fortran Code

  • Thread starter Thread starter just physics
  • Start date Start date
  • Tags Tags
    Code Fortran
AI Thread Summary
The discussion revolves around writing a code to solve a system of equations represented in tridiagonal matrix form. The initial steps involve entering coefficients and constants using arrays and printing the equations in a specified format. The user has successfully created a code that computes solutions but is struggling to implement a verification step to check the correctness of these solutions. Specifically, the code should print the equation in the format of the calculated values, such as confirming that if x1=1, x2=6, r1=1, r2=2, and b1=13, then it should display "1*1 + 2*6 = 13". Guidance is provided to modify the existing print statements to incorporate the actual values from the solution array instead of static variable names. This involves adjusting the inner loop to reference the computed solution values directly.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
2K
Replies
6
Views
3K
Replies
16
Views
2K
Replies
22
Views
5K
Replies
8
Views
4K
Replies
6
Views
3K
Replies
2
Views
2K
Back
Top