clc
clf
%k = @(x,y) 1*x'*y;
k= @(x,y) exp(-((x-y)'*(x-y))/(2.*0.1*0.1));

% sample points
x= (0:.001:1);
n = length(x);

% construct covariance matrix
C = zeros(n,n);
for i=1:n
    for j=1:n
        C(i,j)= k(x(i),x(j));
    end
end

% sample from GP at these points
u = randn(n,1);

[A, S, B]= svd(C);
z = A*sqrt(S)*u;  % z = A S^0.5
% [A,p]= chol(C+1.0e-06*eye(size(C)),'lower');
% z = A*u;

figure(2); hold on;
plot(x,z,'.-')
axis([0, 1, -3, 3])

for i=2:10
    u = randn(n,1);
    [A, S, B]= svd(C);
    z = A*sqrt(S)*u;  % z = A S^0.5 u
%     [A,p]= chol(C+1.0e-06*eye(size(C)),'lower');
%     z = A*u;
    plot(x,z,'.-')
end

plot(x(49),z(49),'s','MarkerFaceColor','r', 'MarkerSize', 10);
