How to convert script from fortran90 to matlab language
2 views (last 30 days)
Show older comments
Can anyone help me to covert this script
Thank you
DIMENSION KB(10) KA(10)
KL=0
KS=1
DO 210 K=1,N0
K1=K+1
IF(A(K)*A(K1).LT.0.)THEN
KS=-KS
IF(KS.EQ.-1)THEN
KL=KL+1
KA(KL)=K1
ENDIF
IF(KS.EQ.1)THEN
KB(KL)=K
ENDIF
ENDIF
210 CONTINUE
IF(KL.GT.0)THEN
DO 230 KQ=1,KL
KAL=KA(KL)
KBL=KB(KL)
K0=KAL-1
K1=KBL+1
XA=H*FLOAT(K0)
XB=H*FLOAT(K1)
FA=A(K0)
FB=A(K1)
DO 220 K=KAL,KBL
XK=H*FLOAT(K)
A(K)=FA+(FB-FA)*(XK-XA)/(XB-XA)
220 CONTINUE
230 CONTINUE
ENDIF
1 Comment
Fabio Freschi
on 27 Oct 2019
this is not a complete Fortran script (and it doesn't use F90 syntax): some variable are missing, such as N0,
Accepted Answer
Fabio Freschi
on 27 Oct 2019
Again, it is difficult to check if not all code is available, however I try
kB = zeros(10,1);
kA = zeros(10,1);
kL = 0;
kS = 1;
for k = 1:N0
k1 = k+1;
if A(k)*A(k1) < 0
kS = -kS;
if kS == -1
kL = kL+1;
kA(kL) = k1;
end
if kS == 1
kB(kL) = k;
end
end
end
if kL > 0
for kQ = 1:kL
kAL = kA(kL);
kBL = kB(kL);
k0 = kAL-1;
K1 = kBL+1;
xA = H*double(k0);
xB = H*double(k1);
FA = A(k0);
FB = A(k1);
for k = kAL:kBL
xK = H*double(k);
A(k) = FA+(FB-FA)*(xK-xA)/(xB-xA);
end
end
end
It goes without saying that this is an attempt to translate the code line-by-line, but it could not be the best matlab code.
3 Comments
Fabio Freschi
on 28 Oct 2019
They are declaration of variables.
The second is simple, just allocate the memory for a double array
b = zeros(N,1);
The first is similar. The only problem is that the variable is declared assuming 0-indexing. Matlab does not allow 0-indexing so you can write
a = zeros(n+1,1);
and scale the 1-indexing in the code
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!