1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Output format of a matrix in Fortran 90

  1. Mar 21, 2015 #1
    I'm new here and I'm also new in programming. I never did it before and now I have a problem with one of the programs in fortran 90 and I can't figure out how to solve it. Maybe some of you can help me. Many thanks in advance.

    1. The problem statement, all variables and given/known data

    I need to plot the results of a fourier series. In the first column the time and in the followings the results and the changes if the degree changes. Therefore I need to create a matrix. And thats where I have the problems. I can't figure out, how to choose the format.

    2. Relevant equations

    3. The attempt at a solution

    Here is how I tried it. It works if I choose a 4x4 matrix and step=1, so that I only get 4 results of each but I want to use a step like 0.1 and a higher degree of the series therefore I need at least a 32x4 matrix. When I change the 4x4 matrix to anything else, the output starts getting messed up.
    Here is my program

    Code (Fortran):

    PROGRAM fourier
    INTEGER :: b, i
    REAL :: t, x, step=1,a
    REAL, PARAMETER :: pi= 4.D0*DATAN(1.D0)
    REAL :: r(4,4)=0.0
    !READ (*,*) step                                                                                
    ! OPEN (15,file="Data.dat")                                                                      
    DO WHILE ( t<pi )
      DO b=0,2
      ! WRITE(*,*) x,t                                                                              
      i = i+1
      !WRITE (*,*) t                                                                                

    !WRITE (*,*) t                                                                                  
    WRITE(*,'(4(F9.6,X))') r

    END PROGRAM fourier
    Last edited by a moderator: Mar 21, 2015
  2. jcsd
  3. Mar 21, 2015 #2


    Staff: Mentor

    Change the declaration of your r matrix like so:
    Code (Text):
    REAL :: r(32,4)
    Since you will be modifying the matrix elements, you don't need to initialize them, as far as I can tell.

    You will also need to modify your DO loop to take into account the larger size of your matrix, with b ranging between 0 and 30 instead of 0 and 2.

    I would also change your last write statement by putting it inside a do loop, printing one row of your matrix at a time, or even a nested pair of do loops to print each element of the matrix. The newer versions of Fortran are able to spit out entire arrays with just a single write statement, but I prefer the control of using loops to print out one element at a time.
  4. Mar 22, 2015 #3
    Hey Mark44,

    thank you very much for your answer!
    I already solved the problem by using an allocatable array.

    The thread can be closed.


    greets Boltzmann
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted