coverting fortran to matlab
Show older comments

5 Comments
dpb
on 29 Nov 2019
Show us your work on the problem to date...where did you get stuck?
HINT: Turn the GOTO into a WHILE structure.
hasan damaj
on 29 Nov 2019
Edited: dpb
on 29 Nov 2019
Get rid of the the clc and clear and incorporate as a function, first.
You'll have to set AMAX to a value greater than the error tolerance (0.01) in order for the while loop to execute.
You can, of course, write the whole expression in MATLAB w/o looping over the H array using vector operations. filter is useful for this.
hasan damaj
on 29 Nov 2019
hasan damaj
on 29 Nov 2019
Answers (2)
J Chen
on 29 Nov 2019
The following statements are wronng
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;
It should be
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 );
Change while amax > 0.01 to
while 1
..
if ( amax > 0.01 )
amax = 0;
else
break
end
end
One end statement has been missing in your code.
6 Comments
hasan damaj
on 29 Nov 2019
dpb
on 29 Nov 2019
Edited: Walter Roberson
on 29 Nov 2019
Read the original code...where do you think it should go?
hasan damaj
on 29 Nov 2019
Edited: Walter Roberson
on 29 Nov 2019
J Chen
on 29 Nov 2019
Replace "while amax > 0.01" with "while 1".
Add
if ( amax > 0.01 )
amax = 0;
else
break
end
just before the corresponding end for the while, i.e., at the end of the while loop. Try your code and figure out the rest!
Where is the looping structure in the original? That's what you need to emulate.
I don't quite agree with the other poster's suggestion (not that it won't work but it isn't my "cup of tea" in how I'd write it).
I pointed out above the modifications needed to write the while as
amax=1;
while amax>E % set E to desired tolerance value
...
end
Walter Roberson
on 29 Nov 2019
while 1 .... ???????
.. (what is this)??????
In MATLAB, if and while are considered true provided that all of the values in the condition are non-zero. The constant 1 there will always be non-zero, so this code is expressing an infinite loop.
MATLAB happens to use the numeric value 1 for the logical value true so a directly equivalent way of writing while 1 is while true -- which is a form I am more likely to write.
hasan damaj
on 11 Dec 2019
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!