clear;

T = 500;        
lambda = -1.1; 
mu = 1;            

nTrajectories= 100;
 
DeltaT = 2;                           
N = T/DeltaT ;
Xemabs = zeros(1,N);
X12abs = zeros(1,N);
X1abs = zeros(1,N);
X2 = ones(1,N);
XEM = ones(nTrajectories,1); 
X_12= ones(nTrajectories,1); 
X_1 = ones(nTrajectories,1); 
X = ones(nTrajectories,1); 

for j = 1:N
       Winc = sqrt(DeltaT)*randn(nTrajectories,1);       
       XEM = (1+DeltaT*lambda + mu*Winc).*XEM;
       X_12 = ((1-DeltaT*lambda*0.5)^(-1))*(1 + DeltaT*lambda*0.5 + mu*Winc).*X_12;
       X_1 = ((1-DeltaT*lambda)^(-1))*(1 + mu*Winc).*X_1;
       X2(j) = exp((2*lambda+mu^2)*j*DeltaT);
       Xemabs(j) = mean(XEM.^2);   
       X12abs(j) = mean(X_12.^2);
       X1abs(j) = mean(X_1.^2);
end

figure;
semilogy([0:DeltaT:T],[1,X2],'Linewidth',2), 
hold on
semilogy([0:DeltaT:T],[1,Xemabs],'Linewidth',2), 
hold on
semilogy([0:DeltaT:T],[1,X12abs],'Linewidth',2), 
hold on
semilogy([0:DeltaT:T],[1,X1abs],'Linewidth',2), 
hold on
xlabel('T', 'Fontsize', 20);
ylabel('E[X_n^2]', 'interpreter','tex','Fontsize', 20);
legend('Sol.','\theta = 0','\theta = 1/2','\theta = 1')
set(gca, 'FontSize', 20);
saveas(gcf, 'figures/ns_02.png');
