how to convert to matlab from fortran
1 view (last 30 days)
Show older comments
Answers (2)
the cyclist
on 25 Dec 2019
You could try f2matlab. It might not get you all the way to the final code you need, but it will probably help.
6 Comments
Image Analyst
on 25 Dec 2019
Edited: Image Analyst
on 25 Dec 2019
That is supposed to be a comment. In MATLAB, it would look like this:
% This is a regional flow example using Gauss-Seidel iteration.
Ben Barrowes
on 26 Dec 2019
OCR and some hand editing produced this fortran code which I compiled and ran:
program flow
C RE GIONAL FLOW SYST&M &XAMPLE
dimENSION H(13, 7)
C INITIALIZE ALL H(I,.JJ VALUES TO BE l00.
DO 5 J=1,7
do 5 I=1,l3
H(I,J) = 100.
5 CONTINUE
c WATER TABLE BOUNOARY
DX=28.
DO 10 i=2,12
H(I,1) = 0.02*DX*(I-2)-100.
10 CONTINUE
C KEEP TRACK OP NUMBER OF ITERATIONS AND OF larRGEST ERROR
C NO-FLOW doundaries NEeD TO BE RESET WITHIN EACH ITERATION LOOP
NUMIT = 0
35 AMAX =0.
NUmIT = NUMIT + 1
C LeFT AND RIGHT NO-PLOW BOUNDARIES
DO 20 J=1,7
H(1,J) = H(3,J)
H (13,J) = H(11,J)
20 COnTINUe
C BOT10M N!rtLCW SOUNDAFC:t
DO 30 i=2,12
H(i,7) =H(i,5)
30 CONTINUE
C SWEEP INTERIOR POINTS WITH 5-POltn' OPERATOR
DO 40 J=2,6
DO 40 i=2,12
OLDVAL= H(I,J)
H(i,J) = (H(I-1,J) + H(I+1,J) + H(I,J-1) + H(I,J+1))/4.
ERR= ABS(H(I,J) -OLdVAL)
IF(ERR.GT.AKAX) AMAX=ERR
40 CONTINUE
C 00 ANOTHER ITERATION IF LARGEST ERROR AFFECTS JRD DECIMAL PLACE
IF (AMAX.GT.0.001) GO TO 35
C WE ARE OONE.
PRINT 50,NUmIT, ((H(I,J),I=2,12),J=l,6)
50 formAT(///1X,'NUMBER OF ITERATIONS IS',I4,///6(11f8.2///))
ENd
Using f2matlab and some other tools I have, this is the resulting matlab code. It seems to run, but I have not tested for accuracy of results. The fprintf statement could use some fixing for the results to look exactly the same.
function flow(varargin)
clear global;
clear functions;
global GlobInArgs nargs
GlobInArgs={mfilename,varargin{:}};
nargs=nargin+1;
global unit2fid;
if ~isempty(unit2fid), unit2fid={};
end
persistent akax amax dx err firstCall h i j l l3 numit oldval format_50
;
if isempty(firstCall),firstCall=1;end;
if firstCall;
format_50=[ '\n' , '\n' , '\n' ,blanks(1),'NUMBER OF ITERATIONS IS','%4d', '\n' , '\n' , '\n' ,'~',repmat([repmat('%8.2f',1,11), '\n' , '\n' , '\n' ] ,1,6)];
akax=0;
amax=0;
dx=0;
err=0;
h=zeros(13,7);
i=0;
j=0;
l=0;
l3=0;
numit=0;
oldval=0;
end
firstCall=0;
% RE GIONAL FLOW SYST&M &XAMPLE
% INITIALIZE ALL H(I,.JJ VALUES TO BE l00.
for j = 1: 7
for i = 1: l3
h(i,j) = 100.;
end
i = fix(l3+1);
end
j = fix(7+1);
% WATER TABLE BOUNOARY
dx = 28.;
for i = 2: 12
h(i,1) = 0.02.*dx.*(i-2) - 100.;
end
i = fix(12+1);
% KEEP TRACK OP NUMBER OF ITERATIONS AND OF larRGEST ERROR
% NO-FLOW doundaries NEeD TO BE RESET WITHIN EACH ITERATION LOOP
numit = 0;
while (1);
amax = 0.;
numit = fix(numit + 1);
% LeFT AND RIGHT NO-PLOW BOUNDARIES
for j = 1: 7
h(1,j) = h(3,j);
h(13,j) = h(11,j);
end
j = fix(7+1);
% BOT10M N!rtLCW SOUNDAFC:t
for i = 2: 12
h(i,7) = h(i,5);
end
i = fix(12+1);
% SWEEP INTERIOR POINTS WITH 5-POltn' OPERATOR
for j = 2: 6
for i = 2: 12
oldval = h(i,j);
h(i,j) =(h(i-1,j)+h(i+1,j)+h(i,j-1)+h(i,j+1))./4.;
err = abs(h(i,j)-oldval);
if(err > akax)
amax = err;
end
end
i = fix(12+1);
end
j = fix(6+1);
% 00 ANOTHER ITERATION IF LARGEST ERROR AFFECTS JRD DECIMAL PLACE
if(amax > 0.001)
continue;
end
% WE ARE OONE.
h(2:12,1:6)
%fprintf(1,[format_50],numit,{{h(i,j),'i','2','1','12'},'j','l','1','6'});
tempBreak=1;
break;
end
clear all
end %program flow
5 Comments
Ben Barrowes
on 26 Dec 2019
One thing I noticed looking at your code is that you have reused the same variable as the loop index variables. You have three loops that use J as the loop variable for example and two that use I as the loop variable. Use different variables for each loop index.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!