%generate Brownian paths
clear
M = input('enter the number of paths: ');
N = input('enter the number of steps: ');
mu = input('enter mu: ');
sigma = input('enter sigma: ');
T = 1;
dt = T/N;

x = zeros(M,N+1);
w = randn(M,N);

t = 0:dt:T;

% generate brownian paths:
for k=1:N
    x(:,k+1) = x(:,k) + mu*dt + sigma*sqrt(dt)*w(:,k);
end

% generate geometric brownian paths:
y = exp(x);

figure

hold on

for l=1:M
    plot(t,x(l,:),'g');
end
xlabel('time'); 
ylabel('x');
title('Brownian Paths');
axis([0 T -3 3]);

hold off

figure

hold on

for l=1:M
    plot(t,y(l,:),'r');
end
xlabel('time'); 
ylabel('y');
title('Geometric Brownian Paths');
axis([0 T 0 10]);

hold off