%This is the matlab program to gnerate geometric brownian motion using random walk.
%Geometric_brownian(N,r,sigma,T) simulates a Geometric Brownian motion on [0,T].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % N            =      Number of steps
 % r            =      Interest rate
 % sigma        =      Diffusion coefficient
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
%Concept:- If X is brownian motion then exp(X) will be geometric Brownian motion. 
% Program is written by Mr. Jitendra Singh (M.Tech student).

function [Y] = geometric_brownian(N,r,sigma,T)
%N=input('number of step:');
N = 1000;
T = input('Enter length of interval:');
r = input('Enter interest rate:');
sigma = input('Enter diffusion coefficient:');

t = (0:1:N)'/N;                                 % t is the column vector [0 1/N 2/N ... 1]

W = [0; cumsum(randn(N,1))]/sqrt(N);            % cumsum function is running sum of N(0,1/N) variables.
t = t*T;
W = W*sqrt(T);
X = (r-(sigma^2)/2)*t + sigma * W;
Y = exp(X);
plot(t,Y);         
title(['Sample Path of Geometric Brownian motion with diffusion cofficient=' num2str(sigma),' Interest rate=' num2str(r) ])
xlabel(['Time'])