%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Ito Formula Comparision
%
%  This code simulates 1 trials of standard Brownian motion over
%  the interval [0,T]. The code then plots and compares the difference
%  between B_t^3 and its equivalent integral representation
%
%  Inputs:
%  1. dt= discretization size
%
%  Ouputs:
%   1. Plot of the comparisons between realizations
%   2. Plot of the difference between the left and right side of Ito
%   formula.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Inputs
dt=.001;

t=[0:dt:1];
[~,n]=size(t);

B=zeros(1,n);

%% Initilizing Plot
figure('units','normalized','outerposition',[0 0 1 1]);
hold on;
xlabel('$$t$$','interpreter','latex')
ylabel('$$B_t^3$$','interpreter','latex')
ax=gca;
ax.FontSize = 40;

%% Construction of Realization of Brownian Motion
for i=1:n-1
        B(i+1)=B(i)+sqrt(dt)*randn;
end

%% Computing Integral of Righthand Side
integral=zeros(1,n);
for i=1:n-1
    integral(i+1)=3*B(i)^2*(B(i+1)-B(i))+3*B(i)*dt+integral(i);
end

%% Comparing the Results

hold on;
plot(t,B.^3,'linewidth',2);
plot(t,integral,'linewidth',2);

figure('units','normalized','outerposition',[0 0 1 1]);
hold on;
xlabel('$$t$$','interpreter','latex')
ylabel('Difference','interpreter','latex')
ax=gca;
ax.FontSize = 40;

plot(t,abs(B.^3-integral),'linewidth',2);





