% simulated 2D brownian motion
% input is N, the number of steps
% the final time T,  dt = T/N
% sigma the noise variance
% picstep is the interval between plots
% starting point is (x,y) = (0,0)
clear, close all

N = input('enter number of time steps \n')
tfinal = input('enter final time T \n')
sigma = input('noise variance \n')
picstep = input('integer interval between time steps \n')
t = linspace(0,tfinal,N);
dt = tfinal/N;
dtsq = sqrt(dt);

x=zeros(size(t)); y = x;

for ii = 1:N
    x(ii+1) = x(ii) + sigma*dtsq*randn;
    y(ii+1) = y(ii) + sigma*dtsq*randn;
    if mod(ii,picstep)==0
        plot(x,y,'*')
        disp('hit any key to continue')
        xlabel('x'), ylabel('y')
        tst = num2str(ii)
        tmp = strcat('at time step ',tst)
        title(tmp)
        grid
        pause
    end
    
end
plot(x,y)
grid
xlabel('x'), ylabel('y')
