Fortran: Solving System of Equations with Tridiagonal Matrix

  • Fortran
  • Thread starter just physics
  • Start date
  • Tags
    Fortran
In summary, this code has to write the r coefficients for a square system and store them in a 2-dimensional matrix. It then loops through the r coefficients and stores the right-hand-side b in a vector.
  • #1
just physics
4
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
  • #2
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
 
  • #3
thanks a lot u r the best :)
 

Related to Fortran: Solving System of Equations with Tridiagonal Matrix

1. What is Fortran and why is it used for solving systems of equations?

Fortran is a high-level programming language that is commonly used in scientific and engineering applications. It is particularly well-suited for solving mathematical problems, such as systems of equations, due to its efficient and reliable numerical capabilities.

2. What is a tridiagonal matrix and why is it important in solving systems of equations?

A tridiagonal matrix is a special type of matrix where all the elements outside of the main diagonal are zero. In the context of solving systems of equations, using a tridiagonal matrix can significantly simplify the calculations and reduce the computational resources needed.

3. How does Fortran handle tridiagonal matrices in solving systems of equations?

Fortran has built-in functions and routines specifically designed for handling tridiagonal matrices in solving systems of equations. These functions are optimized for efficiency and accuracy, making Fortran a powerful tool for solving complex mathematical problems involving tridiagonal matrices.

4. Can Fortran solve systems of equations with non-tridiagonal matrices?

Yes, Fortran is capable of solving systems of equations with non-tridiagonal matrices as well. However, it may not be as efficient or accurate as using specialized functions for tridiagonal matrices. It is important to choose the appropriate method based on the specific problem at hand.

5. Are there any limitations to using Fortran for solving systems of equations with tridiagonal matrices?

While Fortran is a powerful tool for solving systems of equations with tridiagonal matrices, it may not be the best choice for every problem. It is important to consider other factors such as the complexity of the problem, available computational resources, and familiarity with the language before deciding to use Fortran for solving systems of equations.

Similar threads

  • Programming and Computer Science
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
  • General Math
Replies
7
Views
789
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
18
Views
6K
  • Precalculus Mathematics Homework Help
Replies
4
Views
2K
  • Precalculus Mathematics Homework Help
Replies
6
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top