Solving System of Equations with Fortran Code

  • Fortran
  • Thread starter just physics
  • Start date
  • Tags
    Code Fortran
In summary: after the factorization is"
  • #1
just physics
4
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

  • fortran hw.txt
    4.8 KB · Views: 498
Technology news on Phys.org
  • #2
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
 
  • #3
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.
 

Related to Solving System of Equations with Fortran Code

1. What is a system of equations and how is it solved with Fortran code?

A system of equations is a set of two or more equations with multiple variables that must be solved simultaneously. Fortran code uses mathematical algorithms to solve these equations, typically through methods such as Gaussian elimination or the LU decomposition.

2. Why is Fortran a popular language for solving system of equations?

Fortran's main strength is its efficiency in performing complex mathematical computations. It is specifically designed for scientific and engineering applications, making it an ideal choice for solving systems of equations, which often involve large amounts of data and calculations.

3. Can Fortran code handle different types of equations?

Yes, Fortran can handle a wide range of equations, including linear, nonlinear, and differential equations. It also has built-in functions for solving systems of equations with complex numbers, making it suitable for a variety of scientific and engineering problems.

4. Are there any limitations to using Fortran for solving systems of equations?

While Fortran is a powerful language for mathematical computations, it may not be the best choice for solving systems of equations with symbolic variables. In these cases, other languages such as Python or Mathematica may be more suitable.

5. How can I learn to write Fortran code for solving systems of equations?

There are many resources available for learning Fortran, including online tutorials, textbooks, and courses. It is important to have a solid understanding of mathematical concepts and algorithms before attempting to write Fortran code for solving systems of equations. Practice and experimentation are also key to improving your skills.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
799
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
960
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
1
Views
725
Back
Top