0

I am writing a code to find the solution to the Laplacian with a given set of boundary conditions using a Monte Carlo simulation in Octave. I have written the initial code to find single solutions, but this needs to be run quite a few times and then averaged to get a nice, smooth solution. That is the part I need help with, as I don't have a clue how to go about it. The code I have written is:

a=20;
s=1

for (m=s:s:a-s);
for (n=s:s:a-s);

x=m;
y=n;

for (i=1:5000)
R=randi(4);
if (R==1)
  xnew=x+s;
  ynew=y;
elseif (R==2)
  xnew=x-s;
  ynew=y;
elseif (R==3)
  xnew=x;
  ynew=y+s;
elseif (R==4)
  xnew=x;
  ynew=y-s;
endif

%hold on;
%figure(1);
%plot([x xnew],[y ynew])

x=xnew;
y=ynew;

if (x==0);
  u(n,m)=sin(pi*y/a);
  break
elseif (x==a);
  u(n,m)=0;
  break
elseif (y==0);
  u(n,m)=0;
  break
elseif (y==a);
  u(n,m)=0;
  break
else
  continue;
endif

endfor


endfor
endfor

figure(2);
contour(u)

In other words, what I want to do is record this value of "u" (the solution), run the program again, record that value of "u", and continue with this process a hundred times or so, then average them out and contour plot the average solution. I am fairly new to scripting, so any advice you can give would be greatly appreciated.

Thanks, Steve

1 Answers1

0

Why don't you just put it into another loop and store variables?

av_u=0;
count=0;
for i=1:hundred times
    %>>>your code to get u here<<<
    av_u=av_u+u;
    count=count+1;
end
av_u=av_u/count;
Dimitry
  • 2,204
  • 1
  • 16
  • 24