Dirichlet problem Monte Carlo random walks

I'm trying to solve the Dirichlet problem (https://en.wikipedia.org/wiki/Dirichlet_problem) which can be applied on for example two dimensional temperature distribution in a wall. I've figured out how to calculate the temperature in one single point if you have the temperature in the boundaries (The four walls). But I don't know how I should continue if I want to calculate the temperature in several internal points. The code for one internal point is:
imax=101; %number of x-grid connections
jmax=101; %number of y-grid connections.
nmax=10000; %random of random walks
bottom_bc=21; %The different temperatures of the four walls/boundary conditions
right_bc=25;
top_bc=36;
left_bc=41;
s=0; %Initalize the scalar potential
for n=1:nmax
i=51;j=51; %begin the random walk at the centre
while(i~=1)&&(j~=1)&&(i~=imax)&&(j~=jmax) %continue random walking until reaching a wall
r=rand;
if r<0.25
i=i+1; %Step right
elseif (r>=0.25)&&(r<0.5)
i=i-1; %step left
elseif (r>=0.5)&&(r<0.75)
j=j+1; %step up
else
j=j-1; %step down
end
end
if (j==1)&&(i~=1)&&(i~=imax) %reaching a wall (Then the random walk stops)
s=s+bottom_bc;
elseif (j==jmax)&&(i~=1)&&(i~=imax)
s=s+top_bc;
elseif (i==1)&&(j~=1)&&(j~=jmax)
s=s+left_bc;
elseif (i==imax)&&(j~=1)&&(j~=jmax)
s=s+right_bc;
end
end
s=s/nmax %Divide by the total number of walks

Answers (0)

Categories

Asked:

on 4 Dec 2014

Community Treasure Hunt

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

Start Hunting!