Fortran: Solving System of Equations with Tridiagonal Matrix

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

This discussion focuses on solving systems of equations using Fortran, specifically with tridiagonal matrices. The user seeks assistance in formatting the output of equations, where coefficients and variables are displayed in a structured manner. The provided code utilizes nested do-loops to print coefficients and corresponding variables, ensuring proper formatting with Fortran's write statement. Sample output demonstrates the expected result, showcasing the correct alignment of coefficients and variables in the equations.

PREREQUISITES
  • Understanding of Fortran programming language
  • Knowledge of matrix representation of systems of equations
  • Familiarity with nested loops in programming
  • Basic formatting techniques in Fortran's write statement
NEXT STEPS
  • Explore advanced Fortran formatting options for output display
  • Learn about solving systems of equations using LU decomposition in Fortran
  • Investigate the use of arrays in Fortran for matrix operations
  • Study the implementation of tridiagonal matrix algorithms in numerical methods
USEFUL FOR

This discussion is beneficial for students learning Fortran, programmers working with numerical methods, and anyone involved in solving systems of equations using matrix representations.

just physics
Messages
4
Reaction score
0
hi I am just start learning fortran and i have aproblem with my homework:
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 , ok i can write it but the problem is how can i print the equations in the form:
r1X1+r2X2=b1
r1X1+r2X2+r3X3=b2
i can't do this step !
 
Technology news on Phys.org
Something like this may do the trick:

Code:
 do i = 1, n
   do j = 1, n
     write(*,"(sp,f5.1,'X',ss,i1)", advance='no') r(i,j), j
   end do
   write(*,"(' = ' , f5.2)") b(i)
 end do

square system, n x n
r coefficients stored in a 2-dimensional matrix and the
right-hand-side b stored in a vector
nested do-loops
inner loop writes one coefficient and variable at a time, without carriage return
inner loop writes left-hand-side of equal sign
'sp' format spec indicates to explicitly show the positive or negative sign of the r coefficient
'ss' format spec turns off the previous 'sp'...we know j is positive, but don't want sign there...we want to see, say, +3.4X2, not +3.4X+2
after left-hand-side has been written, we go out to the outer loop and write ' =' sign and the corresponding b constant...we can now let the write statement advance a line as usual.

...o.k., so I got a little fancy...

depending on the size of your system of equations and the magnitude of your numbers, you may need to adjust the format specs, of course...

Here is a sample output from the code above:

Code:
 +1.0X1 -2.0X2 +3.0X3 =  2.00
 -4.0X1 +5.0X2 +6.0X3 = -3.00
 +7.0X1 -8.0X2 +9.0X3 =  4.00

from there, I hope you get the gist of it and turn it into whatever you need/want
 
thanks a lot u r the best :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 18 ·
Replies
18
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
8K