function linear_diffusion08sims
% Math 586 Computational Finance Code, Prof. F. B. Hanson, 01/15/2008
% Linear Diffusion SDE Simulation in MATLAB for stock price S(t)
%   with variable coefficients for time t in [0,T] and sample variation:
%        dS(t) = S(t)*(mu(t)*dt + sigma(t)*dW(t)), S(0) = S0.
% OR for computation, use
%        S(t+dt) = S(t)*(1 + mu(t)*dt + sigma(t)*dW(t)), S(0) = S0.
%   Generation is by summing Wiener increments dW of even spacing dt.
%
clc % clear variables, but must come before globals, 
    %    else clears globals too.
clf % clear figures
%
fprintf('\nlinear_diffusion08sims OutPut:');
%%% Initialize input:
N = 1000; T = 1.0; % Set initial time grid: Fixed Delta{t}.
S0 = 100.0; % Initial stock price.
mu = 0.20*ones(1,N+1); sigma = 0.15*ones(1,N+1); % Subfunctions given data?
dt = T/N; % Input Constants once: assign variable V=C first time and V later.
t = 0:dt:T; % Loop-construct for (1X(N+1))-row-vector; t(1,1) = 0;
% Caution: MATLAB requires positive matrix indicies.
randn('state',5); % Set random state for reproducibility.
dW = sqrt(dt)*randn(1,N); % Generate diffusion step (1XN)-vector.
W = zeros(1,N+1); % Predeclare size for efficient MATLAB loops, W(1,1) = 0;
S = zeros(1,N+1); % Predeclare size for efficient MATLAB loops, S(1,1) = 0;
Sdet = zeros(1,N+1); % Predeclare size for efficient MATLAB loops, S(1,1) = 0;
S(1,1) = S0; Sdet(1,1) = S0; % Correct Initial price;
% 
for i = 1:N
    W(1,i+1) = W(1,i) + 10*dW(1,i); % Get scaled new diffusion value to view.
    S(1,i+1) = S(1,i)*(1+mu(1,i)*dt+sigma(1,i)*dW(1,i)); % Get new price
    Sdet(1,i+1) = Sdet(1,i)*(1+mu(1,i)*dt); % Get new deterministic price
end
% END computation, BEGIN plotting:
nfig = 1;
figure(nfig);
scrsize = get(0,'ScreenSize'); % Optiional figure spacing for target screen.
ss = [5.0,4.0,3.5]; % figure spacing factors
%
plot(t,S,'g-',t,Sdet,'k--',t,W,'b--','LineWidth',2);
title('Stock Price for Linear Diffusion','FontWeight','Bold','Fontsize',44);
ylabel('S(t), Sdet(t) and 10*W(t)','FontWeight','Bold','Fontsize',44);
xlabel('t, Time','FontWeight','Bold','Fontsize',44);
hlegend=legend('S(t), stock price','Sdet(t), det. stock price'...
    ,'10*W(t), diffusion noise'...
    ,'Location','NorthWest');
set(hlegend,'Fontsize',32,'FontWeight','Bold');
set(gca,'Fontsize',36,'FontWeight','Bold','linewidth',3);
set(gcf,'Color','White','Position' ...
   ,[scrsize(3)/ss(nfig) 60 scrsize(3)*0.60 scrsize(4)*0.80]); %[l,b,w,h]
%
% End linear_diffusion08sims Code
