function [t,B]=brownian(B0,I,M)
% Brownian motion
% 
%   Syntax: 
%           [t,B]=brownian(B0,I,M)
%   Arguments: 
%           B0 - an nx1 matrix, the initial value 
%           I - an 1x2 matrix, the interval, I=[a,b]
%           M - an integer, the number of increments 
%   Returns: 
%           t - an (M+1)x1 matrix, the time points
%           B - an (M+1)xn matrix, a sample path, B(t)
%   Description:
%           The program computes Brownian motion B(t), a<t<b, with
%           B(a)=B0.  
%             
%   Examples: 
%          [t,B]=brownian([1;0],[0,1],1000); plot(t,B)
%          

%----------------------------------------------------------------------
%   Date created: 2001-02-26
%
%   Author:  Stig Larsson
%------------------------------+---------------------------------------+
if size(B0,2)~=1 
    error('B0 must be a column vector')
end
a=I(1); b=I(2);
k=(b-a)/M;
t=a:k:b; t=t';
sigma=sqrt(k);
increment=[B0'; random('Normal',0,sigma,M,length(B0))];
B=cumsum(increment);


