Output format of a matrix in Fortran 90

In summary: ApprenticeIn summary, Boltzmann'sApprentice had a problem with creating a matrix for a Fourier series in Fortran 90. They initially had a 4x4 matrix but needed a larger one with a step of 0.1 and higher degree. They eventually solved the problem by using an allocatable array.
  • #1
Boltzmann
7
2
Hello,
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. Homework Statement

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 that's where I have the problems. I can't figure out, how to choose the format.

Homework Equations

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
Fortran:
PROGRAM fourier
IMPLICIT NONE
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                                                                                
x=0
t=0
i=1
! OPEN (15,file="Data.dat")                                                                      
DO WHILE ( t<pi )
  DO b=0,2
   a=b
   x=x+1/(2*a+1)*SIN((2.0*a+1)*t)
   r(b+2,i)=x
  ! WRITE(*,*) x,t                                                                               
  ENDDO
  r(1,i)=t
  t=t+step
  i = i+1
  x=0
  !WRITE (*,*) t                                                                                 
ENDDO

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

END PROGRAM fourier
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Boltzmann said:
Hello,
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. Homework Statement

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 that's where I have the problems. I can't figure out, how to choose the format.

Homework Equations

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
Fortran:
PROGRAM fourier
IMPLICIT NONE
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                                                                                 
x=0
t=0
i=1
! OPEN (15,file="Data.dat")                                                                       
DO WHILE ( t<pi )
  DO b=0,2
   a=b
   x=x+1/(2*a+1)*SIN((2.0*a+1)*t)
   r(b+2,i)=x
  ! WRITE(*,*) x,t                                                                                
  ENDDO
  r(1,i)=t
  t=t+step
  i = i+1
  x=0
  !WRITE (*,*) t                                                                                  
ENDDO

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

END PROGRAM fourier
Change the declaration of your r matrix like so:
Code:
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.
 
  • #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.

Thanks.

greets Boltzmann
 

Related to Output format of a matrix in Fortran 90

1. What is the default output format of a matrix in Fortran 90?

The default output format for a matrix in Fortran 90 is called the "free format". This means that the elements of the matrix will be separated by spaces and will be printed in a way that is easy for humans to read.

2. How can I change the output format of a matrix in Fortran 90?

You can change the output format of a matrix in Fortran 90 by using the "format" statement. This statement allows you to specify the exact format in which you want the elements of the matrix to be printed.

3. What are the different types of output formats available for matrices in Fortran 90?

There are two main types of output formats available for matrices in Fortran 90: "free format" and "fixed format". Free format is the default format, while fixed format allows you to specify the exact number of characters for each element of the matrix.

4. How do I specify the output format for a specific element of a matrix in Fortran 90?

To specify the output format for a specific element of a matrix in Fortran 90, you can use the "edit descriptor" in the format statement. These descriptors allow you to control the appearance of the output, such as the number of decimal places or the use of scientific notation.

5. Can I combine different output formats for different elements of a matrix in Fortran 90?

Yes, you can combine different output formats for different elements of a matrix in Fortran 90 by using multiple edit descriptors in the format statement. This allows you to customize the appearance of each element in the matrix according to your specific needs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Calculus and Beyond Homework Help
Replies
3
Views
349
  • Programming and Computer Science
Replies
4
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Programming and Computer Science
Replies
5
Views
4K
Back
Top