cancel
Showing results for
Search instead for
Did you mean:
cancel
Showing results for
Search instead for
Did you mean:

## Programing, random walk applications  Newbie

## Programing, random walk applications

This is a program I am having a problem getting to run. It is using a random walk to determine a "temperature" at a point (xo,yo) in a square plate. The temperature distribution is T(x,y) with the boundaries held at specified temperatures. There is no heat transfer in the z-direction. The method is solving for

T(x,y) one point at a time for the LaPlace equation.

The method is based on the relaxation technique, where at at given point there is 1/4 probability to move in the four directions x+, x-, y+, y-. Several random walks are performed from (xo,yo), each time reaching a different boundary. The frequency of how often one reaches a given boundary out of N random walk trials times the temperature of that boundary will estimate T(xo,yo) =( Sfi*Ti). (This is a problem given in NUMBER CRUNCHING, by Paul J. Nahin. He writes great books)

The attached Mathcad 14.0 program which includes a schematic of the heat transfer scenario, What I think is the program algorithm and the Mathcad program is attached. I have tried several "permutations" of the program and still can't get it to work. Help, please.

Tags (1)
8 REPLIES 8  Peridot
(in response to ptc-1996077)

## Re: Programing, random walk applications

Can you provide a bit more information about the algorithm?

I'm confused about the quantities involved here: the first diagram seems to imply that T represents temperature but x & y imply distance (over 0 to 1, which contradicts the xo and yo initial values)

Furthermore, , I can't see how it ever gets into the while loop, as T4 = T2 and T isn't updated at any stage, so the relation T4<y<T2 is never true,

Your "if xdir<0.5" section doesn't appear to agree with the flowchart, which seems to require both xdir and ydir be less than 0.5 to increment x and y?

Stuart

PS. It's unusual to see one-line multiple assignments done as part of a boolean expression; it works but could be confusing to some readers by implying that it's part of the logic.  Newbie
(in response to StuartBruff)

## Re: Programing, random walk applications

This problem is in Nahin's book - Number Crunching. The "exchange" of (x,y) - (Tx, Ty) works out because he chose one side to have a temperature of 100 C and the other three side as 0 C. It is a unit square divided into 100 parts and the temperature is divided into 100 parts. Ordinarily one would have to choose a different "delta" for space and temperature.

As I mentioned, I thought I had the algorithm correct, but not sure. Also, I never have any luck setting up a "while loop". The idea is to random walk in the unit plane until one of the edges is reached. At each point one has four choices: + or - 1 x and + or - 1y ( in temperature units). Depending on which edge is reached a frequency( fi, i =1,2,3 or 4) of one is recorded for that edge. After many random walks, the quantity: sum fiTi then estimates the steady state temperature ( via solution of the LaPlace) for the starting point or (xo, yo).

Nahin sets up this problem in Matlab - I may be able to scan his program. I tried to duplicate his program in Mathcad and obviously didn't succeed.

Don R.  Emerald II
(in response to ptc-1996077)

## Re: Programing, random walk applications

 Donald Regula wrote:...This problem is in Nahin's book - Number Crunching...

Some of the pages of this book can be found here:

http://books.google.ru/books?id=pxz8G-KF3ZgC&printsec=frontcover&hl=ru#v=onepage&q&f=false

 Donald Regula wrote:...Nahin sets up this problem in Matlab - I may be able to scan his program...

Can you provide some screenshots?  Moonstone
(in response to ptc-1996077)

## Re: Programing, random walk applications

 Donald Regula wrote:Nahin sets up this problem in Matlab - I may be able to scan his program.

The Matlab code can be obtained from http://press.princeton.edu/links/numbercrunchingcodes.pdf

It is as follows (unfortunately the indentation formatting has been lost in copying!) :

%walk.m

N=input('How many walks?');

X=input('X=?');Y=input('Y=?');

X1=X;Y1=Y;

fraction=zeros(1,4);

for loop=1:N

X=X1;Y=Y1;

while X>0&&X<100&&Y>0&&Y<100

xdir=rand;ydir=rand;

if xdir<0.5

X=X+1;

else

X=X-1;

end

if ydir<0.5

Y=Y+1;

else

Y=Y-1;

end

end

if Y==0

i=1;

elseif Y==100

i=3;

elseif X==0

i=2;

else

i=4;

end

fraction(i)=fraction(i)+1;

end

fraction=fraction/N;

fraction,100*fraction(1)

Alan  Moonstone
(in response to StuartBruff)

## Re: Programing, random walk applications

 StuartBruff wrote:Can you provide a bit more information about the algorithm?

If you're interested in the basic idea behind the method it's explained briefly in the attached pdf article. This appeared in Mathematics Today magazine last August. The specific example Donald is trying to solve is only mentioned in passing at the end though.

Alan  Ruby I
(in response to ptc-1996077)

## Re: Programing, random walk applications

The Mathcad program language has not the goto operator.

Therefore better use not flowcharts but structure symbols for graphical descriptions of Mathcad programs.  Moonstone
(in response to ptc-1996077)

## Re: Programing, random walk applications

More like the attached I think (though you'll probably need far more than 360 trials to get a decent result).

Alan

Note: Don't confuse displacement and temperature!! Unfortunately, your displacements (x and y) run from 0 to 100, and your temperatures are either 0 or 100. This doesn't mean you can sensibly compare a displacement and a temperature.  Moonstone
(in response to AlanStevens)

## Re: Programing, random walk applications

On second thoughts, my previous effort always made an x-direction change before a y-direction change. The direction changes should be random, so the attached is better.

Alan

PS I agree that Paul Nahin writes great books!

Announcements
Check out the latest
Mathcad Tip
"PTC Mathcad 15 / Prime 1-6 Update."