How to convert this 2D code segment with random number into the FORTRAN code?
3 views (last 30 days)
Show older comments
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);
end
end
It contains random number in Matlab code and it is also in 2D.
8 Comments
dpb
on 18 Aug 2020
Edited: dpb
on 18 Aug 2020
Which Fortran compiler are you using on what platform and for what kinds of tasks?
Fotran and MATLAB have much in common in terms of basic syntax; in fact the earliest MATLAB was actually written in FORTRAN.
If you want/need support on the Fortran side, the usenet comp.lang.fortran group is still active and for the most part pretty lenient on newbies.
The biggest drawback of Fortran vis a vis MATLAB is the lack of the higher level abstractions for data structures like the table, cell arrays, etc. But, if you're simply "crunching numbers", oft times you can outperform MATLAB significantly with compiled code-- but, then again, if you can write your algorithm to get it into a vectorized form and down in the bowels of machine code in the BLAS routines or the like; you're essentially running the identical code because that's what MATLAB is built on at its very core for those operations. It's all the stuff added on around it that is the speed hog...
There are a lot of books -- from you description I'd probably recommend https://www.amazon.com/Fortran-90-95-Scientists-Engineers/dp/0072825758 as a starter...it's not a reference but not a "for dummies", either.
It's still at the F95 Standard, though and there are a lot of new features in F2003/2008 that are now in most compilers.
I've not kept up with it much having now been out of the consulting gig so I'm not so sure about what to recommend there.
Accepted Answer
A.B.
on 18 Aug 2020
Edited: A.B.
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
end
You can test it here: https://www.tutorialspoint.com/compile_fortran_online.php
More Answers (1)
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);
See Also
Categories
Find more on Fortran with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!