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!