Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fortran to Matlab

Subject: Fortran to Matlab

From: John Wise

Date: 18 Jan, 2011 12:52:06

Message: 1 of 6

Hello everyone,
I tried to convert the fortran subroutine below to matlab function as shown below, but it doesnt give me the right results I wonder if anyone can help me to figure out where I made a mistake in the conversion
Thank you


subroutine rezcore(zp, z, tc, tpc, n, idm, count)
implicit none
integer n, idm, count
real zp(0:idm+1), z(0:idm), tc(0:idm), tpc(0:idm+1)
integer k, ih, ig, j
real s
k=0
ih=0
10 continue
k=k+1
ig=ih
20 continue
if(ih .ge. n) goto 30
if(zp(ih+1) .gt. z(k)) goto 30
ih=ih+1
goto 20
30 continue
if(ih .ne. ig) goto 40
tc(k)=tpc(ih+1)
goto 90
40 continue
s=0.0
j=ig+2
50 continue
if(j .gt. ih) goto 60
s=s+(zp(j)-zp(j-1))*tpc(j)
j=j+1
goto 50
60 continue
s=s+(zp(ig+1)-z(k-1))*tpc(ig+1)+(z(k)-zp(ih))*tpc(ih+1)
tc(k)=s/(z(k)-z(k-1))
90 continue
if(k .lt. n) goto 10
return
end
%---------------------------------------------------------------------------------------------------------------------%---------------------------------------------------------------------------------------------------------------------
function [tc]=rezcore(zp,z,tc,tpc,n,delz)
k=1;
ih=1;
while( true );
 k=k + 1;
 ig=ih;
 while( ih<n );
  if( zp(ih+1)>z(k) )
   break;
  end;
  ih=ih + 1;
 end;
 if( ih==ig )
  tc(k)=tpc(ih+1);
 else
  s=0.0;
  j=ig + 3;
  while( j<=ih );
   s=s +(zp(j)-zp(j-1)).*tpc(j);
   j=j + 1;
  end;
  s=s +(zp(ig+1)-z(k-1)).*tpc(ig+1) +(z(k)-zp(ih)).*tpc(ih+1);
  tc(k)=s./(z(k)-z(k-1));
 end;
 if( k>=n )
  break
 end
end

end

Subject: Fortran to Matlab

From: Steven_Lord

Date: 18 Jan, 2011 14:35:34

Message: 2 of 6



"John Wise" <khabuman@hotmail.co.uk> wrote in message
news:ih42dm$qfd$1@fred.mathworks.com...
> Hello everyone,
> I tried to convert the fortran subroutine below to matlab function as
> shown below, but it doesnt give me the right results I wonder if anyone
> can help me to figure out where I made a mistake in the conversion
> Thank you

Run your Fortran code in a debugger side-by-side with the MATLAB code
running in the Debugger. Compare results at each step; when you see the
results of the two programs start to diverge, investigate the code that you
just executed in each language more closely.

If you're not familiar with how to debug Fortran code, you could simply
instrument it by adding display statements at certain locations and compare
the results that way.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Fortran to Matlab

From: dpb

Date: 18 Jan, 2011 15:34:17

Message: 3 of 6

John Wise wrote:
> Hello everyone,
> I tried to convert the fortran subroutine below to matlab function as
> shown below, but it doesnt give me the right results I wonder if anyone
> can help me to figure out where I made a mistake in the conversion
> Thank you
>
>
> subroutine rezcore(zp, z, tc, tpc, n, idm, count)
> implicit none
> integer n, idm, count
> real zp(0:idm+1), z(0:idm), tc(0:idm), tpc(0:idm+1)
> integer k, ih, ig, j
> real s
> k=0
> ih=0
> 10 continue
> k=k+1
> ig=ih
> 20 continue
> if(ih .ge. n) goto 30
> if(zp(ih+1) .gt. z(k)) goto 30
...
>
> function [tc]=rezcore(zp,z,tc,tpc,n,delz)
> k=1;
> ih=1;
> while( true );
> k=k + 1;
> ig=ih;
> while( ih<n );
...

Don't know if it's all or not, but the translation from 0-based to
1-based arrays has a problem here...there's probably more related to that.

One pita in ML is not being able to specify array lower bounds.

How about just compiling the Fortran in a mex-file instead?

--

Subject: Fortran to Matlab

From: dpb

Date: 18 Jan, 2011 16:03:55

Message: 4 of 6

dpb wrote:
...

>> while( ih<n );
> ...
>
> Don't know if it's all or not, but the translation from 0-based to
> 1-based arrays has a problem here...there's probably more related to that.
>
...

Take it back; that one is ok -- so, I haven't looked enough to tell
where there is a problem, sorry.

--

Subject: Fortran to Matlab

From: dpb

Date: 18 Jan, 2011 20:20:19

Message: 5 of 6

dpb wrote:
> dpb wrote:
> ...
>
>>> while( ih<n );
>> ...
>>
>> Don't know if it's all or not, but the translation from 0-based to
>> 1-based arrays has a problem here...there's probably more related to
>> that.
>>
> ...
>
> Take it back; that one is ok -- so, I haven't looked enough to tell
> where there is a problem, sorry.
...

Oh, just a thought -- should n --> n+1, too??? Didn't look at the
algorithm/code any more, just a random thought related to the origin
base stuff...

--

Subject: Fortran to Matlab

From: benbarrowes

