Réponses (2)

the cyclist
the cyclist le 25 Déc 2019

0 votes

You could try f2matlab. It might not get you all the way to the final code you need, but it will probably help.

6 commentaires

clc
clear
nx = 7;
ny = 13;
h = zeros(nx,ny);
h(:,:) = 100;
h;
Dx = 20;
amax = 1;
while amax >= 0.001
amax = 0;
for j = 2 : ny-1
h(1,j) = 0.02 * Dx * (j-2) + 100;
for i = 1 : nx
h(i,1) = h(i,3);
h(i,13) = h(i,11);
for j = 2 : ny-1
h(7,j) = h(i,11);
for i = 2 : ny - 1
for j = 2 : nx - 1
oldval = h(i,j);
h(i,j) = (h(i-1,j) + h(i+1,j) + h(i,j-1) + h(i,j+1))/4
e = abs(h(i,j) - oldval);
if e > amax
amax = e ;
end
end
end
end
end
end
end
h
hasan damaj
hasan damaj le 25 Déc 2019
Modifié(e) : hasan damaj le 25 Déc 2019
i tried to run the above, it gave me an index exceeds matrix dimension,
can u help me sir and thank u.
attached is the answer
Is that code the output of f2matlab, or is it something you wrote?
The specific reason your code fails is that h(i,j) is a 7x13 matrix, and in the line
h(i,j) = (h(i-1,j) + h(i+1,j) + h(i,j-1) + h(i,j+1))/4
when i == 7, you try to access element h(8,2), which does not exist.
I really can't figure out what you are trying to do in this code. The fact that you have loops over the variables i and j, inside other loops over i and j, is very confusing to me.
hasan damaj
hasan damaj le 25 Déc 2019
Modifié(e) : Image Analyst le 25 Déc 2019
Thank you for your comment.
I'm trying to convert the Fortran code to MATLAB.
"""""this is a regional flow example using gauss-seidel iteration"""
Image Analyst
Image Analyst le 25 Déc 2019
Modifié(e) : Image Analyst le 25 Déc 2019
That is supposed to be a comment. In MATLAB, it would look like this:
% This is a regional flow example using Gauss-Seidel iteration.
hasan damaj
hasan damaj le 25 Déc 2019
i know im not writing a code here :)
thank you

Connectez-vous pour commenter.

