samedi 25 mars 2017

Fortran to Matlab confused to if statement

I have to convert a fortran program to matlab. This is a part of it and I'm facing a problem. Although the fortran results are correct when I run the matlab script it's blocking in an infinite loop (?). I believe the problem is in the first IF statement. Am I missing something to my matlab conversion ? Thanks in advance.

This is the FORTRAN

 do j=1,n-1
    k=j
    if(k/2/=j/2.) then
      if(j==1) x1=x1+h
      if(j>1) x1=x1+2*h
      f1=100/(1+x1**2)
      sum1=sum1+f1
    else
      x2=x2+2*h
      f2=100/(1+x2**2)
      sum2=sum2+f2
    endif
 enddo

This is my MATLAB script

for j = 1:n-1
    k = j;
    if k/2 ~= j/2 
        if j == 1
            x1 = x1+h;
        end
        if j > 1
            x1 = x1+2*h;
        end   
        f1 = 100/(1+x1.^2);
        sum1 = sum1 + f1;
    else
        x2 = x2+2*h;
        f2 = 100/(1+x2.^2);
        sum2 = sum2 + f2;
    end
end

Aucun commentaire:

Enregistrer un commentaire