NickFort
Technical User
- Jun 10, 2010
- 113
I'm trying to translate a piece of code from MATLAB to Fortran (one that typically has lots of iterations), line-by-line. To check that everything is fine and dandy, in both, I output all of the variables at each assignment (an arduous process!). What I've found is that as the iterations proceed, the Fortran code deviates further and further from the MATLAB, by very small amounts that accumulate to something noticeable by the end of the iterations.
Both use double precision, yet, somehow, MATLAB seems to maintain its accuracy while Fortran loses it. I think that the two somehow handle the finite nature of machine numbers differently.
The errors (i.e. difference between MATLAB and Fortran) start off somewhere around 0.000001%, and end off at about 0.08%. One variable even peaks at about 3% difference. This, unfortunately, seems to have an effect on the stability of the whole calculation.
Any idea why there is this discrepancy, and -- more importantly -- do you have any suggestions for mitigating this? Any strategies would be helpful!
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
Both use double precision, yet, somehow, MATLAB seems to maintain its accuracy while Fortran loses it. I think that the two somehow handle the finite nature of machine numbers differently.
The errors (i.e. difference between MATLAB and Fortran) start off somewhere around 0.000001%, and end off at about 0.08%. One variable even peaks at about 3% difference. This, unfortunately, seems to have an effect on the stability of the whole calculation.
Any idea why there is this discrepancy, and -- more importantly -- do you have any suggestions for mitigating this? Any strategies would be helpful!
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.