How to convert this 2D code segment with random number into the FORTRAN code?

1 view (last 30 days)
Nx = 4
Ny = 4
c1 = 0.12
rr = 0.0001
for i=1:Nx
for j=1:Ny
D(i,j) =c1 + rr*(0.5-rand);
It contains random number in Matlab code and it is also in 2D.
Robert101 on 19 Aug 2020
I have started with these compilers:
and was learning from different online sources like:
Also i have bought many books about Fortran and trying to understand and compiling the examples there.
With the passage of time different problems seem to occur. As suggested by people i started to work on new compiler for fortran i.e. Code::blocks. To plot different graphs with GNU was kind of hard to understand and implement. The help to integerate it into that compiler from different forums was never sufficient. So i moved to another compiler.
Few days back i bought the comiler: Simplyfortran. It is not very expensive to buy for a student. Also it has some fundamental libraries and plotting facilities.
Then comes another area. How to use scientific libraries which are the bakbone of my coding program? For instance i need fast fourier transformation of my 2D matrix. It is so easy in matlab to just write fft(variable) and get the results. But my compiler does not have it. GNU scientific library is full of resources of subrouritnes like these but i need to buy it for my compiler too. There is an advantage to buy that package that it gives me around 100 packages to add in my compiler. But still they do not claim that it will always work on the compiler.
NAG compiler with Numerical algorithm libraries seems to be the perfect choice to work with Fortran. But then it is very expensive and could not be afforded.
I am working on Mathematical modeling related to 2D partial differential equations. Therefore, the fundamental algorithm is in 2D and could be written in Fortran with some effort. Although playing around with matrixes with saving different files should not be that hard but rather time consuming for the newbie.
I think it is mostly because no one is around working actively on Fortran but on C, C++ or Java, i could not get much from these people.
Also my Boss has never worked on Fortran, and is also not willing to spend money to buy any compiler.
Therefore, the discussion or proper understanding of Fortran is not easy atleast at this stage.
Apparently, the only advantage is that the code is already written in matlab. I need to convert it into fortran language only. But then the functionalities provided by matlab are so comfortable that Fortran seems to be a daunting task. The blessing in disguise is the increase in my understanding of the most famous scientific computing language.
I keep my fingers crossed.

Sign in to comment.

Accepted Answer

A King
A King on 18 Aug 2020
Edited: A King on 18 Aug 2020
James Response in the above is correct. I would only say that it can be further modernized with the new, more specific, Fortran 2003 syntax, which makes it compiler and architecture independent:
use iso_fortran_env, only: IK => int32, RK => real64
integer(IK), parameter :: Nx = 4_IK
integer(IK), parameter :: Ny = 4_IK
real(RK), parameter :: c1 = 0.12_RK
real(RK), parameter :: rr = 0.0001_RK
real(RK) :: D(Nx,Ny)
call random_number(D)
D = c1 + rr * (0.5_RK - D);
write(*,*) D
  1 Comment
Robert101 on 18 Aug 2020
Thanks a lot. You have written in the best way and i have learnt from it some new ways to write it in better ways.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 18 Aug 2020
Edited: James Tursa on 18 Aug 2020
For your particular case, looks like the MATLAB code could reduce to:
Nx = 4;
Ny = 4;
c1 = 0.12;
rr = 0.0001;
D = c1 + rr*(0.5-rand(Nx,Ny));
The Fortran equivalent could be
integer, parameter :: Nx = 4
integer, parameter :: Ny = 4
double precision :: c1 = 0.12d0
double precision :: rr = 0.0001d0
double precision D(Nx,Ny)
call random_number(D)
D = c1 + rr*(0.5d0-D);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by