%2d sde
%Euler-Murayama method

x1=0;x2=0; %initial condition
T=1; %time limit


%T=1; x1=0;x2=0;
%ylim([-1,1]);
xlim([0,T]);

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

%clf reset;
%axis equal;
Bmt = cumsum(sqrt(dt)*randn(2,n));

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


%plot(t,Bmt(1:n),'Color',[0 0 1]);
hold on;
   
%plot(t,0.5*(Bmt.^2-t),'Color',[0 1 0]); %check against explicit solition

x=zeros(2,n);
x(1,1)=x1; x(2,1)=x2; %set the initial cond.

for (j=2:1:n)
    x(:,j)=x(:,j-1)+fd(x(:,j-1))*dt+ff(x(:,j-1)).*(Bmt(:,j)-Bmt(:,j-1));
%     x(1,j)=x(1,j-1)+dt;
%     x(2,j)=x(2,j-1)+0.05*(2-x(2,j-1))*dt+0.1*(Bmt(2,j)-Bmt(2,j-1));
%     x(2,j)=x(2,j-1)-(x(2,j-1)/(1-x(1,j-1)))*dt+1*(Bmt(2,j)-Bmt(2,j-1));
   
end
    
plot(x(1,:), x(2,:), 'Color',[1 0 0]);
hold on;

    
title('SDE');
xlim([0,T]);
%ylim([-4,4]);
legend('B.m.','solution to SDE','Location', 'northeastoutside' );


% xlabel('t');
% ylabel('X_t');

%axis equal;

hold off;

  