Date: 19 Jan, 2011 12:30:56

Message: 6 of 6

On Jan 18, 7:52 am, "John Wise" <khabu...@hotmail.co.uk> wrote:
> Hello everyone,
> I tried to convert the fortran subroutine below to matlab function as shown below, but it doesnt give me the right results I wonder if anyone can help me to figure out where I made a mistake in the conversion
> Thank you
>
> subroutine rezcore(zp, z, tc, tpc, n, idm, count)
> implicit none
> integer n, idm, count
> real zp(0:idm+1), z(0:idm), tc(0:idm), tpc(0:idm+1)
> integer k, ih, ig, j
> real s
> k=0
> ih=0
> 10 continue
> k=k+1
> ig=ih
> 20 continue
> if(ih .ge. n) goto 30
> if(zp(ih+1) .gt. z(k)) goto 30
> ih=ih+1
> goto 20
> 30 continue
> if(ih .ne. ig) goto 40
> tc(k)=tpc(ih+1)
> goto 90
> 40 continue
> s=0.0
> j=ig+2
> 50 continue
> if(j .gt. ih) goto 60
> s=s+(zp(j)-zp(j-1))*tpc(j)
> j=j+1
> goto 50
> 60 continue
> s=s+(zp(ig+1)-z(k-1))*tpc(ig+1)+(z(k)-zp(ih))*tpc(ih+1)
> tc(k)=s/(z(k)-z(k-1))
> 90 continue
> if(k .lt. n) goto 10
> return
> end
> %---------------------------------------------------------------------------------------------------------------------%---------------------------------------------------------------------------------------------------------------------
> function [tc]=rezcore(zp,z,tc,tpc,n,delz)
> k=1;
> ih=1;
> while( true );
>  k=k + 1;
>  ig=ih;
>  while( ih<n );
>   if( zp(ih+1)>z(k) )
>    break;
>   end;
>   ih=ih + 1;
>  end;
>  if( ih==ig )
>   tc(k)=tpc(ih+1);
>  else
>   s=0.0;
>   j=ig + 3;
>   while( j<=ih );
>    s=s +(zp(j)-zp(j-1)).*tpc(j);
>    j=j + 1;
>   end;
>   s=s +(zp(ig+1)-z(k-1)).*tpc(ig+1) +(z(k)-zp(ih)).*tpc(ih+1);
>   tc(k)=s./(z(k)-z(k-1));
>  end;
>  if( k>=n )
>   break
>  end
> end
>
> end


Here is a fortran version without goto's. Since you don't give any
sample input, I can't validate this because I can't compile and run it
without guessing at inputs.

subroutine rezcore(zp, z, tc, tpc, n, idm, count)
implicit none
integer n, idm, count
real zp(0:idm+1), z(0:idm), tc(0:idm), tpc(0:idm+1)
integer k, ih, ig, j
real s
k=0
ih=0
do
 k=k+1
 ig=ih
 do
  if(ih .ge. n) exit
  if(zp(ih+1) .gt. z(k)) exit
  ih=ih+1
 end do
 do
  if (ih == ig) then
   tc(k)=tpc(ih+1)
   exit
  end if
  s=0.0
  j=ig+2
  do
   if(j .gt. ih) exit
   s=s+(zp(j)-zp(j-1))*tpc(j)
   j=j+1
  end do
  s=s+(zp(ig+1)-z(k-1))*tpc(ig+1)+(z(k)-zp(ih))*tpc(ih+1)
  tc(k)=s/(z(k)-z(k-1))
  exit
 end do
 if(k .ge. n) exit
end do
return
end subroutine rezcore


The next step is to convert it to matlab. Here is what f2matlab gives
me:


function [zp, z, tc, tpc, n, idm, countmlv]=rezcore(zp, z, tc, tpc, n,
idm, countmlv);
persistent ig ih j k s ;

if isempty(k), k=0; end;
if isempty(ih), ih=0; end;
if isempty(ig), ig=0; end;
if isempty(j), j=0; end;
if isempty(s), s=0; end;
k=0;
ih=0;
while (1);
 k=fix(k+1);
 ig=fix(ih);
 while (1);
  if(ih >= n)
   break;
  end;
  if(zp(ih+1+1) > z(k+1))
   break;
  end;
  ih=fix(ih+1);
 end;
 while (1);
  if(ih == ig)
   tc(k+1)=tpc(ih+1+1);
   break;
  end;
  s=0.0;
  j=fix(ig+2);
  while (1);
   if(j > ih)
    break;
   end;
   s=s+(zp(j+1)-zp(j-1+1)).*tpc(j+1);
   j=fix(j+1);
  end;
  s=s+(zp(ig+1+1)-z(k-1+1)).*tpc(ig+1+1)+(z(k+1)-zp(ih+1)).*tpc(ih
+1+1);
  tc(k+1)=s./(z(k+1)-z(k-1+1));
  break;
 end;
 if(k >= n)
  break;
 end;
end;
return;
end %subroutine rezcore



Note that the odd (for matlab) variable handling (persistent, isempty
checks) can be necessary due to how fortran and matlab scope variables
differently. Also note that f2matlab deals with 0 (or negative)
indexing by simply adding the appropriate number(s) to the index(ces).
The variable count was renamed as well because count is intrinsic to
matlab.

Again, without a test case, this is not validated.

Good luck,
Ben Barrowes
http://www.mathworks.com/matlabcentral/fileexchange/5260

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us