TWODIM.M 1/17/2005
Let's make a two-dimensional data set to illustrate EOF analysis
Run this over and over again with the graphic window open and see how little the eigenvectors move, even though the data are jumping all over the place. Increase and reduce the sample size.
Vary the parameter c1 [ 1 -1 2 ] to change the slope of the cloud of points.
clear all
echo on
loop on time
tap space bar to go to next set of data
echo off
for ii=1:10
Set the sample size to num
num=120;
set the parameter controlling the amplitude of the first variablea1=2.0;
set the parameter controlling the noise of the second variablea2=2.5;
set the parameter scaling the second variable on the firstc1=0.5;
Make data setfor i=1:num
First number is Gaussian noise with an amplitude of a1x(1,i)=randn(1)*a1;
Second number is the first times a slope c1,
plus more noise of amplitude a2x(2,i)=c1*x(1,i)+randn(1)*a2;end
Remove mean
xm=x;
for i=1,2
xm(1,:)=x(1,:)-mean(x(1,:));
xm(2,:)=x(2,:)-mean(x(1,:));
end
Compute covariance matrixc=xm*xm'/num;
do SVD of covariance matrix[u s v]=svd(c)
make a scatter diagram of the data
scatter(xm(1,1:num),xm(2,1:num),10)
set(get(1,'CurrentAxes'),'FontSize',14)
xlabel('X1','Fontsize',14)
ylabel('X2','Fontsize',14)
Write the eigenvectors and eigenvalues on the plot
es=num2str(u,'%8.4g')
xtitle=strcat('e1=',es(1,1),es(2,1),',e2=',es(1,2),es(2,2))
xtitle=strcat(es);
ss=num2str(s,'%8.2g')
ytitle=strcat(ss);title(xtitle)
h=text(-8,8,xtitle)
set(h,'fontsize',14)
h2=text(-8,6,ytitle)
set(h2,'fontsize',14)
Plot the eigenvectors in red on this plot
Make them 10 units long instead of 1, so you can see them better
u=u*10;
xx(1,1)=0.0;
xx(2,1)=u(1,1);
xx(1,2)=0.0;
xx(2,2)=u(1,2);
yy(1,1)=0.0;
yy(2,1)=u(2,1);
yy(1,2)=0.0;
yy(2,2)=u(2,2);
line(xx,yy,'color','red')
set the axis limits
axis([-10 10 -10 10])
make the plot square, so the eigenvectors look orthogonalset (gca,'DataAspectRatio',[1 1 1])
echo on
press space bar to continue
echo off
pause
clear all
end
echo on
FINISHED!


