clear; set(0,'defaultaxesfontsize',14); format long
%%% linear system in 2D, with and without model noise
%%% based on Ex 2.2 in: Law, Stuart, Zygalakis (2015), "Data Assimilation"

N=50% number of steps
A=[0 1; -1 0];% dynamics determined by A
sigma=0.25;% model noise variance is sigma^2
sd=1; rng(sd);% choose random number seed; try different seeds, e.g. 0,1,2,3,...
x=zeros(N+1,2); xnoise=zeros(N+1,2);% preallocate space
x(1,:)=[1 1]; xnoise(1,:)=x(1,:);% initial conditions

for i=1:N
    x(i+1,:)=x(i,:)*A';
    xnoise(i+1,:)=xnoise(i,:)*A'+sigma.*randn(1,2);
end

figure(1), plot(x(:,1),x(:,2)), xlim([-1.5 1.5]), ylim([-1.5 1.5]); title('w/o noise')
figure(2), plot(xnoise(:,1),xnoise(:,2)); title('with noise')



