maxSize=min(6,size(trainingData,2));

r2Autoenc=zeros([maxSize,size(trainingData,2)]);
for i=1:maxSize
    ae=trainAutoencoder(trainingData,i,'ShowProgressWindow',false);
    predicted=predict(ae,trainingData);
    r2Autoenc(i,:)=correlation2(trainingData,predicted);
end

fig=figure();
subplot(1,3,1);
plot(r2Autoenc)
title('Autoencoder')
xlabel('number of syergies')
ylabel('R^2')

%PCA
[COEFF,SCORE,latent] = pca(trainingData,'Centered',false);
r2PCA=zeros([maxSize,size(trainingData,2)]);
for i=1:maxSize
    predicted=SCORE(:,1:i)*COEFF(:,1:i)';
    r2PCA(i,:)=correlation2(trainingData,predicted);   
end
subplot(1,3,2);
plot(r2PCA)
title('PCA')
xlabel('number of syergies')
ylabel('R^2')

%NNMF
r2NNMF=zeros([maxSize,size(trainingData,2)]);
for i=1:maxSize
    [W,H]=nnmf(trainingData,i);
    predicted=W*H;
    r2NNMF(i,:)=correlation2(trainingData,predicted);
end

subplot(1,3,3);
plot(r2NNMF)
title('NNMF')
xlabel('number of syergies')
ylabel('R^2')