Converting working Fortran code to MATLAB
이전 댓글 표시
Hello,
I am trying to convert a Fortran code into a Matlab code. Most of the fortran code consists of matrices / arrays such as T(i,k), u(i,k), x(i), dpdx(x).
I'm wondering what is the best way to replicate those arrays in matlab. For instance the matrix T(i,k) in the for loop, shouldn't I use the zeros command instead of writing T(i,k) = 0 which is part of my initial condtions for my problem??
I'm a programming newbie so some code modifications are not as obvious to me.
One piece of the Fortran code is as following:
"
imin = 0
imax = 100
kmin = 0
kmax = 100
%%%
do i = imin, imax, 1
x(i) = xmin + real (i - imin, kr) / real (imax - imin, kr) * (xmax - xmin)
h(i) = hm + x(i) ** 2.0_kr / (2.0_kr * Rad)
dpdx(i) = 6.0_kr * visc / h(i) ** 3.0_kr * ((U_ls + U_us) * h(i) + cons1)
do k = kmin, kmax, 1
z(k) = zmin + real (k - kmin, kr) / real (kmax - kmin, kr) * (h(i) - zmin)
T(i,k) = 0.0_kr
A1 = dpdx(i) / (2.0_kr * visc)
A2 = (- dpdx(i) / (2.0_kr * visc) * h(i) + (U_us - U_ls) / h(i))
u(i,k) = A1 * z(k) ** 2.0_kr + A2 * z(k) + U_ls
dudz(i,k) = 2.0_kr * A1 * z(k) + A2
enddo
enddo
%%%
My matlab equivalent currently looks like this:
for i = 1:imax
x(i)= xmin + (i - imin) / (imax - imin) * (xmax - xmin);
h(i) = hmin + x(i).^2 / (2 * Rad);
dpdx = 6 * visc /(h(i).^3) * ((U_ls * U_us) * h(i) + cons1);
for k = kmin:kmax
z(i) = zmin + (k - kmin) / (kmax - kmin) * (h(i) - zmin);
T(i,k) = 0
A1 = dpdx / (2 * visc);
A2 = (-dpdx / (2 * visc)) * h(i) + (U_us - U_ls) / h(i);
u(i,k) = A1 * z(i).^2 + A2 * z(i) + U_ls;
dudz(i,k) = 2 * A1 * z(i) + A2;
end
end
I'm not sure about the i and k arrays in the for loop which in the original code run from 0 to 100 but in matlab they go from 1 to 100, how do I correct this?
Please ignore the other variables I listed like hmin, visc, Rad etc. I would really appreciate it if someone could tell me that the structure of my matlab code is correct and would yield me the same output as the original Fortran code.
댓글 수: 1
KSSV
2020년 6월 10일
In MATLAB the indices should be posititve integers. You can run the loop from i = 1 to 101.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!