%exercise (2b), ex sheet 2

N = 100;
Delta_t = 1/N; %time step
max = 100; %no. of basis functions
M = 1000; %no. of samples
x = zeros(M,N+1);
xi = randn(M,max); % iid gaussian rv's
T = 0:Delta_t:1;
Variance = zeros(1,N+1); %Variance as a function of time

for i=1:(N+1)
    for j=1:M
        for k=1:max
            x(j,i) = x(j,i) + sqrt(2)*xi(j,k)*sin(k*pi*T(i))/(k*pi);
        end;
    end;
    Variance(i) = var(x(1:M,i));
end;

%Plot a trajectory

figure;
plot(T,x(1,1:(N+1)));

%Plot Covariance

figure;
%plot(T,x(1,1:(N+1)).^2);
hold on;
plot(T, T-T.^2,'r');
plot(T,Variance,'-s') 
hold off;