%% Brownian.m

clear all;
%randn('seed',0);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Definitions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

N   = 1000; % Anz. d. Zeitschritte
M   = 500; % Anz. d. Realisierungen
TAU = 1;  % interne Schrittweite
X   = zeros(M,N);
EX  = zeros(1,N);
VX  = zeros(1,N);

%%%%%%%%%%%%%%%%%%%%%%%% Simulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for l=1:M        
        
    for k=1:N-1
        X(l,k+1) = X(l,k) - sqrt(TAU)*randn(1);
    end
               
end; %for

EX=sum(X,1)./M;
VX=sum((X-repmat(EX,M,1)).^2,1)/(M-1);

%%%%%%%%%%%%%%%%%%%%%%%%%% Plots %%%%%%%%%%%%%%%%%%%%%%%


figure(1)
plot(X,TAU.*[1:N])
xlabel('x')
ylabel('t')
title([num2str(M),' unabhängige Realisierungen einer Brownschen Bewegung'])


figure(2)
subplot(2,1,1)
plot(EX,TAU.*[1:N])
xlim([-10,10])
xlabel('E(x(t))')
ylabel('t')
title(['Erwartungswert (Mittel über alle ',num2str(M),' Realisierungen)'])
subplot(2,1,2)
plot(VX,TAU.*[1:N])
xlabel('Var(x(t))')
ylabel('t')
title(['Varianz (Mittel über alle ',num2str(M),' Realisierungen)'])

