%convergence to SDE simulation
T=5; %final time
x0=1; %initial state
noite=20; %number of iterations


dt=0.0001;
t=[0:dt:T];
n=size(t);
n=n(2);

clf reset;
 
Bmt = cumsum(sqrt(dt)*randn(1,n));
plot(t,Bmt(1:n),'Color',[0 0 0]);
hold on;
  

%x0=x2;
inand=ones(1,n)*x0; %initial integrand
xlim([0,T]);
ylim([-3,3]);
plot(t,inand,'Color',[0.5*(1-1/(noite+1)) 1-1/(noite+1) 1]); 
 

for (i=1:noite)
    pause(0.2);
    inand1=inand;
    inand1(1)=x0;
    for (j=2:n)
       time=j*dt;
       dr=fd([time,inand(j-1)]); dr=dr(2);
       fl=ff([time,inand(j-1)]); fl=fl(2);       
       inand1(j)=inand1(j-1)+dr*dt+fl*(Bmt(:,j)-Bmt(:,j-1));
    end
    inand=inand1; %new integrand
    if (i<=10) waitforbuttonpress; end
    plot(t,inand,'Color',[0.5*(1-(i+1)/(noite+1)) 1-(i+1)/(noite+1) 1]); 
 
   
end
    
legend('B.m.','Picard iterations','Location', 'northeast' );
   
title('Convergence to the strong solution of SDE');

  