Ben Barrowes
Ben Barrowes le 26 Déc 2019
OCR and some hand editing produced this fortran code which I compiled and ran:
program flow
C RE GIONAL FLOW SYST&M &XAMPLE
dimENSION H(13, 7)
C INITIALIZE ALL H(I,.JJ VALUES TO BE l00.
DO 5 J=1,7
do 5 I=1,l3
H(I,J) = 100.
5 CONTINUE
c WATER TABLE BOUNOARY
DX=28.
DO 10 i=2,12
H(I,1) = 0.02*DX*(I-2)-100.
10 CONTINUE
C KEEP TRACK OP NUMBER OF ITERATIONS AND OF larRGEST ERROR
C NO-FLOW doundaries NEeD TO BE RESET WITHIN EACH ITERATION LOOP
NUMIT = 0
35 AMAX =0.
NUmIT = NUMIT + 1
C LeFT AND RIGHT NO-PLOW BOUNDARIES
DO 20 J=1,7
H(1,J) = H(3,J)
H (13,J) = H(11,J)
20 COnTINUe
C BOT10M N!rtLCW SOUNDAFC:t
DO 30 i=2,12
H(i,7) =H(i,5)
30 CONTINUE
C SWEEP INTERIOR POINTS WITH 5-POltn' OPERATOR
DO 40 J=2,6
DO 40 i=2,12
OLDVAL= H(I,J)
H(i,J) = (H(I-1,J) + H(I+1,J) + H(I,J-1) + H(I,J+1))/4.
ERR= ABS(H(I,J) -OLdVAL)
IF(ERR.GT.AKAX) AMAX=ERR
40 CONTINUE
C 00 ANOTHER ITERATION IF LARGEST ERROR AFFECTS JRD DECIMAL PLACE
IF (AMAX.GT.0.001) GO TO 35
C WE ARE OONE.
PRINT 50,NUmIT, ((H(I,J),I=2,12),J=l,6)
50 formAT(///1X,'NUMBER OF ITERATIONS IS',I4,///6(11f8.2///))
ENd
Using f2matlab and some other tools I have, this is the resulting matlab code. It seems to run, but I have not tested for accuracy of results. The fprintf statement could use some fixing for the results to look exactly the same.
function flow(varargin)
clear global;
clear functions;
global GlobInArgs nargs
GlobInArgs={mfilename,varargin{:}};
nargs=nargin+1;
global unit2fid;
if ~isempty(unit2fid), unit2fid={};
end
persistent akax amax dx err firstCall h i j l l3 numit oldval format_50
;
if isempty(firstCall),firstCall=1;end;
if firstCall;
format_50=[ '\n' , '\n' , '\n' ,blanks(1),'NUMBER OF ITERATIONS IS','%4d', '\n' , '\n' , '\n' ,'~',repmat([repmat('%8.2f',1,11), '\n' , '\n' , '\n' ] ,1,6)];
akax=0;
amax=0;
dx=0;
err=0;
h=zeros(13,7);
i=0;
j=0;
l=0;
l3=0;
numit=0;
oldval=0;
end
firstCall=0;
% RE GIONAL FLOW SYST&M &XAMPLE
% INITIALIZE ALL H(I,.JJ VALUES TO BE l00.
for j = 1: 7
for i = 1: l3
h(i,j) = 100.;
end
i = fix(l3+1);
end
j = fix(7+1);
% WATER TABLE BOUNOARY
dx = 28.;
for i = 2: 12
h(i,1) = 0.02.*dx.*(i-2) - 100.;
end
i = fix(12+1);
% KEEP TRACK OP NUMBER OF ITERATIONS AND OF larRGEST ERROR
% NO-FLOW doundaries NEeD TO BE RESET WITHIN EACH ITERATION LOOP
numit = 0;
while (1);
amax = 0.;
numit = fix(numit + 1);
% LeFT AND RIGHT NO-PLOW BOUNDARIES
for j = 1: 7
h(1,j) = h(3,j);
h(13,j) = h(11,j);
end
j = fix(7+1);
% BOT10M N!rtLCW SOUNDAFC:t
for i = 2: 12
h(i,7) = h(i,5);
end
i = fix(12+1);
% SWEEP INTERIOR POINTS WITH 5-POltn' OPERATOR
for j = 2: 6
for i = 2: 12
oldval = h(i,j);
h(i,j) =(h(i-1,j)+h(i+1,j)+h(i,j-1)+h(i,j+1))./4.;
err = abs(h(i,j)-oldval);
if(err > akax)
amax = err;
end
end
i = fix(12+1);
end
j = fix(6+1);
% 00 ANOTHER ITERATION IF LARGEST ERROR AFFECTS JRD DECIMAL PLACE
if(amax > 0.001)
continue;
end
% WE ARE OONE.
h(2:12,1:6)
%fprintf(1,[format_50],numit,{{h(i,j),'i','2','1','12'},'j','l','1','6'});
tempBreak=1;
break;
end
clear all
end %program flow

5 commentaires

hasan damaj
hasan damaj le 26 Déc 2019
thank you mr. ben
but the results are not the same and they are negative and i think you got the rows and columns switched..
Ben Barrowes
Ben Barrowes le 26 Déc 2019
Yes. As I wrote, I have not tested this for accuracy of the results. Please test and debug from here on. Hope this helps.
Mr. Ben, i attached a possible code i figured can u just look at it,
clc
clear
nx = 7;
ny = 13;
h = zeros(nx,ny);
h(:,:) = 100;
h;
Dx = 20;
amax = 1;
% while amax >= 0.001
% amax = 0;
for j = 2 : ny-1
h(1,j) = 0.02 * Dx * (j-2) + 100;
for i = 1 : nx
h(i,1) = h(i,3);
h(i,13) = h(i,11);
for j = 2 : ny-1
h(7,j) = h(5,j);
while amax >= 0.001
amax = 0 ;
for i = 2 : ny - 1
for j = 2 : nx - 1
oldval = h(i,j);
h(i,j) = (h(i-1,j) + h(i+1,j) + h(i,j-1) + h(i,j+1))/4;
e = abs(h(i,j) - oldval);
if e > amax
amax = e ;
end
end
end
end
end
end
end
h
is the location of while loop ok?? its giving me index exceeds matrix dimensions.
the above comments shows you the result of table of numbers
thank you
Ben Barrowes
Ben Barrowes le 26 Déc 2019
One thing I noticed looking at your code is that you have reused the same variable as the loop index variables. You have three loops that use J as the loop variable for example and two that use I as the loop variable. Use different variables for each loop index.
hasan damaj
hasan damaj le 26 Déc 2019
i cant change the variable because its 1 equation where h is in terms of i and j

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulation, Tuning, and Visualization dans Centre d'aide et File Exchange

Produits

Version

R2016a

Tags

Aucun tag saisi pour le moment.

Commenté :

le 26 Déc 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by