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

Programing, random walk applications


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.


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?


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.

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.

23-Emerald II

Donald Regula wrote:

...This problem is in Nahin's book - Number Crunching...

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

Donald Regula wrote:

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

Can you provide some screenshots?

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

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


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




for loop=1:N


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


if xdir<0.5





if ydir<0.5






if Y==0


elseif Y==100


elseif X==0










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.


The Mathcad program language has not the goto operator.

Therefore better use not flowcharts but structure symbols for graphical descriptions of Mathcad programs.

See please -

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


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.

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.


PS I agree that Paul Nahin writes great books!

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