## converting a code from Matlab to Fortran, but Fortran diverged

## converting a code from Matlab to Fortran, but Fortran diverged

(OP)

thread214-1651477: Fortran's precision vs. MATLAB's

I am dealing with the same problem. I translated a code from Matlab to Fortran ( CVF). The Matlab code works pretty well but in iteration loop in Fortran the error is accumulated to a unacceptable number. I used double precision in Fortran. I don't understand why Matlab can calculate the result with just a 4-5 iteration but the Fortran diverges.

Is the anyone who can help me out with this?

I am dealing with the same problem. I translated a code from Matlab to Fortran ( CVF). The Matlab code works pretty well but in iteration loop in Fortran the error is accumulated to a unacceptable number. I used double precision in Fortran. I don't understand why Matlab can calculate the result with just a 4-5 iteration but the Fortran diverges.

Is the anyone who can help me out with this?

Ehsan Safari,

Reservoir Engineer

ICOFC

www.icofc.org

## RE: converting a code from Matlab to Fortran, but Fortran diverged

To find the error, you need to debug your program.

IMO, nobody could help you, without seeing an example where it happens.

## RE: converting a code from Matlab to Fortran, but Fortran diverged

what do you mean by initialization of variables?

here is the part of the code that the divergence starts. The main code is too big to post it here.

the problems occurs in the do-while loop as in Matlab the results is obtained after 4-5 iteration but in Fortran it will never get out of the loop. I am sure all the subroutine inside the loop are working properly except the subroutine VapourPhase which its results have some discrepancy of 0.000000001%.

## CODE --> matlab

## CODE --> Fortran

Ehsan Safari,

Reservoir Engineer

ICOFC

www.icofc.org

## RE: converting a code from Matlab to Fortran, but Fortran diverged

You could try to print the arrays after each step and compare them with the same arrays computed by Matlab.