├── CH01 ├── CH01_SEC02.m ├── CH01_SEC02_production.m ├── CH01_SEC03_Rotation.m ├── CH01_SEC03_Rotation_production.m ├── CH01_SEC04_1_Linear.m ├── CH01_SEC04_2_Cement.m ├── CH01_SEC04_3_Housing.m ├── CH01_SEC05_1_PCAGaussian.m ├── CH01_SEC05_2_OvarianCancer.m ├── CH01_SEC06_1.m ├── CH01_SEC06_2_3_4.m ├── CH01_SEC06_2_3_4_production.m ├── CH01_SEC07_1_Truncation.m ├── CH01_SEC07_1_production.m ├── CH01_SEC07_2_Truncation.m ├── CH01_SEC07_2_production.m ├── CH01_SEC07_3_Alignment.m ├── CH01_SEC07_3_production.m ├── CH01_SEC08_RSVD.m ├── CH01_SEC09_Tensor.m ├── dog.jpg ├── optimal_SVHT_coef.m └── rsvd.m ├── CH02 ├── CH02_SEC01_0_InnerProduct.m ├── CH02_SEC01_1_FourierSines.m ├── CH02_SEC01_1_FourierSines_production.m ├── CH02_SEC01_2_Gibbs.m ├── CH02_SEC01_2_Gibbs_Movie.m ├── CH02_SEC01_2_Gibbs_production.m ├── CH02_SEC02_1_DFT.m ├── CH02_SEC02_1_DFT_production.m ├── CH02_SEC02_2_Denoise.m ├── CH02_SEC02_2_Denoise_production.m ├── CH02_SEC02_3_SpectralDerivative.m ├── CH02_SEC03_1_FFTHeat.m ├── CH02_SEC03_1_FFTHeat_production.m ├── CH02_SEC03_2_FFTWave.m ├── CH02_SEC03_3_FFTBurgers.m ├── CH02_SEC04_1_SpectrogramChirp.m ├── CH02_SEC04_2_SpectrogramBeethoven │ ├── CC.mat │ └── CH02_SEC04_2_SpectrogramBeethoven.m ├── CH02_SEC05_HAAR.m ├── CH02_SEC06_1_2DFFT.m ├── CH02_SEC06_1_2DFFT_production.m ├── CH02_SEC06_2_Compress.m ├── CH02_SEC06_2_Compress_production.m ├── CH02_SEC06_3_Denoise.m ├── CH02_SEC06_3_Denoise_production.m ├── CH02_SEC06_4_Wavelet.m ├── CH02_SEC06_4_Wavelet_production.m ├── CH02_SEC06_5_WaveletCompress.m ├── CH02_SEC06_5_WaveletCompress_production.m ├── rhsBurgers.m ├── rhsHeat.m ├── rhsWave.m └── rhsWaveSpatial.m ├── CH03 ├── CC2.mat ├── CH03_SEC01_Compress.m ├── CH03_SEC03_1_Underdetermined.m ├── CH03_SEC03_2_AudioCS.m ├── CH03_SEC04_Matrices.m ├── CH03_SEC05_1_RobustRegression.m ├── CH03_SEC05_1_RobustRegression_production.m ├── CH03_SEC05_2_LASSO.m ├── CH03_SEC06_SparseRepresentation.m ├── CH03_SEC06_SparseRepresentation_production.m ├── CH03_SEC07_RPCA.m ├── QRsensors.m ├── RPCA.m ├── SVT.m ├── jelly.jpg ├── mustache.jpg └── shrink.m ├── CH04 ├── CH04_SEC01_LinearRegression.m ├── CH04_SEC02_1_GradientDescent.m ├── CH04_SEC02_2_DeltaSearch.m ├── CH04_SEC03_1_OverUnderDetermined.m ├── CH04_SEC03_1_OverUnderDetermined_production.m ├── CH04_SEC04_1_CompareRegression.m ├── CH04_SEC04_1_CompareRegression_production.m ├── CH04_SEC05_0_Fig4p16_Pareto.m ├── CH04_SEC05_1_CrossValidate.m ├── CH04_SEC06_1_kFoldValidation.m ├── CH04_SEC06_1_kFoldValidation_production.m ├── CH04_SEC07_1_ModelValidation.m ├── CH04_SEC07_2_RegressAIC_BIC.m ├── fit1.m ├── fit2.m └── fit3.m ├── CH05 ├── AllMethodsCatsDogs.m ├── AllMethodsCatsDogs2012.m ├── CH05_SEC01_1_FischerExtraction.m ├── CH05_SEC01_1_FischerExtraction_production.m ├── CH05_SEC02_1_Fig5p7_Fig5p8.m ├── CH05_SEC03_1_Kmeans.m ├── CH05_SEC03_1_Kmeans_production.m ├── CH05_SEC04_1_Dendrogram.m ├── CH05_SEC04_1_Dendrogram_production.m ├── CH05_SEC05_1_GaussianMixtureModels.m ├── CH05_SEC06_1_LDA_Classify.m ├── CH05_SEC07_1_SVM.m ├── CH05_SEC08_1_Trees.m ├── CH05_SEC08_1_Trees_production.m ├── catData.mat ├── catData_w.mat ├── dc_wavelet.m ├── dogData.mat └── dogData_w.mat ├── CH06 ├── CH06_SEC01_1_NN.m ├── CH06_SEC01_1_NN_production.m ├── CH06_SEC02_1_NN.m ├── CH06_SEC04_1_StochasticGradientDescent.m ├── CH06_SEC05_1_DeepCNN.m ├── CH06_SEC06_1_NNLorenz.m ├── NLactivation.m ├── catData_w.mat ├── dogData_w.mat ├── extra │ ├── am582_test2.m │ ├── burgers.mat │ └── ml_dcnn.m ├── lrz_rhs.m └── tausearch.m ├── CH07 ├── CH07_SEC01_SimulateLogistic.m ├── CH07_SEC01_SimulateLorenz.m ├── CH07_SEC02_DMD_Cylinder.m ├── CH07_SEC03_SINDY_Lorenz.m ├── CH07_SEC03_SINDY_Lorenz_production.m ├── CH07_SEC04_Koopman.m ├── CH07_SEC05_HAVOK_Lorenz.m ├── DMD.m ├── LorenzMovie │ ├── MOVIE_LORENZ.m │ ├── MOVIE_LORENZ_BW.m │ ├── QuiverLorenz.m │ └── lorenz3D.m ├── logistic.m ├── lorenz.m ├── plotCylinder.m ├── poolData.m ├── poolDataLIST.m └── sparsifyDynamics.m ├── CH08 ├── CH08_SEC01_CruiseControl.m ├── CH08_SEC07_0_Fig8p12.m ├── CH08_SEC07_1_LQR.m ├── CH08_SEC07_2_KalmanFilter.m ├── CH08_SEC07_2b_Obsv.m ├── CH08_SEC07_3_LQGPlot.m ├── CH08_SEC07_3_LQG_Simulink │ ├── Cart_Pendulum.c │ ├── Cart_Pendulum.tlc │ ├── Cart_Pendulum_wrapper.c │ ├── SFB__Cart_Pendulum__SFB.mat │ ├── SFB__cartpend2__SFB.mat │ ├── SFB__cartpend_sim__SFB.mat │ ├── cartpend_sim.c │ ├── cartpend_sim.mexmaci64 │ ├── cartpend_sim.tlc │ ├── cartpend_sim_wrapper.c │ ├── lqg_cartpend_sim.pdf │ ├── lqg_cartpend_sim.slx │ ├── lqg_cartpend_sim.slx.r2015b │ ├── lqg_simulinkINIT.m │ ├── lqg_simulinkINITBOOK.m │ ├── lqr_cartpend_sim.slx │ └── rtwmakecfg.m ├── CH08_SEC08_1_TransferFunction.m ├── CH08_SEC08_2_SandT.m ├── CH08_SEC08_3_PlantInversion.m ├── drawpend.m ├── drawpend_FIG.m ├── pendcart.m └── simpend.m ├── CH09 ├── BPOD.m ├── CH09_SEC02_1_GramianPlot.m ├── CH09_SEC02_2_BalancedTruncation.m ├── CH09_SEC03_ERA_OKID.m ├── CH09_SEC03_Fig9p5.m ├── CH09_z_extra_BT_ERA_OKID.m ├── ERA.m ├── OKID.m ├── testSys.mat └── testSys_Fig9p5.mat ├── CH10 ├── CH10_SEC02_GA_PID │ ├── CH10_SEC02_Fig10p6.m │ ├── CH10_SEC02_GA_PID.m │ ├── CH10_SEC02_GA_PID_production.m │ ├── history.mat │ ├── myfun.m │ ├── pidtest.m │ ├── randpop.mat │ └── testLQR.m ├── CH10_SEC03_ESC_Krstic_ex1p3 │ ├── NEWkrstic_example_1p3_wnoise.slx │ ├── krstic_1p3.m │ ├── krstic_example_1p3.mdl │ ├── krstic_example_1p3_wnoise.mdl │ └── plotresponse.m ├── CH10_SEC03_ESCfixed.m └── CH10_SEC03_ESCsinusoidal.m ├── CH11 ├── CH11_SEC01_1_Fig11p1.m ├── CH11_SEC02_1_HarmonicOscillator.m ├── CH11_SEC02_2_Fig11p2_Fig11p3.m ├── CH11_SEC03_1_NonlinearSchrodinger.m ├── CH11_SEC03_2_Fig11p4_Fig11p5.m ├── CH11_SEC05_1_Invariance.m ├── CH11_SEC05_1_Invariance_production.m ├── extra │ ├── POD_invariance2.m │ ├── ch_ROM_rom_nls.m │ ├── ch_ROM_rom_nls2.m │ ├── ch_rom_a_rhs.m │ ├── ch_rom_deim.m │ ├── ch_rom_deim2.m │ └── ch_rom_deim_rhs.m ├── harm_rhs.m └── nls_rhs.m ├── CH12 ├── CH12_SEC01_1_GAPPY.m ├── CH12_SEC02_1_GAPPY.m ├── CH12_SEC03_1_GAPPY_ConditionNumber.m ├── CH12_SEC04_1_GAPPY_Variance.m ├── CH12_SEC04_1_GAPPY_Variance_production.m ├── CH12_SEC06_1_DEIM.m ├── CH12_SEC06_1_DEIM_production.m ├── CH12_SEC06_2_DEIM.m ├── nls_rhs.m ├── old_extra │ ├── POD_invariance.m │ ├── POD_invariance2.m │ ├── ch_ROM_rom_nls.m │ ├── ch_ROM_rom_nls2.m │ ├── ch_pod.m │ ├── ch_pod_harm (josenathankutz's conflicted copy 2014-09-08).m │ ├── ch_pod_harm.m │ ├── ch_pod_harm_rhs.m │ ├── ch_pod_quad.m │ ├── ch_pod_sol.m │ ├── ch_rom_a_rhs.m │ └── deimPLOT │ │ ├── ccmat.mat │ │ ├── data.mat │ │ ├── dataexam.m │ │ ├── deim.m │ │ ├── gappy.m │ │ ├── gappy2.m │ │ ├── gappy3.m │ │ ├── gappy3_old.m │ │ ├── gappy4.m │ │ ├── gappy_karni.m │ │ ├── gappy_karni_BOOK.m │ │ ├── gappy_karni_v2.m │ │ ├── gappy_willcox.m │ │ ├── plotMat.m │ │ ├── psi_L.mat │ │ └── rhs.m └── rom_deim_rhs.m ├── README.md └── UTILS ├── CC2.mat ├── SVT.m ├── colors2.mat ├── coolcolors.mat ├── coolcolorsBW.mat ├── cosamp.m ├── istft ├── Resynthesis.m ├── WindowChoice.m ├── istft.m ├── license.txt ├── stft.m └── track.wav ├── optimal_SVHT_coef.m ├── pad.m ├── padflip.m └── shrink.m /CH01/CH01_SEC02.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | A=imread('../DATA/dog.jpg'); 4 | X=double(rgb2gray(A)); % Convert RBG->gray, 256 bit->double. 5 | nx = size(X,1); ny = size(X,2); 6 | imagesc(X), axis off, colormap gray 7 | 8 | [U,S,V] = svd(X); 9 | 10 | for r=[5 20 100]; % Truncation value 11 | Xapprox = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; % Approx. image 12 | figure, imagesc(Xapprox), axis off 13 | title(['r=',num2str(r,'%d'),']); 14 | end 15 | 16 | %% f_ch01_ex02_2 17 | subplot(1,2,1), semilogy(diag(S),'k') 18 | subplot(1,2,2), plot(cumsum(diag(S))/sum(diag(S)),'k') -------------------------------------------------------------------------------- /CH01/CH01_SEC02_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | A=imread('../DATA/dog.jpg'); 4 | X=double(rgb2gray(A)); % Convert RBG to gray, 256 bit to double. 5 | nx = size(X,1); ny = size(X,2); 6 | 7 | [U,S,V] = svd(X); 8 | 9 | figure, subplot(2,2,1) 10 | imagesc(X), axis off, colormap gray 11 | title('Original') 12 | 13 | plotind = 2; 14 | for r=[5 20 100]; % Truncation value 15 | Xapprox = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; % Approx. image 16 | subplot(2,2,plotind), plotind = plotind + 1; 17 | imagesc(Xapprox), axis off 18 | title(['r=',num2str(r,'%d'),', ',num2str(100*r*(nx+ny)/(nx*ny),'%2.2f'),'% storage']); 19 | end 20 | set(gcf,'Position',[100 100 550 400]) 21 | 22 | %% f_ch01_ex02_2 23 | figure, subplot(1,2,1) 24 | semilogy(diag(S),'k','LineWidth',1.2), grid on 25 | xlabel('r') 26 | ylabel('Singular value, \sigma_r') 27 | xlim([-50 1550]) 28 | subplot(1,2,2) 29 | plot(cumsum(diag(S))/sum(diag(S)),'k','LineWidth',1.2), grid on 30 | xlabel('r') 31 | ylabel('Cumulative Energy') 32 | xlim([-50 1550]); ylim([0 1.1]) 33 | set(gcf,'Position',[100 100 550 240]) -------------------------------------------------------------------------------- /CH01/CH01_SEC03_Rotation.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | theta = [pi/15; -pi/9; -pi/20]; 3 | Sigma = diag([3; 1; 0.5]); % scale x, then y, then z 4 | 5 | Rx = [1 0 0; % rotate about x-axis 6 | 0 cos(theta(1)) -sin(theta(1)); 7 | 0 sin(theta(1)) cos(theta(1))]; 8 | 9 | Ry = [cos(theta(2)) 0 sin(theta(2)); % rotate about y-axis 10 | 0 1 0; 11 | -sin(theta(2)) 0 cos(theta(2))]; 12 | 13 | Rz = [cos(theta(3)) -sin(theta(3)) 0; % rotate about z-axis 14 | sin(theta(3)) cos(theta(3)) 0; 15 | 0 0 1]; 16 | 17 | X = Rz*Ry*Rx*Sigma; % rotate and scale 18 | 19 | %% Plot sphere 20 | subplot(1,2,1), hold on 21 | [x,y,z] = sphere(25); 22 | h1=surf(x,y,z); 23 | set(h1,'FaceAlpha',.7) 24 | colormap jet, lighting phong, axis equal 25 | axis([-2 2 -2 2 -2 2]), view([45 26]) 26 | 27 | %% 28 | xR = 0*x; yR = 0*y; zR = 0*z; 29 | 30 | for i=1:size(x,1) 31 | for j=1:size(x,2) 32 | vec = [x(i,j); y(i,j); z(i,j)]; 33 | vecR = X*vec; 34 | xR(i,j) = vecR(1); 35 | yR(i,j) = vecR(2); 36 | zR(i,j) = vecR(3); 37 | end 38 | end 39 | 40 | subplot(1,2,2), hold on 41 | h2=surf(xR,yR,zR,z); % using the z-position of sphere for color 42 | set(h2,'FaceAlpha',.7) 43 | colormap jet, lighting phong, axis equal 44 | axis([-2 2 -2 2 -2 2]), view([45 26]) 45 | set(gcf,'Position',[100 100 800 350]) -------------------------------------------------------------------------------- /CH01/CH01_SEC04_1_Linear.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | x = 3; % True slope 4 | a = [-2:.25:2]'; 5 | b = a*x + 1*randn(size(a)); % Add noise 6 | plot(a,x*a,'k','LineWidth',1.5) % True relationship 7 | hold on 8 | plot(a,b,'rx','LineWidth',1.5) % Noisy measurements 9 | 10 | [U,S,V] = svd(a,'econ'); 11 | xtilde = V*inv(S)*U'*b; % Least-square fit 12 | 13 | plot(a,xtilde*a,'b--','LineWidth',1.5) % Plot fit 14 | l1=legend('True line','Noisy data','Regression line'); 15 | 16 | set(l1,'String',{' ','',''}) 17 | set(l1,'Location','NorthWest') 18 | grid on 19 | set(gcf,'Position',[100 100 300 300]) 20 | set(gcf,'PaperPositionMode','auto') 21 | set(gca,'FontSize',13) 22 | print('-depsc2', '-loose', 'figures/CH01_EX_PseudoInv') 23 | 24 | %% Three methods of computing regression 25 | xtilde1 = V*inv(S)*U'*b 26 | xtilde2 = pinv(a)*b 27 | xtilde3 = regress(b,a) 28 | 29 | 30 | -------------------------------------------------------------------------------- /CH01/CH01_SEC04_2_Cement.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load hald; % Load Portlant Cement dataset 4 | A = ingredients; 5 | b = heat; 6 | 7 | [U,S,V] = svd(A,'econ'); 8 | x = V*inv(S)*U'*b; % Solve Ax=b using the SVD 9 | 10 | plot(b,'k','LineWidth',2); hold on % Plot data 11 | plot(A*x,'r-o','LineWidth',1.,'MarkerSize',2); % Plot regression 12 | l1 = legend('Heat data','Regression') 13 | 14 | %% Alternative 1 (regress) 15 | x = regress(b,A); 16 | 17 | %% Alternative 2 (pinv) 18 | x = pinv(A)*b; -------------------------------------------------------------------------------- /CH01/CH01_SEC04_3_Housing.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load housing.data 4 | 5 | b = housing(:,14); % housing values in $1000s 6 | A = housing(:,1:13); % other factors, 7 | A = [A ones(size(A,1),1)]; % Pad with ones for nonzero offset 8 | 9 | x = regress(b,A); 10 | 11 | subplot(1,2,1) 12 | plot(b,'k-o'); 13 | hold on, grid on 14 | plot(A*x,'r-o'); 15 | set(gca,'FontSize',13) 16 | xlim([0 size(A,1)]) 17 | 18 | subplot(1,2,2) 19 | [b sortind] = sort(housing(:,14)); % sorted values 20 | plot(b,'k-o') 21 | hold on, grid on 22 | plot(A(sortind,:)*x,'r-o') 23 | l1=legend('Housing value','Regression') 24 | set(l1,'Location','NorthWest') 25 | set(gca,'FontSize',13) 26 | xlim([0 size(A,1)]) 27 | 28 | set(gcf,'Position',[100 100 600 250]) 29 | 30 | 31 | %% 32 | A2 = A-ones(size(A,1),1)*mean(A,1); 33 | for i=1:size(A,2)-1 34 | A2std = std(A2(:,i)); 35 | A2(:,i) = A2(:,i)/A2std; 36 | end 37 | A2(:,end) = ones(size(A,1),1); 38 | 39 | x = regress(b,A2) 40 | figure 41 | bar(x(1:13)) 42 | 43 | xlabel('Attribute'), ylabel('Correlation') 44 | set(gca,'FontSize',13) 45 | set(gcf,'Position',[100 100 600 250]) -------------------------------------------------------------------------------- /CH01/CH01_SEC05_1_PCAGaussian.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | xC = [2; 1;]; % Center of data (mean) 4 | sig = [2; .5;]; % Principal axes 5 | 6 | theta = pi/3; % Rotate cloud by pi/3 7 | R = [cos(theta) -sin(theta); % Rotation matrix 8 | sin(theta) cos(theta)]; 9 | 10 | nPoints = 10000; % Create 10,000 points 11 | X = R*diag(sig)*randn(2,nPoints) + diag(xC)*ones(2,nPoints); 12 | 13 | subplot(1,2,1) % Plot cloud of noisy data 14 | scatter(X(1,:),X(2,:),'k.','LineWidth',2) 15 | hold on, box on, grid on 16 | axis([-6 8 -6 8]) 17 | 18 | %% f_ch01_ex03_1b 19 | 20 | Xavg = mean(X,2); % Compute mean 21 | B = X - Xavg*ones(1,nPoints); % Mean-subtracted Data 22 | [U,S,V] = svd(B/sqrt(nPoints),'econ'); % Find principal components (SVD) 23 | 24 | subplot(1,2,2) 25 | scatter(X(1,:),X(2,:),'k.','LineWidth',2) % Plot data to overlay PCA 26 | hold on, box on, grid on 27 | axis([-6 8 -6 8]) 28 | 29 | theta = (0:.01:1)*2*pi; 30 | [Xstd] = U*S*[cos(theta); sin(theta)]; % 1-std confidence interval 31 | plot(Xavg(1)+Xstd(1,:),Xavg(2) + Xstd(2,:),'r-','LineWidth',1.5) 32 | plot(Xavg(1)+2*Xstd(1,:),Xavg(2) + 2*Xstd(2,:),'r-','LineWidth',1.5) 33 | plot(Xavg(1)+3*Xstd(1,:),Xavg(2) + 3*Xstd(2,:),'r-','LineWidth',1.5) 34 | 35 | % Plot principal components U(:,1)S(1,1) and U(:,2)S(2,2) 36 | plot([Xavg(1) Xavg(1)+U(1,1)*S(1,1)],[Xavg(2) Xavg(2)+U(2,1)*S(1,1)],'c-','LineWidth',2) 37 | plot([Xavg(1) Xavg(1)+U(1,2)*S(2,2)],[Xavg(2) Xavg(2)+U(2,2)*S(2,2)],'c-','LineWidth',2) 38 | 39 | set(gcf,'Position',[100 100 600 300]) -------------------------------------------------------------------------------- /CH01/CH01_SEC05_2_OvarianCancer.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load ovariancancer; % Load ovarian cancer data 4 | 5 | [U,S,V] = svd(obs,'econ'); 6 | 7 | figure 8 | subplot(1,2,1) 9 | semilogy(diag(S),'k-o','LineWidth',1.5) 10 | set(gca,'FontSize',13), axis tight, grid on 11 | subplot(1,2,2) 12 | plot(cumsum(diag(S))./sum(diag(S)),'k-o','LineWidth',1.5) 13 | set(gca,'FontSize',13), axis tight, grid on 14 | set(gcf,'Position',[100 100 600 250]) 15 | 16 | figure, hold on 17 | for i=1:size(obs,1) 18 | x = V(:,1)'*obs(i,:)'; 19 | y = V(:,2)'*obs(i,:)'; 20 | z = V(:,3)'*obs(i,:)'; 21 | if(grp{i}=='Cancer') 22 | plot3(x,y,z,'rx','LineWidth',2); 23 | else 24 | plot3(x,y,z,'bo','LineWidth',2); 25 | end 26 | end 27 | view(85,25), grid on, set(gca,'FontSize',13) -------------------------------------------------------------------------------- /CH01/CH01_SEC06_1.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load ../DATA/allFaces.mat 4 | 5 | allPersons = zeros(n*6,m*6); 6 | count = 1; 7 | for i=1:6 8 | for j=1:6 9 | allPersons(1+(i-1)*n:i*n,1+(j-1)*m:j*m) ... 10 | = reshape(faces(:,1+sum(nfaces(1:count-1))),n,m); 11 | count = count + 1; 12 | end 13 | end 14 | 15 | figure(1), axes('position',[0 0 1 1]), axis off 16 | imagesc(allPersons), colormap gray 17 | 18 | %% 19 | 20 | for person = 1:length(nfaces) 21 | subset = faces(:,1+sum(nfaces(1:person-1)):sum(nfaces(1:person))); 22 | allFaces = zeros(n*8,m*8); 23 | 24 | count = 1; 25 | for i=1:8 26 | for j=1:8 27 | if(count<=nfaces(person)) 28 | allFaces(1+(i-1)*n:i*n,1+(j-1)*m:j*m) ... 29 | = reshape(subset(:,count),n,m); 30 | count = count + 1; 31 | end 32 | end 33 | end 34 | 35 | imagesc(allFaces), colormap gray 36 | end -------------------------------------------------------------------------------- /CH01/CH01_SEC06_2_3_4.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load ../DATA/allFaces.mat 4 | 5 | % We use the first 36 people for training data 6 | trainingFaces = faces(:,1:sum(nfaces(1:36))); 7 | avgFace = mean(trainingFaces,2); % size n*m by 1; 8 | 9 | % Compute eigenfaces on mean-subtracted training data 10 | X = trainingFaces-avgFace*ones(1,size(trainingFaces,2)); 11 | [U,S,V] = svd(X,'econ'); 12 | 13 | imagesc(reshape(avgFace,n,m)) % Plot avg face 14 | imagesc(reshape(U(:,1),n,m)) % Plot first eigenface 15 | 16 | %% Now show eigenface reconstruction of image that was omitted from test set 17 | 18 | testFace = faces(:,1+sum(nfaces(1:36))); % First face of person 37 19 | imagesc(reshape(testFace,n,m)) 20 | 21 | testFaceMS = testFace - avgFace; 22 | for r=[25 50 100 200 400 800 1600] 23 | reconFace = avgFace + (U(:,1:r)*(U(:,1:r)'*testFaceMS)); 24 | imagesc(reshape(reconFace,n,m)) 25 | end 26 | 27 | 28 | %% Project person 2 and 7 onto PC5 and PC6 29 | 30 | P1num = 2; % Person number 2 31 | P2num = 7; % Person number 7 32 | 33 | P1 = faces(:,1+sum(nfaces(1:P1num-1)):sum(nfaces(1:P1num))); 34 | P2 = faces(:,1+sum(nfaces(1:P2num-1)):sum(nfaces(1:P2num))); 35 | 36 | P1 = P1 - avgFace*ones(1,size(P1,2)); 37 | P2 = P2 - avgFace*ones(1,size(P2,2)); 38 | 39 | PCAmodes = [5 6]; % Project onto PCA modes 5 and 6 40 | PCACoordsP1 = U(:,PCAmodes)'*P1; 41 | PCACoordsP2 = U(:,PCAmodes)'*P2; 42 | 43 | plot(PCACoordsP1(1,:),PCACoordsP1(2,:),'kd') 44 | plot(PCACoordsP2(1,:),PCACoordsP2(2,:),'r^') -------------------------------------------------------------------------------- /CH01/CH01_SEC06_2_3_4_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | load ../DATA/allFaces.mat 4 | 5 | % We use the first 36 people for training data 6 | trainingFaces = faces(:,1:sum(nfaces(1:36))); 7 | avgFace = mean(trainingFaces,2); % size n*m by 1; 8 | 9 | % compute eigenfaces on mean-subtracted training data 10 | X = trainingFaces-avgFace*ones(1,size(trainingFaces,2)); 11 | [U,S,V] = svd(X,'econ'); 12 | 13 | figure, axes('position',[0 0 1 1]), axis off 14 | imagesc(reshape(avgFace,n,m)), colormap gray 15 | 16 | for i=1:50 % plot the first 50 eigenfaces 17 | pause(0.1); % wait for 0.1 seconds 18 | imagesc(reshape(U(:,i),n,m)); colormap gray; 19 | end 20 | 21 | 22 | 23 | %% Now show eigenface reconstruction of image that was omitted from test set 24 | 25 | testFace = faces(:,1+sum(nfaces(1:36))); % first face of person 37 26 | axes('position',[0 0 1 1]), axis off 27 | imagesc(reshape(testFace,n,m)), colormap gray 28 | 29 | testFaceMS = testFace - avgFace; 30 | for r=25:25:2275 31 | reconFace = avgFace + (U(:,1:r)*(U(:,1:r)'*testFaceMS)); 32 | imagesc(reshape(reconFace,n,m)), colormap gray 33 | title(['r=',num2str(r,'%d')]); 34 | pause(0.1) 35 | end 36 | 37 | 38 | %% Project person 2 and 7 onto PC5 and PC6 39 | 40 | P1num = 2; % person number 2 41 | P2num = 7; % person number 7 42 | P1 = faces(:,1+sum(nfaces(1:P1num-1)):sum(nfaces(1:P1num))); 43 | P2 = faces(:,1+sum(nfaces(1:P2num-1)):sum(nfaces(1:P2num))); 44 | P1 = P1 - avgFace*ones(1,size(P1,2)); 45 | P2 = P2 - avgFace*ones(1,size(P2,2)); 46 | 47 | figure 48 | subplot(1,2,1), imagesc(reshape(P1(:,1),n,m)); colormap gray, axis off 49 | subplot(1,2,2), imagesc(reshape(P2(:,1),n,m)); colormap gray, axis off 50 | 51 | % project onto PCA modes 5 and 6 52 | PCAmodes = [5 6]; 53 | PCACoordsP1 = U(:,PCAmodes)'*P1; 54 | PCACoordsP2 = U(:,PCAmodes)'*P2; 55 | 56 | figure 57 | plot(PCACoordsP1(1,:),PCACoordsP1(2,:),'kd','MarkerFaceColor','k') 58 | axis([-4000 4000 -4000 4000]), hold on, grid on 59 | plot(PCACoordsP2(1,:),PCACoordsP2(2,:),'r^','MarkerFaceColor','r') 60 | set(gca,'XTick',[0],'YTick',[0]); -------------------------------------------------------------------------------- /CH01/CH01_SEC07_1_Truncation.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | t = (-3:.01:3)'; 4 | 5 | Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)]; 6 | Strue = [2 0; 0 .5]; 7 | Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)]; 8 | 9 | X = Utrue*Strue*Vtrue'; 10 | figure, imshow(X); 11 | 12 | %% 13 | sigma = 1; 14 | Xnoisy = X+sigma*randn(size(X)); 15 | figure, imshow(Xnoisy); 16 | 17 | %% 18 | [U,S,V] = svd(Xnoisy); 19 | 20 | N = size(Xnoisy,1); 21 | cutoff = (4/sqrt(3))*sqrt(N)*sigma; % Hard threshold 22 | r = max(find(diag(S)>cutoff)); % Keep modes w/ sig > cutoff 23 | Xclean = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; 24 | figure, imshow(Xclean) 25 | 26 | %% 27 | cdS = cumsum(diag(S))./sum(diag(S)); % Cumulative energy 28 | r90 = min(find(cdS>0.90)); % Find r to capture 90% energy 29 | 30 | X90 = U(:,1:r90)*S(1:r90,1:r90)*V(:,1:r90)'; 31 | figure, imshow(X90) 32 | 33 | %% plot singular values 34 | 35 | semilogy(diag(S),'-ok','LineWidth',1.5), hold on, grid on 36 | semilogy(diag(S(1:r,1:r)),'or','LineWidth',1.5) 37 | plot([-20 N+20],[cutoff cutoff],'r--','LineWidth',2) 38 | axis([-10 610 .003 300]) 39 | rectangle('Position',[-5,20,100,200],'LineWidth',2,'LineStyle','--') 40 | 41 | figure 42 | semilogy(diag(S),'-ok','LineWidth',1.5) 43 | hold on, grid on 44 | semilogy(diag(S(1:r,1:r)),'or','LineWidth',1.5) 45 | plot([-20 N+20],[cutoff cutoff],'r--','LineWidth',2) 46 | axis([-5 100 20 200]) 47 | 48 | figure 49 | plot(cdS,'-ok','LineWidth',1.5) 50 | hold on, grid on 51 | plot(cdS(1:r90),'ob','LineWidth',1.5) 52 | plot(cdS(1:r),'or','LineWidth',1.5) 53 | set(gca,'XTick',[0 300 r90 600],'YTick',[0 .5 0.9 1.0]) 54 | xlim([-10 610]) 55 | plot([r90 r90 -10],[0 0.9 0.9],'b--','LineWidth',1.5) -------------------------------------------------------------------------------- /CH01/CH01_SEC07_1_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | t = (-3:.01:3)'; 4 | 5 | Utrue = [cos(17*t).*exp(-t.^2) sin(11*t)]; 6 | Strue = [2 0; 0 .5]; 7 | Vtrue = [sin(5*t).*exp(-t.^2) cos(13*t)]; 8 | 9 | X = Utrue*Strue*Vtrue'; 10 | figure, imshow(X); 11 | 12 | %% 13 | sigma = 1; 14 | Xnoisy = X+sigma*randn(size(X)); 15 | figure, imshow(Xnoisy); 16 | 17 | %% 18 | [U,S,V] = svd(Xnoisy); 19 | 20 | N = size(Xnoisy,1); 21 | cutoff = (4/sqrt(3))*sqrt(N)*sigma; % Hard threshold 22 | r = max(find(diag(S)>cutoff)); % Keep modes w/ sig > cutoff 23 | Xclean = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; 24 | figure, imshow(Xclean) 25 | 26 | %% 27 | cdS = cumsum(diag(S))./sum(diag(S)); % Cumulative energy 28 | r90 = min(find(cdS>0.90)); % Find r to capture 90% energy 29 | 30 | X90 = U(:,1:r90)*S(1:r90,1:r90)*V(:,1:r90)'; 31 | figure, imshow(X90) 32 | 33 | %% plot singular values 34 | figure 35 | semilogy(diag(S),'-ok','LineWidth',1.5) 36 | hold on, grid on 37 | semilogy(diag(S(1:r,1:r)),'or','LineWidth',1.5) 38 | plot([-20 N+20],[cutoff cutoff],'r--','LineWidth',2) 39 | axis([-10 610 .003 300]) 40 | rectangle('Position',[-5,20,100,200],'LineWidth',2,'LineStyle','--') 41 | 42 | figure 43 | semilogy(diag(S),'-ok','LineWidth',1.5) 44 | hold on, grid on 45 | semilogy(diag(S(1:r,1:r)),'or','LineWidth',1.5) 46 | plot([-20 N+20],[cutoff cutoff],'r--','LineWidth',2) 47 | axis([-5 100 20 200]) 48 | 49 | figure 50 | plot(cdS,'-ok','LineWidth',1.5) 51 | hold on, grid on 52 | plot(cdS(1:r90),'ob','LineWidth',1.5) 53 | plot(cdS(1:r),'or','LineWidth',1.5) 54 | set(gca,'XTick',[0 300 r90 600],'YTick',[0 .5 0.9 1.0]) 55 | xlim([-10 610]) 56 | plot([r90 r90 -10],[0 0.9 0.9],'b--','LineWidth',1.5) -------------------------------------------------------------------------------- /CH01/CH01_SEC07_2_Truncation.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | n = 1000; % 1000 x 1000 square 4 | X = zeros(n,n); 5 | X(n/4:3*n/4,n/4:3:n/4) = 1; 6 | imshow(X); 7 | 8 | Y = imrotate(X,10,'bicubic'); % rotate 10 degrees 9 | Y = Y - Y(1,1); 10 | nY = size(Y,1); 11 | startind = floor((nY-n)/2); 12 | Xrot = Y(startind:startind+n-1, startind:startind+n-1); 13 | imshow(Xrot); 14 | [U,S,V] = svd(X); % SVD well-aligned square 15 | [U,S,V] = svd(Xrot); % SVD rotated square 16 | semilogy(diag(S),'-ko') 17 | semilogy(diag(S),'-ko') -------------------------------------------------------------------------------- /CH01/CH01_SEC07_2_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | n = 1000; 4 | q = n/4; 5 | X = zeros(n,n); 6 | X(q:(n/2)+q,q:(n/2)+q) = 1; 7 | 8 | subplot(2,2,1), imshow(X); colormap gray, axis off 9 | 10 | Y = imrotate(X,10,'bicubic'); % rotate 10 degrees 11 | Y = Y - Y(1,1); 12 | nY = size(Y,1); 13 | startind = floor((nY-n)/2); 14 | Xrot = Y(startind:startind+n-1, startind:startind+n-1); 15 | subplot(2,2,2), imshow(Xrot); colormap gray, axis off 16 | 17 | [U,S,V] = svd(X); % svd well-aligned square 18 | [U,S,V] = svd(Xrot); % svd rotated square 19 | 20 | subplot(2,2,3), semilogy(diag(S),'-ko') 21 | ylim([1.e-16 1.e4]), grid on 22 | set(gca,'YTick',[1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4]) 23 | set(gca,'XTick',[0 250 500 750 1000]) 24 | 25 | subplot(2,2,4), semilogy(diag(S),'-ko') 26 | ylim([1.e-16 1.e4]), grid on 27 | set(gca,'YTick',[1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4]) 28 | set(gca,'XTick',[0 250 500 750 1000]) -------------------------------------------------------------------------------- /CH01/CH01_SEC07_3_Alignment.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | n = 1000; 3 | X = zeros(n,n); 4 | X(n/4:3*n/4,n/4:3*n/4) = 1; 5 | [U,S,V] = svd(X); 6 | imagesc(X), hold on; 7 | semilogy(diag(S),'-o','color',cm(1,:)), hold on, grid on 8 | 9 | nAngles = 12; % sweep through 12 angles, from 0:4:44 10 | Xrot = X; 11 | for j=2:nAngles 12 | Y = imrotate(X,(j-1)*4,'bicubic'); % rotate (j-1)*4 13 | startind = floor((size(Y,1)-n)/2); 14 | Xrot1 = Y(startind:startind+n-1, startind:startind+n-1); 15 | Xrot2 = Xrot1 - Xrot1(1,1); 16 | Xrot2 = Xrot2/max(Xrot2(:)); 17 | Xrot(Xrot2>.5) = j; 18 | 19 | [U,S,V] = svd(Xrot1); 20 | subplot(1,2,1), imagesc(Xrot), colormap([0 0 0; cm]) 21 | subplot(1,2,2), semilogy(diag(S),'-o','color',cm(j,:)) 22 | end 23 | -------------------------------------------------------------------------------- /CH01/CH01_SEC07_3_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | n = 1000; 3 | X = zeros(n,n); 4 | X(n/4:3*n/4,n/4:3*n/4) = 1; 5 | nAngles = 12; % sweep through 12 different angles, from 0:4:44 6 | cm=colormap(jet(nAngles)); 7 | 8 | [U,S,V] = svd(X); 9 | subplot(1,2,1), imagesc(X), hold on; 10 | subplot(1,2,2), semilogy(diag(S),'-o','color',cm(1,:)), hold on, grid on 11 | 12 | Xrot = X; 13 | for j=2:nAngles 14 | Y = imrotate(X,(j-1)*4,'bicubic'); % rotate theta = (j-1)*4 15 | startind = floor((size(Y,1)-n)/2); 16 | Xrot1 = Y(startind:startind+n-1, startind:startind+n-1); 17 | Xrot2 = Xrot1 - Xrot1(1,1); 18 | Xrot2 = Xrot2/max(Xrot2(:)); 19 | Xrot(Xrot2>.5) = j; 20 | 21 | [U,S,V] = svd(Xrot1); 22 | subplot(1,2,1), imagesc(Xrot), colormap([0 0 0; cm]) 23 | subplot(1,2,2), semilogy(diag(S),'-o','color',cm(j,:)) 24 | end 25 | axis([1.e-16 1.e3 -10 1000]) 26 | set(gca,'XTick',[0 250 500 750 1000]) 27 | set(gca,'YTick',[1.e-16 1.e-12 1.e-8 1.e-4 1. 1.e4]); 28 | set(gcf,'Position',[100 100 550 230]) 29 | -------------------------------------------------------------------------------- /CH01/CH01_SEC08_RSVD.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A=imread('jupiter.jpg'); 3 | X=double(rgb2gray(A)); 4 | [U,S,V] = svd(X,'econ'); % Deterministic SVD 5 | 6 | r = 400; % Target rank 7 | q = 1; % Power iterations 8 | p = 5; % Oversampling parameter 9 | [rU,rS,rV] = rsvd(X,r,q,p); % Randomized SVD 10 | 11 | %% Reconstruction 12 | XSVD = U(:,1:r)*S(1:r,1:r)*V(:,1:r)'; % SVD approx. 13 | errSVD = norm(X-XSVD,2)/norm(X,2); 14 | XrSVD = rU(:,1:r)*rS(1:r,1:r)*rV(:,1:r)'; % rSVD approx. 15 | errrSVD = norm(X-XrSVD,2)/norm(X,2); 16 | 17 | %% Plot 18 | ax1 = axes('Position',[.005 .005 .33 .99]); 19 | imagesc(X), axis off, colormap gray 20 | 21 | ax2 = axes('Position',[.335 .005 .33 .99]); 22 | imagesc(XSVD), axis off 23 | 24 | ax3 = axes('Position',[.665 .005 .33 .99]); 25 | imagesc(XrSVD), axis off 26 | 27 | set(gcf,'Position',[100 100 2114 1000]) 28 | set(gcf,'PaperPositionMode','auto') 29 | print('-depsc2', '-loose' , 'rSVD.eps'); 30 | %% Illustrate power iterations 31 | X = randn(1000,100); 32 | [U,S,V] = svd(X,'econ'); 33 | S = diag(1:-.01:.01); 34 | X = U*S*V'; 35 | 36 | plot(diag(S),'ko','LineWidth',1.5) 37 | hold on 38 | 39 | CC = jet(11); 40 | Y = X; 41 | for q=1:5 42 | Y = X'*Y; 43 | Y = X*Y; 44 | [Uq,Sq,Vq] = svd(Y,'econ'); 45 | plot(diag(Sq),'-o','Color',CC(mod(2*q+4,11)+1,:),'LineWidth',1.5); 46 | end 47 | legend('SVD','rSVD, q=1','rSVD, q=2','rSVD, q=3','rSVD, q=4','rSVD, q=5'); 48 | set(gcf,'Position',[100 100 500 225]) 49 | ylim([0 1]) -------------------------------------------------------------------------------- /CH01/CH01_SEC09_Tensor.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | 4 | % A=randn(10,10,10); 5 | % model=parafac(A,3); 6 | % [A1,A2,A3]=fac2let(model) 7 | 8 | 9 | x=-5:0.1:5; y=-6:0.1:6; t=0:0.1:10*pi; 10 | [X,Y,T]=meshgrid(x,y,t); 11 | A=exp(-(X.^2+0.5*Y.^2)).*(cos(2*T))+ ... 12 | (sech(X).*tanh(X).*exp(-0.2*Y.^2)).*sin(T); 13 | 14 | for j=1:length(t) 15 | pcolor(x,y,A(:,:,j)), shading interp, caxis([-1 1]), drawnow 16 | end 17 | 18 | figure(1) 19 | for j=1:8 20 | subplot(2,4,j) 21 | pcolor(x,y,A(:,:,8*j-3)), colormap(hot), shading interp, caxis([-1 1]), axis off 22 | end 23 | 24 | 25 | figure(2) 26 | model=parafac(A,2); 27 | [A1,A2,A3]=fac2let(model); 28 | subplot(3,1,1), plot(y,A1,'Linewidth',[2]) 29 | subplot(3,1,2), plot(x,A2,'Linewidth',[2]) 30 | subplot(3,1,3), plot(t,A3,'Linewidth',[2]) 31 | 32 | subplot(3,1,1), set(gca,'Xtick',[-6 0 6],'Fontsize',[15]) 33 | subplot(3,1,2), set(gca,'Xtick',[-5 0 5],'Fontsize',[15]) 34 | subplot(3,1,3), set(gca,'Xlim',[0 10*pi],'Xtick',[0 5*pi 10*pi],'Xticklabels',{'0','5\pi','10\pi'},'Fontsize',[15]) 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /CH01/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH01/dog.jpg -------------------------------------------------------------------------------- /CH01/rsvd.m: -------------------------------------------------------------------------------- 1 | function [U,S,V] = rsvd(X,r,q,p); 2 | 3 | % Step 1: Sample column space of X with P matrix 4 | ny = size(X,2); 5 | P = randn(ny,r+p); 6 | Z = X*P; 7 | for k=1:q 8 | Z = X*(X'*Z); 9 | end 10 | [Q,R] = qr(Z,0); 11 | 12 | % Step 2: Compute SVD on projected Y=Q'*X; 13 | Y = Q'*X; 14 | [UY,S,V] = svd(Y,'econ'); 15 | U = Q*UY; -------------------------------------------------------------------------------- /CH02/CH02_SEC01_0_InnerProduct.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | f = [0 0 .1 .2 .25 .2 .25 .3 .35 .43 .45 .5 .55 .5 .4 .425 .45 .425 .4 .35 .3 .25 .225 .2 .1 0 0]; 4 | g = [0 0 .025 .1 .2 .175 .2 .25 .25 .3 .32 .35 .375 .325 .3 .275 .275 .25 .225 .225 .2 .175 .15 .15 .05 0 0] -0.025; 5 | 6 | x = 0.1*(1:length(f)); 7 | 8 | xf = (.01:.01:x(end)); 9 | ff = interp1(x,f,xf,'cubic') 10 | 11 | gf = interp1(x,g,xf,'cubic') 12 | 13 | 14 | plot(xf(20:end-10),ff(20:end-10),'k','LineWidth',1.5) 15 | hold on 16 | plot(x(2:end-1),f(2:end-1),'bo','MarkerFace','b') 17 | plot(xf(20:end-10),gf(20:end-10),'k','LineWidth',1.5) 18 | plot(x(2:end-1),g(2:end-1),'ro','MarkerFace','r') 19 | 20 | 21 | xlim([.1 2.7]) 22 | ylim([-.1 .6]) 23 | set(gca,'XTick',[.2:.1:2.6],'XTickLabels',{},'LineWidth',1.2) 24 | set(gca,'YTick',[]); 25 | box off 26 | 27 | set(gcf,'Position',[100 100 550 250]) 28 | 29 | set(gcf,'PaperPositionMode','auto') 30 | print('-depsc2', '-loose', '../figures/InnerProduct'); 31 | 32 | % %% 33 | % xc = x; 34 | % fc = f; 35 | % n = length(x); 36 | % hold on 37 | % fapx = 0*ff; 38 | % dx = xc(2)-xc(1); 39 | % L = xc(end)-xc(1); 40 | % L = 2.5 41 | % A0 = (1/pi)*sum(fc.*ones(size(xc)))*dx*L; 42 | % fapx = fapx + A0/2; 43 | % for k=1:10 44 | % Ak = (1/pi)*sum(fc.*cos(2*pi*k*xc/L))*dx*L; 45 | % Bk = (1/pi)*sum(fc.*sin(2*pi*k*xc/L))*dx*L; 46 | % 47 | % fapx = fapx + Ak*cos(2*k*pi*xf/L) + Bk*sin(2*k*pi*xf/L); 48 | % end 49 | % plot(xf,fapx,'k') 50 | -------------------------------------------------------------------------------- /CH02/CH02_SEC01_1_FourierSines.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % Define domain 4 | dx = 0.001; 5 | L = pi; 6 | x = (-1+dx:dx:1)*L; 7 | n = length(x); nquart = floor(n/4); 8 | 9 | % Define hat function 10 | f = 0*x; 11 | f(nquart:2*nquart) = 4*(1:nquart+1)/n; 12 | f(2*nquart+1:3*nquart) = 1-4*(0:nquart-1)/n; 13 | plot(x,f,'-k','LineWidth',1.5), hold on 14 | 15 | % Compute Fourier series 16 | CC = jet(20); 17 | A0 = sum(f.*ones(size(x)))*dx; 18 | fFS = A0/2; 19 | for k=1:20 20 | A(k) = sum(f.*cos(pi*k*x/L))*dx; % Inner product 21 | B(k) = sum(f.*sin(pi*k*x/L))*dx; 22 | fFS = fFS + A(k)*cos(k*pi*x/L) + B(k)*sin(k*pi*x/L); 23 | plot(x,fFS,'-','Color',CC(k,:),'LineWidth',1.2) 24 | end 25 | 26 | 27 | %% Plot amplitudes 28 | figure 29 | clear ERR 30 | clear A 31 | fFS = A0/2; 32 | A(1) = A0/2; 33 | ERR(1) = norm(f-fFS); 34 | kmax = 100; 35 | for k=1:kmax 36 | A(k+1) = sum(f.*cos(pi*k*x/L))*dx; 37 | B(k+1) = sum(f.*sin(pi*k*x/L))*dx; 38 | % plot(x,B(k)*sin(2*k*pi*x/L),'k-','LineWidth',1.2); 39 | fFS = fFS + A(k+1)*cos(k*pi*x/L) + B(k+1)*sin(k*pi*x/L); 40 | ERR(k+1) = norm(f-fFS)/norm(f); 41 | end 42 | thresh = median(ERR)*sqrt(kmax)*4/sqrt(3); 43 | r = max(find(ERR>thresh)); 44 | r = 7; 45 | subplot(2,1,1) 46 | semilogy(0:1:kmax,A,'k','LineWidth',1.5) 47 | hold on 48 | semilogy(r,A(r+1),'bo','LineWidth',1.5) 49 | xlim([0 kmax]) 50 | ylim([10^(-7) 1]) 51 | subplot(2,1,2) 52 | semilogy(0:1:kmax,ERR,'k','LineWidth',1.5) 53 | hold on 54 | semilogy(r,ERR(r+1),'bo','LineWidth',1.5) 55 | -------------------------------------------------------------------------------- /CH02/CH02_SEC01_2_Gibbs.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | dx = 0.01; L = 10; 3 | x = 0:dx:L; 4 | n = length(x); nquart = floor(n/4); 5 | 6 | f = zeros(size(x)); 7 | f(nquart:3*nquart) = 1; 8 | 9 | A0 = sum(f.*ones(size(x)))*dx*2/L; 10 | fFS = A0/2; 11 | for k=1:100 12 | Ak = sum(f.*cos(2*pi*k*x/L))*dx*2/L; 13 | Bk = sum(f.*sin(2*pi*k*x/L))*dx*2/L; 14 | fFS = fFS + Ak*cos(2*k*pi*x/L) + Bk*sin(2*k*pi*x/L); 15 | end 16 | 17 | plot(x,f,'k','LineWidth',2), hold on 18 | plot(x,fFS,'r-','LineWidth',1.2) -------------------------------------------------------------------------------- /CH02/CH02_SEC01_2_Gibbs_Movie.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | dx = 0.01; 4 | L = 2*pi; 5 | x = 0:dx:L; 6 | 7 | f = zeros(size(x));% sin(x/L);%ones(size(x)); 8 | f(floor(length(f)/4):floor(3*length(f)/4)) = 1+f(floor(length(f)/4):floor(3*length(f)/4)); 9 | % f = (x-pi).^2; 10 | fFS = zeros(size(x)); 11 | 12 | A0 = (1/pi)*sum(f.*ones(size(x)))*dx; 13 | for m=1:100 14 | fFS = A0/2; 15 | for k=1:m 16 | Ak = (1/pi)*sum(f.*cos(2*pi*k*x/L))*dx; 17 | Bk = (1/pi)*sum(f.*sin(2*pi*k*x/L))*dx; 18 | fFS = fFS + Ak*cos(2*k*pi*x/L) + Bk*sin(2*k*pi*x/L); 19 | end 20 | 21 | plot(x,f,'k') 22 | hold on 23 | plot(x,fFS,'r-') 24 | 25 | pause(0.1) 26 | end -------------------------------------------------------------------------------- /CH02/CH02_SEC01_2_Gibbs_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | dx = 0.01; 4 | L = 10; 5 | x = 0:dx:L; 6 | 7 | f = zeros(size(x)); 8 | f(floor(length(f)/4):floor(3*length(f)/4)) = 1;%+f(floor(length(f)/4):floor(3*length(f)/4)); 9 | % fFS = zeros(size(x)); 10 | 11 | A0 = sum(f.*ones(size(x)))*dx*2/L; 12 | for m=100%1:100 13 | fFS = A0/2; 14 | for k=1:m 15 | Ak = sum(f.*cos(2*pi*k*x/L))*dx*2/L; 16 | Bk = sum(f.*sin(2*pi*k*x/L))*dx*2/L; 17 | fFS = fFS + Ak*cos(2*k*pi*x/L) + Bk*sin(2*k*pi*x/L); 18 | end 19 | 20 | plot(x,f,'k','LineWidth',2) 21 | hold on 22 | plot(x,fFS,'r-','LineWidth',1.2) 23 | 24 | pause(0.1) 25 | end 26 | 27 | set(gca,'XTick',[],'XTickLabels',{},'LineWidth',1.2) 28 | set(gca,'YTick',[]); 29 | box off 30 | % axis off 31 | 32 | set(gcf,'Position',[100 100 550 200]) 33 | set(gcf,'PaperPositionMode','auto') 34 | % print('-depsc2', '-loose', '../figures/Gibbs'); -------------------------------------------------------------------------------- /CH02/CH02_SEC02_1_DFT.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | n = 256; 3 | w = exp(-i*2*pi/n); 4 | 5 | % Slow 6 | for i=1:n 7 | for j=1:n 8 | DFT(i,j) = w^((i-1)*(j-1)); 9 | end 10 | end 11 | 12 | % Fast 13 | [I,J] = meshgrid(1:n,1:n); 14 | DFT = w.^((I-1).*(J-1)); 15 | imagesc(real(DFT)) 16 | -------------------------------------------------------------------------------- /CH02/CH02_SEC02_1_DFT_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | 4 | % x = ones(500,1); 5 | % t = .1*(1:500)'; 6 | % x = cos(t*2*pi); 7 | % y = fft(x); 8 | % 9 | % N = length(x); 10 | % 11 | % plot(real(y),'k') 12 | % hold on 13 | % plot(real(DFT*x),'r--') 14 | 15 | %% slow and steady 16 | clear all, close all, clc 17 | N = 256; 18 | w = exp(-i*2*pi/N); 19 | for i=1:N 20 | for j=1:N 21 | DFT(i,j) = w^((i-1)*(j-1)); 22 | end 23 | end 24 | 25 | imagesc(real(DFT)) 26 | colormap jet 27 | axis off 28 | axis equal 29 | box on 30 | colorbar 31 | set(gcf,'Position',[100 100 1000 900]) 32 | set(gcf,'PaperPositionMode','auto') 33 | % print('-depsc2', '-loose', '../figures/DFTMatrixreal'); 34 | % print('-dpng', '-loose', '../figures/DFTMatrixreal'); 35 | 36 | %% 37 | 38 | imagesc(imag(DFT)) 39 | colormap jet 40 | axis off 41 | axis equal 42 | box on 43 | colorbar 44 | set(gcf,'Position',[100 100 1000 900]) 45 | set(gcf,'PaperPositionMode','auto') 46 | % print('-depsc2', '-loose', '../figures/DFTMatriximag'); 47 | % print('-dpng', '-loose', '../figures/DFTMatriximag'); 48 | %% really fast 49 | [I,J] = meshgrid(1:N,1:N); 50 | DFT = w.^((I-1).*(J-1)); 51 | -------------------------------------------------------------------------------- /CH02/CH02_SEC02_2_Denoise.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | %% Create a simple signal with two frequencies 3 | dt = .001; 4 | t = 0:dt:1; 5 | f = sin(2*pi*50*t) + sin(2*pi*120*t); % Sum of 2 frequencies 6 | f = f + 2.5*randn(size(t)); % Add some noise 7 | 8 | %% Compute the Fast Fourier Transform FFT 9 | n = length(t); 10 | fhat = fft(f,n); % Compute the fast Fourier transform 11 | PSD = fhat.*conj(fhat)/n; % Power spectrum (power per freq) 12 | freq = 1/(dt*n)*(0:n); % Create x-axis of frequencies in Hz 13 | L = 1:floor(n/2); % Only plot the first half of freqs 14 | 15 | %% Use the PSD to filter out noise 16 | indices = PSD>100; % Find all freqs with large power 17 | PSDclean = PSD.*indices; % Zero out all others 18 | fhat = indices.*fhat; % Zero out small Fourier coeffs. in Y 19 | ffilt = ifft(fhat); % Inverse FFT for filtered time signal 20 | 21 | %% PLOTS 22 | subplot(3,1,1) 23 | plot(t,f,'r','LineWidth',1.2), hold on 24 | plot(t,f,'k','LineWidth',1.5) 25 | legend('Noisy','Clean') 26 | 27 | subplot(3,1,2) 28 | plot(t,f,'k','LineWidth',1.5), hold on 29 | plot(t,ffilt,'b','LineWidth',1.2) 30 | legend('Clean','Filtered') 31 | 32 | subplot(3,1,3) 33 | plot(freq(L),PSD(L),'r','LineWidth',1.5), hold on 34 | plot(freq(L),PSDclean(L),'-b','LineWidth',1.2) 35 | legend('Noisy','Filtered') 36 | 37 | % 38 | % %% PLOTS 39 | % subplot(3,1,1) 40 | % plot(t,y,'r','LineWidth',1.2) 41 | % hold on 42 | % plot(t,x,'k','LineWidth',1.5) 43 | % axis([0 .25 -5 5]) 44 | % legend('Noisy','Clean') 45 | % 46 | % subplot(3,1,2) 47 | % plot(t,x,'k','LineWidth',1.5) 48 | % hold on 49 | % plot(t,yfilt,'b','LineWidth',1.2) 50 | % axis([0 .25 -5 5]) 51 | % legend('Clean','Filtered') 52 | % 53 | % subplot(3,1,3) 54 | % plot(freq(L),PSD(L),'r','LineWidth',1.5) 55 | % hold on 56 | % plot(freq(L),PSDclean(L),'-b','LineWidth',1.2) 57 | % legend('Noisy','Filtered') -------------------------------------------------------------------------------- /CH02/CH02_SEC02_2_Denoise_production.m: -------------------------------------------------------------------------------- 1 | clear all 2 | 3 | %% create a simple signal with two frequencies 4 | dt = .001; 5 | t = 0:dt:1; 6 | x = sin(2*pi*50*t) + sin(2*pi*120*t); 7 | y = x + 2.5*randn(size(t)); % add some noise 8 | 9 | 10 | 11 | %% Compute the Fast Fourier Transform FFT 12 | N = length(t); 13 | Y = fft(y,N); % computes the (fast) discrete fourier transform 14 | PSD = Y.*conj(Y)/N; % Power spectrum (how much power in each freq) 15 | freq = 1/(dt*N)*(0:N); %create the x-axis of frequencies in Hz 16 | L = 1:floor(N/2); % only plot the first half of freqs 17 | 18 | 19 | %% Use the PSD to filter out noise 20 | indices = PSD>100; % Find all freqs with large power 21 | PSDclean = PSD.*indices; % Zero out all others 22 | 23 | Y = indices.*Y; % zero out small Fourier coefficients in Y 24 | yfilt = ifft(Y); % inverse FFT to get filtered time-domain signal 25 | 26 | 27 | %% PLOTS 28 | subplot(3,1,1) 29 | plot(-1000,-1000,'k','LineWidth',1.5) 30 | hold on 31 | plot(-1000,-1000,'r','LineWidth',1.2) 32 | plot(t,y,'r','LineWidth',1.2) 33 | plot(t,x,'k','LineWidth',1.5) 34 | axis([0 .25 -5 5]) 35 | legend('Clean','Noisy') 36 | set(gca,'LineWidth',1.2,'FontSize',12) 37 | 38 | subplot(3,1,3) 39 | plot(t,x,'k','LineWidth',1.5) 40 | hold on 41 | plot(t,yfilt,'b','LineWidth',1.2) 42 | axis([0 .25 -5 5]) 43 | legend('Clean','Filtered') 44 | set(gca,'LineWidth',1.2,'FontSize',12) 45 | 46 | subplot(3,1,2) 47 | plot(freq(L),PSD(L),'r','LineWidth',1.5) 48 | % xlabel('Frequency (Hz)') 49 | hold on 50 | plot(freq(L),PSDclean(L),'-b','LineWidth',1.2) 51 | legend('Noisy','Filtered') 52 | set(gca,'LineWidth',1.2,'FontSize',12) 53 | 54 | set(gcf,'Position',[100 100 550 450]) 55 | set(gcf,'PaperPositionMode','auto') 56 | print('-depsc2', '-loose', '../figures/DENOISE'); 57 | -------------------------------------------------------------------------------- /CH02/CH02_SEC02_3_SpectralDerivative.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | n = 128; 4 | L = 30; 5 | dx = L/(n); 6 | x = -L/2:dx:L/2-dx; 7 | f = cos(x).*exp(-x.^2/25); % Function 8 | df = -(sin(x).*exp(-x.^2/25) + (2/25)*x.*f); % Derivative 9 | 10 | %% Approximate derivative using finite Difference... 11 | for kappa=1:length(df)-1 12 | dfFD(kappa) = (f(kappa+1)-f(kappa))/dx; 13 | end 14 | dfFD(end+1) = dfFD(end); 15 | 16 | %% Derivative using FFT (spectral derivative) 17 | fhat = fft(f); 18 | kappa = (2*pi/L)*[-n/2:n/2-1]; 19 | kappa = fftshift(kappa); % Re-order fft frequencies 20 | dfhat = i*kappa.*fhat; 21 | dfFFT = real(ifft(dfhat)); 22 | 23 | %% Plotting commands 24 | plot(x,df,'k','LineWidth',1.5), hold on 25 | plot(x,dfFD,'b--','LineWidth',1.2) 26 | plot(x,dfFFT,'r--','LineWidth',1.2) 27 | legend('True Derivative','Finite Diff.','FFT Derivative') -------------------------------------------------------------------------------- /CH02/CH02_SEC03_1_FFTHeat.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | a = 1; % Thermal diffusivity constant 3 | L = 100; % Length of domain 4 | N = 1000; % Number of discretization points 5 | dx = L/N; 6 | x = -L/2:dx:L/2-dx; % Define x domain 7 | 8 | % Define discrete wavenumbers 9 | kappa = (2*pi/L)*[-N/2:N/2-1]; 10 | kappa = fftshift(kappa); % Re-order fft wavenumbers 11 | 12 | % Initial condition 13 | u0 = 0*x; 14 | u0((L/2 - L/10)/dx:(L/2 + L/10)/dx) = 1; 15 | 16 | % Simulate in Fourier frequency domain 17 | t = 0:0.1:10; 18 | [t,uhat]=ode45(@(t,uhat)rhsHeat(t,uhat,kappa,a),t,fft(u0)); 19 | 20 | for k = 1:length(t) % iFFT to return to spatial domain 21 | u(k,:) = ifft(uhat(k,:)); 22 | end 23 | 24 | 25 | % Plot solution in time 26 | figure, waterfall((u(1:10:end,:))); 27 | figure, imagesc(flipud(u)); 28 | 29 | %% FIGURES (PRODUCTION) 30 | figure 31 | CC = colormap(jet(100)); 32 | dt = 0.1; 33 | for k = 1:100 34 | u(k,:) = ifft(uhat(k,:)); 35 | if(mod(k-1,10)==0) 36 | plot(x,u(k,:),'Color',CC(k,:),'LineWidth',1.5) 37 | hold on, grid on, drawnow 38 | end 39 | end 40 | % xlabel('Spatial variable, x') 41 | % ylabel('Temperature, u(x,t)') 42 | axis([-50 50 -.1 1.1]) 43 | set(gca,'LineWidth',1.2,'FontSize',12); 44 | set(gcf,'Position',[100 100 550 220]); 45 | set(gcf,'PaperPositionMode','auto') 46 | % print('-depsc2', '-loose', '../../figures/FFTHeat1'); 47 | 48 | % 49 | figure 50 | subplot(1,2,1) 51 | h=waterfall(u(1:10:end,:)); 52 | set(h,'LineWidth',2,'FaceAlpha',.5); 53 | colormap(jet/1.5) 54 | view(22,29) 55 | set(gca,'LineWidth',1.5) 56 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 57 | set(gca,'ZTick',[0 .5 1],'ZTickLabels',{}) 58 | set(gca,'YTick',[0 5 10],'YTickLabels',{}) 59 | 60 | subplot(1,2,2) 61 | imagesc(flipud(u)); 62 | set(gca,'LineWidth',1.5) 63 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 64 | set(gca,'YTick',[0 50 100],'YTickLabels',{}) 65 | 66 | colormap jet 67 | set(gcf,'Position',[100 100 600 250]) 68 | set(gcf,'PaperPositionMode','auto') 69 | % print('-depsc2', '-loose', '../../figures/FFTHeat2'); -------------------------------------------------------------------------------- /CH02/CH02_SEC03_1_FFTHeat_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | a = 1; % Thermal diffusivity constant 3 | L = 100; % Length of domain 4 | N = 1000; % Number of discretization points 5 | dx = L/N; 6 | x = -L/2:dx:L/2-dx; % Define x domain 7 | 8 | % Define discrete wavenumbers 9 | kappa = (2*pi/L)*[-N/2:N/2-1]; 10 | kappa = fftshift(kappa); % Re-order fft wavenumbers 11 | 12 | % Initial condition 13 | u0 = 0*x; 14 | u0((L/2 - L/10)/dx:(L/2 + L/10)/dx) = 1; 15 | 16 | % Simulate in Fourier frequency domain 17 | t = 0:0.1:10; 18 | [t,uhat]=ode45(@(t,uhat)rhsHeat(t,uhat,kappa,a),t,fft(u0)); 19 | 20 | for k = 1:length(t) % iFFT to return to spatial domain 21 | u(k,:) = ifft(uhat(k,:)); 22 | end 23 | 24 | 25 | % Plot solution in time 26 | subplot(1,2,1) 27 | h=waterfall((u(1:10:end,:))); 28 | set(h,'LineWidth',2,'FaceAlpha',.5); 29 | colormap(jet/1.5) 30 | 31 | subplot(1,2,2) 32 | imagesc(flipud(u)); 33 | colormap jet 34 | %% FIGURES (PRODUCTION) 35 | figure 36 | CC = colormap(jet(100)); 37 | dt = 0.1; 38 | for k = 1:100 39 | u(k,:) = ifft(uhat(k,:)); 40 | if(mod(k-1,10)==0) 41 | plot(x,u(k,:),'Color',CC(k,:),'LineWidth',1.5) 42 | hold on, grid on, drawnow 43 | end 44 | end 45 | % xlabel('Spatial variable, x') 46 | % ylabel('Temperature, u(x,t)') 47 | axis([-50 50 -.1 1.1]) 48 | set(gca,'LineWidth',1.2,'FontSize',12); 49 | set(gcf,'Position',[100 100 550 220]); 50 | set(gcf,'PaperPositionMode','auto') 51 | % print('-depsc2', '-loose', '../../figures/FFTHeat1'); 52 | 53 | % 54 | figure 55 | subplot(1,2,1) 56 | h=waterfall(u(1:10:end,:)); 57 | set(h,'LineWidth',2,'FaceAlpha',.5); 58 | colormap(jet/1.5) 59 | view(22,29) 60 | set(gca,'LineWidth',1.5) 61 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 62 | set(gca,'ZTick',[0 .5 1],'ZTickLabels',{}) 63 | set(gca,'YTick',[0 5 10],'YTickLabels',{}) 64 | 65 | subplot(1,2,2) 66 | imagesc(flipud(u)); 67 | set(gca,'LineWidth',1.5) 68 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 69 | set(gca,'YTick',[0 50 100],'YTickLabels',{}) 70 | 71 | colormap jet 72 | set(gcf,'Position',[100 100 600 250]) 73 | set(gcf,'PaperPositionMode','auto') 74 | % print('-depsc2', '-loose', '../../figures/FFTHeat2'); -------------------------------------------------------------------------------- /CH02/CH02_SEC03_2_FFTWave.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % Define spatial domain 4 | c = 2; % Wave speed 5 | L = 20; % Length of domain 6 | N = 1000; % Number of discretization points 7 | dx = L/N; 8 | x = -L/2:dx:L/2-dx; % Define x domain 9 | 10 | % Define discrete wavenumbers 11 | kappa = (2*pi/L)*[-N/2:N/2-1]; 12 | kappa = fftshift(kappa'); % Re-order fft wavenumbers 13 | 14 | % Initial condition 15 | u0 = sech(x); 16 | uhat0 = fft(u0); 17 | 18 | % Simulate in Fourier frequency domain 19 | dt = 0.025; 20 | t = 0:dt:100*dt; 21 | [t,uhat] = ode45(@(t,uhat)rhsWave(t,uhat,kappa,c),t,uhat0); 22 | 23 | % Alternatively, simulate in spatial domain 24 | [t,u] = ode45(@(t,u)rhsWaveSpatial(t,u,kappa,c),t,u0); 25 | 26 | % Inverse FFT to bring back to spatial domain 27 | for k = 1:length(t) 28 | u(k,:) = ifft(uhat(k,:)); 29 | end 30 | 31 | % Plot solution in time 32 | subplot(1,2,1) 33 | h=waterfall(real(u(1:10:end,:))); 34 | set(h,'LineWidth',2,'FaceAlpha',.5); 35 | colormap(jet/1.5) 36 | 37 | subplot(1,2,2) 38 | imagesc(flipud(real(u))); 39 | colormap jet 40 | 41 | %% FIGURES (PRODUCTION) 42 | figure 43 | CC = colormap(jet(101)); 44 | dt = 0.025; 45 | for k = 1:length(t) 46 | u(k,:) = real(ifft(uhat(k,:))); 47 | if(mod(k-1,10)==0) 48 | plot(x,u(k,:),'Color',CC(k,:),'LineWidth',1.5) 49 | hold on, grid on, drawnow 50 | end 51 | end 52 | % xlabel('Spatial variable, x') 53 | % ylabel('Temperature, u(x,t)') 54 | axis([-L/2 L/2 -.1 1.1]) 55 | set(gca,'LineWidth',1.2,'FontSize',12); 56 | set(gcf,'Position',[100 100 550 220]); 57 | set(gcf,'PaperPositionMode','auto') 58 | print('-depsc2', '-loose', '../../figures/FFTWave1'); 59 | 60 | % 61 | figure 62 | subplot(1,2,1) 63 | h=waterfall(u(1:10:end,:)); 64 | set(h,'LineWidth',2,'FaceAlpha',.5); 65 | colormap(jet/1.5) 66 | % view(22,29) 67 | view(3,21) 68 | set(gca,'LineWidth',1.5) 69 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 70 | set(gca,'ZTick',[0 .5 1],'ZTickLabels',{}) 71 | set(gca,'YTick',[0 5 10],'YTickLabels',{}) 72 | 73 | subplot(1,2,2) 74 | imagesc(flipud(u)); 75 | set(gca,'LineWidth',1.5) 76 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 77 | set(gca,'YTick',[0 50 100],'YTickLabels',{}) 78 | 79 | colormap jet 80 | set(gcf,'Position',[100 100 600 250]) 81 | set(gcf,'PaperPositionMode','auto') 82 | print('-depsc2', '-loose', '../../figures/FFTWave2'); -------------------------------------------------------------------------------- /CH02/CH02_SEC03_3_FFTBurgers.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | nu=0.001; % Diffusion constant 3 | 4 | % Define spatial domain 5 | L = 20; % Length of domain 6 | N = 1000; % Number of discretization points 7 | dx = L/N; 8 | x = -L/2:dx:L/2-dx; % Define x domain 9 | 10 | % Define discrete wavenumbers 11 | kappa = (2*pi/L)*[-N/2:N/2-1]; 12 | kappa = fftshift(kappa'); % Re-order fft wavenumbers 13 | 14 | % Initial condition 15 | u0 = sech(x); 16 | 17 | % Simulate PDE in spatial domain 18 | dt = 0.025; 19 | t = 0:dt:100*dt; 20 | [t,u] = ode45(@(t,u)rhsBurgers(t,u,kappa,nu),t,u0); 21 | 22 | % Plot solution in time 23 | subplot(1,2,1) 24 | h=waterfall(real(u(1:10:end,:))); 25 | set(h,'LineWidth',2,'FaceAlpha',.5); 26 | colormap(jet/1.5) 27 | view(5,55) 28 | 29 | subplot(1,2,2) 30 | imagesc(flipud(real(u))); 31 | colormap jet 32 | 33 | %% FIGURES (PRODUCTION) 34 | figure 35 | CC = colormap(jet(100)); 36 | dt = 0.1; 37 | for k = 1:100 38 | if(mod(k-1,10)==0) 39 | plot(x,real(u(k,:)),'k','LineWidth',1.5) 40 | % plot(x,real(u(k,:)),'Color',CC(k,:),'LineWidth',1.5) 41 | hold on, grid on, drawnow 42 | end 43 | end 44 | % xlabel('Spatial variable, x') 45 | % ylabel('Temperature, u(x,t)') 46 | axis([-L/2 L/2 -.1 1.1]) 47 | set(gca,'LineWidth',1.2,'FontSize',12); 48 | set(gcf,'Position',[100 100 550 220]); 49 | set(gcf,'PaperPositionMode','auto') 50 | print('-depsc2', '-loose', '../../figures/FFTBurgers1'); 51 | 52 | % 53 | figure 54 | subplot(1,2,1) 55 | h=waterfall(real(u(1:10:end,:))); 56 | set(h,'LineWidth',2,'FaceAlpha',.5); 57 | colormap(jet/1.5) 58 | view(5,55) 59 | set(gca,'LineWidth',1.5) 60 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 61 | set(gca,'ZTick',[0 .5 1],'ZTickLabels',{}) 62 | set(gca,'YTick',[0 5 10],'YTickLabels',{}) 63 | set(gca,'YLim',[1 11]) 64 | set(gca,'ZLim',[-.1 1.1]) 65 | 66 | subplot(1,2,2) 67 | imagesc(flipud(real(u))); 68 | set(gca,'LineWidth',1.5) 69 | set(gca,'XTick',[0 500 1000],'XTickLabels',{}) 70 | set(gca,'YTick',[0 50 100],'YTickLabels',{}) 71 | 72 | colormap jet 73 | set(gcf,'Position',[100 100 600 250]) 74 | set(gcf,'PaperPositionMode','auto') 75 | print('-depsc2', '-loose', '../../figures/FFTBurgers2'); -------------------------------------------------------------------------------- /CH02/CH02_SEC04_1_SpectrogramChirp.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | t = 0:0.001:2; 4 | f0 = 50; 5 | f1 = 250; 6 | t1 = 2; 7 | x = chirp(t,f0,t1,f1,'quadratic'); 8 | x = cos(2*pi*t.*(f0 + (f1-f0)*t.^2/(3*t1^2))); 9 | % There is a typo in Matlab documentation... 10 | % ... divide by 3 so derivative amplitude matches frequency 11 | 12 | spectrogram(x,128,120,128,1e3,'yaxis') 13 | colormap jet 14 | 15 | set(gca,'LineWidth',1.2,'FontSize',12); 16 | set(gcf,'Position',[100 100 550 200]); 17 | set(gcf,'PaperPositionMode','auto') 18 | print('-depsc2', '-loose', '../../figures/SPECTROGRAM_CHIRP'); 19 | 20 | %% 21 | 22 | -------------------------------------------------------------------------------- /CH02/CH02_SEC04_2_SpectrogramBeethoven/CC.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH02/CH02_SEC04_2_SpectrogramBeethoven/CC.mat -------------------------------------------------------------------------------- /CH02/CH02_SEC04_2_SpectrogramBeethoven/CH02_SEC04_2_SpectrogramBeethoven.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % If you download mp3read, you can use this code 4 | % also, need to download mp3read from 5 | % http://www.mathworks.com/matlabcentral/fileexchange/13852-mp3read-and-mp3write 6 | % [Y,FS,NBITS,OPTS] = mp3read('../../DATA/beethoven.mp3'); % add in your own song 7 | % T = 40; % 40 seconds 8 | % y=Y(1:T*FS); % First 40 seconds 9 | load ../../DATA/beethoven_40sec.mat 10 | %% Spectrogram 11 | spectrogram(y,5000,400,24000,24000,'yaxis'); 12 | 13 | %% SPECTROGRAM 14 | % uncomment remaining code and download stft code by M.Sc. Eng. Hristo Zhivomirov 15 | % wlen = 5000; 16 | % h=400; % Overlap is wlen - h 17 | % % perform time-frequency analysis and resynthesis of the original signal 18 | % [S, f, t_stft] = stft(y, wlen, h, FS/4, FS); % y axis range goes up to 4000 HZ 19 | % imagesc(log10(abs(S))); 20 | % load CC.mat 21 | % colormap(ones(size(CC))-(CC)) 22 | % 23 | % axis xy, hold on 24 | % XTicks = [1 300 600 900 1200 1500 1800 2100]; 25 | % XTickLabels = {'0','5','10','15','20','25','30','35'}; 26 | % YTicks = [0 1000 2000 3000]; 27 | % YTickLabels = {'0','4000','8000','12000'}; 28 | % set(gca,'XTick',XTicks,'XTickLabels',XTickLabels); 29 | % set(gca,'YTick',YTicks,'YTickLabels',YTickLabels); 30 | % 31 | % % plot a frequency 32 | % freq = @(n)(((2^(1/12))^(n-49))*440); 33 | % freq(40) % frequency of 40th key = C -------------------------------------------------------------------------------- /CH02/CH02_SEC05_HAAR.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | x = 0:.001:1; 4 | 5 | n = length(x); 6 | n2 = floor(n/2); 7 | n4 = floor(n/4); 8 | 9 | f10 = 0*x; 10 | f10(1:n2-1) = 1; 11 | f10(n2:end) = -1; 12 | 13 | f21 = 0*x; 14 | f21(1:n4-1) = 1; 15 | f21(n4:n2-1) = -1; 16 | f21 = f21*sqrt(2); 17 | 18 | f22 = 0*x; 19 | f22(n2:n2+n4-1) = 1; 20 | f22(n2+n4:end) = -1; 21 | f22 = f22*sqrt(2); 22 | 23 | x = [-1 0 x 1 2]; 24 | f10 = [0 0 f10 0 0]; 25 | f21 = [0 0 f21 0 0]; 26 | f22 = [0 0 f22 0 0]; 27 | 28 | subplot(3,1,1) 29 | plot(x,f10,'k','LineWidth',2) 30 | xlim([-.2 1.2]) 31 | ylim([-1.2 1.2]) 32 | set(gca,'XTick',[0 .25 .5 .75 1]) 33 | set(gca,'LineWidth',1.2,'FontSize',12); 34 | subplot(3,1,2) 35 | plot(x,f21,'k','LineWidth',2) 36 | xlim([-.2 1.2]) 37 | ylim([-1.75 1.75]) 38 | set(gca,'XTick',[0 .25 .5 .75 1]) 39 | set(gca,'LineWidth',1.2,'FontSize',12); 40 | subplot(3,1,3) 41 | plot(x,f22,'k','LineWidth',2) 42 | xlim([-.2 1.2]) 43 | ylim([-1.75 1.75]) 44 | set(gca,'XTick',[0 .25 .5 .75 1]) 45 | set(gca,'LineWidth',1.2,'FontSize',12); 46 | set(gcf,'Position',[100 100 550 350]); 47 | set(gcf,'PaperPositionMode','auto') 48 | print('-depsc2', '-loose', '../figures/HAAR'); 49 | 50 | %% 51 | figure 52 | x = -5:.001:5; 53 | 54 | fMexHat = (1-x.^2).*exp(-x.^2/2); 55 | plot(x,fMexHat,'k','LineWidth',2) -------------------------------------------------------------------------------- /CH02/CH02_SEC06_1_2DFFT.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); % Convert to grayscale image 4 | subplot(1,3,1), imagesc(B); % Plot image 5 | 6 | for j=1:size(B,1); % Compute row-wise FFT 7 | Cshift(j,:) = fftshift(fft(B(j,:))); 8 | C(j,:) = (fft(B(j,:))); 9 | end 10 | subplot(1,3,2), imagesc(log(abs(Cshift))) 11 | 12 | for j=1:size(C,2); % Compute column-wise FFT 13 | D(:,j) = fft(C(:,j)); 14 | end 15 | subplot(1,3,3), imagesc(fftshift(log(abs(D)))) 16 | 17 | D = fft2(B); % Much more efficient to use fft2 -------------------------------------------------------------------------------- /CH02/CH02_SEC06_2_Compress.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% FFT Compression 6 | Bt=fft2(B); % B is grayscale image from above 7 | Btsort = sort(abs(Bt(:))); % Sort by magnitude 8 | 9 | % Zero out all small coefficients and inverse transform 10 | for keep=[.1 .05 .01 .002]; 11 | thresh = Btsort(floor((1-keep)*length(Btsort))); 12 | ind = abs(Bt)>thresh; % Find small indices 13 | Atlow = Bt.*ind; % Threshold small indices 14 | Alow=uint8(ifft2(Atlow)); % Compressed image 15 | figure, imshow(Alow) % Plot Reconstruction 16 | end 17 | set(f1,'Position',[100 100 600 800]) 18 | set(f2,'Position',[100 100 600 800]) -------------------------------------------------------------------------------- /CH02/CH02_SEC06_2_Compress_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% FFT Compression 6 | figure 7 | Bt=fft2(B); 8 | Btsort = sort(abs(Bt(:))); % Sort by magnitude 9 | 10 | % Zero out all small coefficients and inverse transform 11 | percentvec = [.1 .05 .01 .002]; 12 | for k=1:4 13 | keep = percentvec(k); 14 | thresh = Btsort(floor((1-keep)*length(Btsort))); 15 | ind = abs(Bt)>thresh; 16 | Atlow = Bt.*ind; % Threshold small indices 17 | Flow = log(abs(fftshift(Atlow))+1); % put FFT on log-scale 18 | imshow(mat2gray(Flow),[]); 19 | 20 | % Plot Reconstruction 21 | Alow=uint8(ifft2(Atlow)); 22 | imshow(Alow) 23 | 24 | axis off 25 | set(gcf,'PaperPositionMode','auto') 26 | % print('-depsc2', '-loose', ['../figures/2DFFT_Compress',num2str(k)]); 27 | % print('-dpng', '-loose', ['../figures/2DFFT_Compress',num2str(k)]); 28 | 29 | end -------------------------------------------------------------------------------- /CH02/CH02_SEC06_3_Denoise.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% Denoise 6 | Bnoise = B + uint8(200*randn(size(B))); % Add some noise 7 | Bt=fft2(Bnoise); 8 | F = log(abs(Btshift)+1); % Put FFT on log-scale 9 | 10 | subplot(2,2,1), imagesc(Bnoise) % Plot image 11 | subplot(2,2,2), imagesc(F) % Plot FFT 12 | 13 | [nx,ny] = size(B); 14 | [X,Y] = meshgrid(-ny/2+1:ny/2,-nx/2+1:nx/2); 15 | R2 = X.^2+Y.^2; 16 | ind = R2<150^2; 17 | Btshiftfilt = Btshift.*ind; 18 | Ffilt = log(abs(Btshiftfilt)+1); % Put FFT on log-scale 19 | subplot(2,2,4), imagesc(Ffilt) % Plot filtered FFT 20 | 21 | Btfilt = ifftshift(Btshiftfilt); 22 | Bfilt = ifft2(Btfilt); 23 | subplot(2,2,3), imagesc(uint8(real(Bfilt))) % Filtered image 24 | 25 | colormap gray 26 | set(gcf,'Position',[100 100 600 800]) -------------------------------------------------------------------------------- /CH02/CH02_SEC06_4_Wavelet.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% Wavelet decomposition (2 level) 6 | n = 2; w = 'db1'; [C,S] = wavedec2(B,n,w); 7 | 8 | % LEVEL 1 9 | A1 = appcoef2(C,S,w,1); % Approximation 10 | [H1 V1 D1] = detcoef2('a',C,S,k); % Details 11 | A1 = wcodemat(A1,128); 12 | H1 = wcodemat(H1,128); 13 | V1 = wcodemat(V1,128); 14 | D1 = wcodemat(D1,128); 15 | 16 | % LEVEL 2 17 | A2 = appcoef2(C,S,w,1); % Approximation 18 | [H2 V2 D2] = detcoef2('a',C,S,k); % Details 19 | A2 = wcodemat(A2,128); 20 | H2 = wcodemat(H2,128); 21 | V2 = wcodemat(V2,128); 22 | D2 = wcodemat(D2,128); 23 | 24 | dec2 = [A2 H2; V2 D2]; 25 | dec1 = [imresize(dec2,size(H1)) H1 ; V1 D1]; 26 | image(dec1); 27 | colormap gray 28 | axis off, axis tight 29 | set(gcf,'Position',[100 100 600 800]) -------------------------------------------------------------------------------- /CH02/CH02_SEC06_4_Wavelet_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% 6 | n = 2; w = 'db1'; [c,s] = wavedec2(B,n,w); 7 | plotwavelet2(c,s,2,'db1',128,'square'); 8 | colormap gray 9 | axis off 10 | axis tight 11 | set(gcf,'Position',[100 100 600 800]) 12 | set(gcf,'PaperPositionMode','auto') 13 | print('-dpng', '-loose', '../figures/2DWavelet_Square'); 14 | 15 | %% 16 | n = 3; w = 'sym2'; [c,s] = wavedec2(B,n,w); 17 | [H1,V1,D1] = detcoef2('all',c,s,1); 18 | A1 = appcoef2(c,s,'haar',1); 19 | V1img = wcodemat(V1,255,'mat',1); 20 | H1img = wcodemat(H1,255,'mat',1); 21 | D1img = wcodemat(D1,255,'mat',1); 22 | A1img = wcodemat(A1,255,'mat',1); 23 | imagesc(A1img) 24 | figure 25 | [H2,V2,D2] = detcoef2('all',c,s,2); 26 | A2 = appcoef2(c,s,'haar',2); 27 | V2img = wcodemat(V2,255,'mat',2); 28 | H2img = wcodemat(H2,255,'mat',2); 29 | D2img = wcodemat(D2,255,'mat',2); 30 | A2img = wcodemat(A2,255,'mat',2); 31 | imagesc(V2img) 32 | figure 33 | colormap(gray); rv = length(gray); 34 | plotwavelet2(c,s,3,'haar',rv,'square') 35 | 36 | figure 37 | subplot(2,2,1) 38 | imagesc(A1img) 39 | 40 | subplot(2,2,2) 41 | imagesc(V1img) 42 | 43 | subplot(2,2,3) 44 | imagesc(H1img) 45 | 46 | subplot(2,2,4) 47 | imagesc(D1img) 48 | -------------------------------------------------------------------------------- /CH02/CH02_SEC06_5_WaveletCompress.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% Wavelet Compression 6 | [C,S] = wavedec2(B,4,'db1'); 7 | Csort = sort(abs(C(:))); % Sort by magnitude 8 | 9 | for keep = [.1 .05 .01 .005] 10 | thresh = Csort(floor((1-keep)*length(Csort))); 11 | ind = abs(C)>thresh; 12 | Cfilt = C.*ind; % Threshold small indices 13 | 14 | % Plot Reconstruction 15 | Arecon=uint8(waverec2(Cfilt,S,'db1')); 16 | figure, imagesc(uint8(Arecon)) 17 | end -------------------------------------------------------------------------------- /CH02/CH02_SEC06_5_WaveletCompress_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | A = imread('../../CH01_SVD/DATA/dog.jpg'); 3 | B = rgb2gray(A); 4 | 5 | %% Wavelet Compression 6 | figure 7 | [C,S] = wavedec2(B,4,'db1'); 8 | 9 | % Zero out all small coefficients and inverse transform 10 | Csort = sort(abs(C(:))); 11 | keepvec = [.1 .05 .01 .005 .002 .001]; 12 | for k=1:6 13 | figure 14 | keep = keepvec(k); 15 | % keep = 0.05; 16 | thresh = Csort(floor((1-keep)*length(Csort))); 17 | ind = abs(C)>thresh; 18 | Cfilt = C.*ind; 19 | 20 | % Plot Reconstruction 21 | Alow=uint8(waverec2(Cfilt,S,'db1')); 22 | imagesc(uint8(Alow)) 23 | colormap gray 24 | 25 | colormap gray 26 | axis off 27 | axis tight 28 | set(gcf,'Position',[100 100 600 800]) 29 | set(gcf,'PaperPositionMode','auto') 30 | % print('-depsc2', '-loose', ['../figures/2DFFT_Compress',num2str(k)]); 31 | print('-dpng', '-loose', ['../figures/2DWavelet_Compress',num2str(k)]); 32 | 33 | end -------------------------------------------------------------------------------- /CH02/rhsBurgers.m: -------------------------------------------------------------------------------- 1 | function dudt = rhsBurgers(t,u,kappa,nu) 2 | uhat = fft(u); 3 | duhat = i*kappa.*uhat; 4 | dduhat = -(kappa.^2).*uhat; 5 | du = ifft(duhat); 6 | ddu = ifft(dduhat); 7 | dudt = -u.*du + nu*ddu; -------------------------------------------------------------------------------- /CH02/rhsHeat.m: -------------------------------------------------------------------------------- 1 | function duhatdt = rhsHeat(t,uhat,kappa,a) 2 | duhatdt = -a^2*(kappa.^2)'.*uhat; % Linear and diagonal -------------------------------------------------------------------------------- /CH02/rhsWave.m: -------------------------------------------------------------------------------- 1 | function duhatdt = rhsWave(t,uhat,kappa,c) 2 | duhatdt = -c*i*kappa.*uhat; -------------------------------------------------------------------------------- /CH02/rhsWaveSpatial.m: -------------------------------------------------------------------------------- 1 | function dudt = rhsWaveSpatial(t,u,kappa,c) 2 | uhat = fft(u); 3 | duhat = i*kappa.*uhat; 4 | du = ifft(duhat); 5 | dudt = -c*du; -------------------------------------------------------------------------------- /CH03/CC2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH03/CC2.mat -------------------------------------------------------------------------------- /CH03/CH03_SEC01_Compress.m: -------------------------------------------------------------------------------- 1 | % Compressed Sensing Examples 2 | % Copyright Brunton, Kutz, Proctor, 2016, All Rights Reserved 3 | % Code by Steven L. Brunton (sbrunton@uw.edu) 4 | 5 | clear all, close all, clc 6 | 7 | figure 8 | A=imread('jelly', 'jpeg'); % Load image 9 | Abw=rgb2gray(A); % Convert image to grayscale 10 | imshow(Abw). % Plot image 11 | 12 | axis off 13 | set(gcf,'PaperPositionMode','auto') 14 | print('-depsc2', '-loose', '../figures/f_chCS_ex01a'); 15 | 16 | %% Compute the FFT of our image using fft2 17 | At=fft2(Abw); 18 | F = log(abs(fftshift(At))+1); % put FFT on log-scale 19 | imshow(mat2gray(F),[]); 20 | 21 | axis off 22 | set(gcf,'PaperPositionMode','auto') 23 | print('-depsc2', '-loose', '../figures/f_chCS_ex01b'); 24 | 25 | %% Zero out all small coefficients and inverse transform 26 | Bt = sort(abs(At(:))); 27 | keep = 0.05; 28 | thresh = Bt(floor((1-keep)*length(Bt))); 29 | ind = abs(At)>thresh; 30 | Atlow = At.*ind; 31 | Flow = log(abs(fftshift(Atlow))+1); % put FFT on log-scale 32 | imshow(mat2gray(Flow),[]); 33 | 34 | axis off 35 | set(gcf,'PaperPositionMode','auto') 36 | print('-depsc2', '-loose', '../figures/f_chCS_ex01c'); 37 | 38 | %% Plot Reconstruction 39 | Alow=uint8(ifft2(Atlow)); 40 | imshow(Alow) 41 | 42 | axis off 43 | set(gcf,'PaperPositionMode','auto') 44 | print('-depsc2', '-loose', '../figures/f_chCS_ex01d'); 45 | 46 | %% 47 | figure 48 | Anew = imresize(Abw,.2); 49 | surf(double(Anew)); 50 | shading flat, view(-168,86) 51 | 52 | 53 | subplot(1,2,1) 54 | surf(double(Anew)); 55 | shading flat, view(0,90) 56 | axis off 57 | 58 | subplot(1,2,2) 59 | surf(double(Anew)); 60 | view(-3,72) 61 | axis off 62 | 63 | set(gcf,'Position',[10 10 1200 700]) 64 | set(gcf,'PaperPositionMode','auto') 65 | print('-depsc2', '-loose', '../figures/f_chCS_ex01e'); -------------------------------------------------------------------------------- /CH03/CH03_SEC03_1_Underdetermined.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % Solve y = Theta * s for "s" 4 | n = 1000; % dimension of s 5 | p = 200; % number of measurements, dim(y) 6 | Theta = randn(p,n); 7 | y = randn(p,1); 8 | 9 | % L1 minimum norm solution s_L1 10 | cvx_begin; 11 | variable s_L1(n); 12 | minimize( norm(s_L1,1) ); 13 | subject to 14 | Theta*s_L1 == y; 15 | cvx_end; 16 | 17 | s_L2 = pinv(Theta)*y; % L2 minimum norm solution s_L2 18 | 19 | 20 | %% 21 | figure 22 | subplot(3,2,1) 23 | plot(s_L1,'b','LineWidth',1.5) 24 | ylim([-.2 .2]), grid on 25 | subplot(3,2,2) 26 | plot(s_L2,'r','LineWidth',1.5) 27 | ylim([-.2 .2]), grid on 28 | subplot(3,2,[3 5]) 29 | [hc,h] = hist(s_L1,[-.1:.01:.1]) 30 | bar(h,hc,'b') 31 | axis([-.1 .1 -50 1000]) 32 | subplot(3,2,[4 6]) 33 | [hc,h] = hist(s_L2,[-.1:.01:.1]) 34 | bar(h,hc,'r') 35 | axis([-.1 .1 -20 400]) 36 | 37 | set(gcf,'Position',[100 100 600 350]) 38 | set(gcf,'PaperPositionMode','auto') 39 | print('-depsc2', '-loose', '../figures/f_chCS_ex03_underdetermined'); -------------------------------------------------------------------------------- /CH03/CH03_SEC05_1_RobustRegression.m: -------------------------------------------------------------------------------- 1 | % Compressed Sensing Examples 2 | % 3 | % Copyright 2016, All Rights Reserved 4 | % Code by Steven L. Brunton (sbrunton@uw.edu) 5 | 6 | % Download and install CVX to run this example: http://cvxr.com/cvx/download/ 7 | 8 | clear all, close all, clc 9 | 10 | x = sort(4*(rand(25,1)-.5)); % Random data from [-2,2] 11 | b = .9*x + .1*randn(size(x)); % Line y=.9x with noise 12 | atrue = x\b; % Least-squares slope (no outliers) 13 | 14 | b(end) = -5.5; % Introduce outlier 15 | acorrupt = x\b; % New slope 16 | 17 | cvx_begin; % L1 optimization to reject outlier 18 | variable aL1; % aL1 is slope to be optimized 19 | minimize( norm(aL1*x-b,1) ); % aL1 is robust 20 | cvx_end; 21 | 22 | hold on 23 | scatter(x(1:end-1),b(1:end-1),'bo') % Data 24 | scatter(x(end),b(end),'ro') % Outlier 25 | xgrid = -2:.01:2; 26 | plot(xgrid,xgrid*atrue,'k--') % L2 fit (no outlier) 27 | plot(xgrid,xgrid*acorrupt,'r--') % L2 fit (outlier) 28 | plot(xgrid,xgrid*aL1,'b--') % L1 fit 29 | axis([-2 2 -6 2]) 30 | 31 | %% 32 | set(gcf,'Position',[100 100 600 400]) 33 | set(gcf,'PaperPositionMode','auto') 34 | print('-depsc2', '-loose', '../figures/f_chCS_robustregression'); -------------------------------------------------------------------------------- /CH03/CH03_SEC05_1_RobustRegression_production.m: -------------------------------------------------------------------------------- 1 | % Compressed Sensing Examples 2 | % 3 | % Copyright 2016, All Rights Reserved 4 | % Code by Steven L. Brunton (sbrunton@uw.edu) 5 | 6 | % Download and install CVX to run this example: http://cvxr.com/cvx/download/ 7 | 8 | clear all, close all, clc 9 | 10 | x = sort(4*(rand(25,1)-.5)); % Random data from [-2,2] 11 | b = .9*x + .1*randn(size(x)); % Line y=.9x with noise 12 | atrue = x\b; % Least-squares slope (no outliers) 13 | 14 | b(end) = -5.5; % Introduce outlier 15 | acorrupt = x\b; % New slope 16 | 17 | cvx_begin; 18 | variable aL1; % aL1 is slope to be optimized 19 | minimize( norm(aL1*x-b,1) ); % minimize L1 error 20 | cvx_end; 21 | 22 | hold on 23 | scatter(x(1:end-1),b(1:end-1),'ko','MarkerFaceColor','b') % Data 24 | scatter(x(end),b(end),'ko','MarkerFaceColor','r') % Outlier 25 | xgrid = -2:.01:2; 26 | plot(xgrid,xgrid*atrue,'k--','LineWidth',1.5) % L2 fit (no outlier) 27 | plot(xgrid,xgrid*acorrupt,'r--','LineWidth',1.2) % L2 fit (outlier) 28 | plot(xgrid,xgrid*aL1,'b--','LineWidth',1.2) % L1 fit 29 | axis([-2 2 -6 2]) 30 | 31 | %% 32 | set(gcf,'Position',[100 100 600 400]) 33 | set(gcf,'PaperPositionMode','auto') 34 | print('-depsc2', '-loose', '../figures/f_chCS_robustregression'); -------------------------------------------------------------------------------- /CH03/CH03_SEC05_2_LASSO.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | A = randn(100,10); % Matrix of possible predictors 4 | x = [0; 0; 1; 0; 0; 0; -1; 0; 0; 0]; % Two nonzero predictors 5 | b = A*x + 2*randn(100,1); % Observations (with noise) 6 | 7 | xL2 = pinv(A)*b 8 | 9 | [XL1 FitInfo] = lasso(A,b,'CV',10); 10 | lassoPlot(XL1,FitInfo,'PlotType','CV') 11 | lassoPlot(XL1,FitInfo,'PlotType','Lambda') 12 | 13 | xL1 = XL1(:,FitInfo.Index1SE) 14 | xL1DeBiased = pinv(A(:,abs(xL1)>0))*b 15 | 16 | 17 | 18 | %% 19 | 20 | figure(1) 21 | set(gcf,'Position',[100 100 600 400]) 22 | set(gcf,'PaperPositionMode','auto') 23 | print('-depsc2', '-loose', '../figures/f_chCS_ex07_lassoA'); 24 | 25 | figure(2) 26 | set(gcf,'Position',[100 100 600 400]) 27 | set(gcf,'PaperPositionMode','auto') 28 | print('-depsc2', '-loose', '../figures/f_chCS_ex07_lassoB'); 29 | -------------------------------------------------------------------------------- /CH03/CH03_SEC07_RPCA.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | addpath('./utils'); 3 | load allFaces.mat 4 | X = faces(:,1:nfaces(1)); 5 | [L,S] = RPCA(X); 6 | 7 | 8 | %% 9 | inds = [3 4 14 15 17 18 19 20 21 32 43]; 10 | for k=[3 4 14 15 17 18 19 20 21 32 43] 11 | k 12 | subplot(2,2,1) 13 | imagesc(reshape(X(:,k),192,168)), colormap gray 14 | subplot(2,2,3) 15 | imagesc(reshape(L(:,k),192,168)), colormap gray 16 | subplot(2,2,4) 17 | imagesc(reshape(S(:,k),192,168)), colormap gray 18 | pause 19 | end -------------------------------------------------------------------------------- /CH03/QRsensors.m: -------------------------------------------------------------------------------- 1 | function C = QRsensors(Psi,p) 2 | [Q,R,pivot] = qr(Psi','vector'); 3 | pivot 4 | C = zeros(p,size(Psi,1)); 5 | for j=1:p 6 | C(j,pivot(j))=1; 7 | end 8 | end -------------------------------------------------------------------------------- /CH03/RPCA.m: -------------------------------------------------------------------------------- 1 | function [L,S] = RPCA(X) 2 | [n1,n2] = size(X); 3 | mu = n1*n2/(4*sum(abs(X(:)))); 4 | lambda = 1/sqrt(max(n1,n2)); 5 | thresh = 1e-7*norm(X,'fro'); 6 | 7 | S = zeros(size(X)); 8 | Y = zeros(size(X)); 9 | count = 0; 10 | while((norm(X-L-S,'fro')>thresh)&&(count<1000)) 11 | L = SVT(X-S+(1/mu)*Y,1/mu); 12 | S = shrink(X-L+(1/mu)*Y,lambda/mu); 13 | Y = Y + mu*(X-L-S); 14 | count = count + 1 15 | end -------------------------------------------------------------------------------- /CH03/SVT.m: -------------------------------------------------------------------------------- 1 | function out = SVT(X,tau) 2 | [U,S,V] = svd(X,'econ'); 3 | out = U*shrink(S,tau)*V'; 4 | end -------------------------------------------------------------------------------- /CH03/jelly.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH03/jelly.jpg -------------------------------------------------------------------------------- /CH03/mustache.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH03/mustache.jpg -------------------------------------------------------------------------------- /CH03/shrink.m: -------------------------------------------------------------------------------- 1 | function out = shrink(X,tau) 2 | out = sign(X).*max(abs(X)-tau,0); 3 | end -------------------------------------------------------------------------------- /CH04/CH04_SEC01_LinearRegression.m: -------------------------------------------------------------------------------- 1 | clear all; close all; 2 | 3 | % The data 4 | x=[1 2 3 4 5 6 7 8 9 10] 5 | y=[0.2 0.5 0.3 3.5 1.0 1.5 1.8 2.0 2.3 2.2] 6 | 7 | p1=fminsearch('fit1',[1 1],[],x,y); 8 | p2=fminsearch('fit2',[1 1],[],x,y); 9 | p3=fminsearch('fit3',[1 1],[],x,y); 10 | 11 | xf=0:0.1:11 12 | y1=polyval(p1,xf); y2=polyval(p2,xf); y3=polyval(p3,xf); 13 | 14 | subplot(2,1,2) 15 | plot(xf,y1,'k'), hold on 16 | plot(xf,y2,'k--','Linewidth',[2]) 17 | plot(xf,y3,'k','Linewidth',[2]) 18 | plot(x,y,'ro','Linewidth',[2]), hold on 19 | 20 | legend('E_\infty','E_1','E_2','location','NorthWest') 21 | set(gca,'Fontsize',[15],'Ylim',[0 4],'Ytick',[0 1 2 3 4],'Xlim',[0 11],'Xtick',[0 2 4 6 8 10]) 22 | 23 | x=[1 2 3 4 5 6 7 8 9 10] 24 | y=[0.2 0.5 0.3 0.7 1.0 1.5 1.8 2.0 2.3 2.2] 25 | 26 | p1=fminsearch('fit1',[1 1],[],x,y); 27 | p2=fminsearch('fit2',[1 1],[],x,y); 28 | p3=fminsearch('fit3',[1 1],[],x,y); 29 | 30 | xf=0:0.1:11 31 | y1=polyval(p1,xf); 32 | y2=polyval(p2,xf); 33 | y3=polyval(p3,xf); 34 | 35 | subplot(2,1,1) 36 | plot(xf,y1,'k'), hold on 37 | plot(xf,y2,'k--','Linewidth',[2]) 38 | plot(xf,y3,'k','Linewidth',[2]) 39 | 40 | plot(x,y,'ro','Linewidth',[2]), hold on 41 | 42 | legend('E_\infty','E_1','E_2','location','NorthWest') 43 | set(gca,'Fontsize',[15],'Ylim',[0 4],'Ytick',[0 1 2 3 4],'Xlim',[0 11],'Xtick',[0 2 4 6 8 10]) 44 | -------------------------------------------------------------------------------- /CH04/CH04_SEC02_2_DeltaSearch.m: -------------------------------------------------------------------------------- 1 | function mindel=delsearch(del,x,y,dfx,dfy,X,Y,F) 2 | x0=x-del*dfx; 3 | y0=y-del*dfy; 4 | mindel=interp2(X,Y,F,x0,y0); 5 | -------------------------------------------------------------------------------- /CH04/CH04_SEC03_1_OverUnderDetermined.m: -------------------------------------------------------------------------------- 1 | clear all; close all; 2 | figure(1) 3 | % underdetermined 4 | n=20; m=100 5 | A=rand(n,m); b=rand(n,1); 6 | 7 | cvx_begin; 8 | variable x2(m) 9 | minimize( norm(x2,2) ); 10 | subject to 11 | A*x2 == b; 12 | cvx_end; 13 | 14 | cvx_begin; 15 | variable x1(m) 16 | minimize( norm(x1,1) ); 17 | subject to 18 | A*x1 == b; 19 | cvx_end; 20 | 21 | subplot(3,1,1), bar(x2) 22 | subplot(3,1,2), bar(x1) 23 | subplot(3,2,5), hist(x2,40) 24 | subplot(3,2,6), hist(x1,40) 25 | 26 | subplot(3,1,1), axis([0 100 -.2 .2]), subplot(3,1,2), axis([0 100 -.4 .4]) 27 | subplot(3,2,5), axis([-.15 .15 0 82]), set(gca,'Ytick',[0 40 80],'Xtick',[-0.1 0 0.1]) 28 | h = findobj(gca,'Type','patch'); h.FaceColor = [0.6 0.6 0.6]; h.EdgeColor = 'k'; 29 | subplot(3,2,6), axis([-.15 .15 0 82]), set(gca,'Ytick',[0 40 80],'Xtick',[-0.1 0 0.1]) 30 | h = findobj(gca,'Type','patch'); h.FaceColor = [0.6 0.6 0.6]; h.EdgeColor = 'k'; 31 | 32 | %% 33 | clear A 34 | clear b 35 | clear x 36 | figure(2) 37 | 38 | % overdetermined 39 | n=500; m=100; 40 | A=rand(n,m); 41 | b=rand(n,1); 42 | xdag=pinv(A)*b; 43 | 44 | lam=[0 0.1 0.5]; 45 | for j=1:3 46 | 47 | cvx_begin; 48 | variable x(m) 49 | minimize( norm(A*x-b,2) + lam(j)*norm(x,1) ); 50 | cvx_end; 51 | 52 | subplot(4,1,j),bar(x) 53 | subplot(4,3,9+j), hist(x,20) 54 | end 55 | 56 | 57 | for j=1:3 58 | 59 | 60 | subplot(4,1,j), axis([0 100 -.15 .15]) 61 | subplot(4,3,9+j), axis([-.15 .15 0 80]), set(gca,'Ytick',[0 40 80]) 62 | h = findobj(gca,'Type','patch'); 63 | h.FaceColor = [0.6 0.6 0.6]; 64 | h.EdgeColor = 'k'; 65 | end 66 | 67 | 68 | %% 69 | % matrix overdetermined system 70 | 71 | clear A 72 | clear b 73 | clear x 74 | figure(3) 75 | 76 | % overdetermined 77 | n=300; m=60; p=20; 78 | A=rand(n,m); b=rand(n,p); 79 | 80 | lam=[0 0.1]; 81 | for j=1:2 82 | cvx_begin; 83 | variable x(m,p) 84 | minimize(norm(A*x-b,2) + lam(j)*norm(x,1)); 85 | cvx_end; 86 | subplot(2,1,j), pcolor(x.'), colormap(hot), colorbar 87 | end 88 | 89 | %% 90 | subplot(2,1,1), set(gca,'Fontsize',[15],'Xtick',[1 20 40 60],'Ytick',[1 10 20]) 91 | subplot(2,1,2), set(gca,'Fontsize',[15],'Xtick',[1 20 40 60],'Ytick',[1 10 20]) 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /CH04/CH04_SEC03_1_OverUnderDetermined_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; 2 | figure(1) 3 | % underdetermined 4 | n=20; m=100 5 | A=rand(n,m); b=rand(n,1); 6 | 7 | cvx_begin; 8 | variable x2(m) 9 | minimize( norm(x2,2) ); 10 | subject to 11 | A*x2 == b; 12 | cvx_end; 13 | 14 | cvx_begin; 15 | variable x1(m) 16 | minimize( norm(x1,1) ); 17 | subject to 18 | A*x1 == b; 19 | cvx_end; 20 | 21 | subplot(3,1,1), bar(x2,'FaceColor',[.6 .6 .6],'EdgeColor','k') 22 | subplot(3,1,2), bar(x1,'FaceColor',[.6 .6 .6],'EdgeColor','k') 23 | subplot(3,2,5), hist(x2,40) 24 | subplot(3,2,6), hist(x1,40) 25 | 26 | subplot(3,1,1), axis([0 100 -.2 .2]), subplot(3,1,2), axis([0 100 -.4 .4]) 27 | subplot(3,2,5), axis([-.15 .15 0 82]), set(gca,'Ytick',[0 40 80],'Xtick',[-0.1 0 0.1]) 28 | h = findobj(gca,'Type','patch'); h.FaceColor = [0.6 0.6 0.6]; h.EdgeColor = 'k'; 29 | subplot(3,2,6), axis([-.15 .15 0 82]), set(gca,'Ytick',[0 40 80],'Xtick',[-0.1 0 0.1]) 30 | h = findobj(gca,'Type','patch'); h.FaceColor = [0.6 0.6 0.6]; h.EdgeColor = 'k'; 31 | 32 | %% 33 | clear A 34 | clear b 35 | clear x 36 | figure(2) 37 | 38 | % overdetermined 39 | n=500; m=100; 40 | A=rand(n,m); 41 | b=rand(n,1); 42 | xdag=pinv(A)*b; 43 | 44 | lam=[0 0.1 0.5]; 45 | for j=1:3 46 | 47 | cvx_begin; 48 | variable x(m) 49 | minimize( norm(A*x-b,2) + lam(j)*norm(x,1) ); 50 | cvx_end; 51 | 52 | subplot(4,1,j),bar(x,'FaceColor',[.6 .6 .6],'EdgeColor','k') 53 | subplot(4,3,9+j), hist(x,20) 54 | end 55 | 56 | 57 | for j=1:3 58 | 59 | 60 | subplot(4,1,j), axis([0 100 -.15 .15]) 61 | subplot(4,3,9+j), axis([-.15 .15 0 80]), set(gca,'Ytick',[0 40 80]) 62 | h = findobj(gca,'Type','patch'); 63 | h.FaceColor = [0.6 0.6 0.6]; 64 | h.EdgeColor = 'k'; 65 | end 66 | 67 | 68 | %% 69 | % matrix overdetermined system 70 | 71 | clear A 72 | clear b 73 | clear x 74 | figure(3) 75 | 76 | % overdetermined 77 | n=300; m=60; p=20; 78 | A=rand(n,m); b=rand(n,p); 79 | 80 | lam=[0 0.1]; 81 | for j=1:2 82 | cvx_begin; 83 | variable x(m,p) 84 | minimize(norm(A*x-b,2) + lam(j)*norm(x,1)); 85 | cvx_end; 86 | subplot(2,1,j), pcolor(x.'), colormap(hot), colorbar 87 | end 88 | 89 | %% 90 | subplot(2,1,1), set(gca,'Fontsize',[15],'Xtick',[1 20 40 60],'Ytick',[1 10 20]) 91 | subplot(2,1,2), set(gca,'Fontsize',[15],'Xtick',[1 20 40 60],'Ytick',[1 10 20]) 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /CH04/CH04_SEC04_1_CompareRegression.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; L=4; 4 | x=linspace(0,L,n); 5 | f=(x.^2).'; % parabola with 100 data points 6 | 7 | M=20; % polynomial degree 8 | for j=1:M 9 | phi(:,j)=(x.').^(j-1); % build matrix A 10 | end 11 | 12 | for j=1:4 13 | fn=(x.^2+0.1*randn(1,n)).'; 14 | an=pinv(phi)*fn; fna=phi*an; % least-square fit 15 | En=norm(f-fna)/norm(f); 16 | subplot(4,2,4+j),bar(an) 17 | end 18 | 19 | subplot(2,1,1), plot(x,f,'k'), hold on 20 | 21 | %% different regressions 22 | subplot(2,1,1) 23 | lambda=0.1; phi2=phi(:,2:end); 24 | for jj=1:100 25 | f=(x.^2+0.2*randn(1,n)).'; 26 | a1=pinv(phi)*f; f1=phi*a1; E1(jj)=norm(f-f1)/norm(f); 27 | a2=phi\f; f2=phi*a2; E2(jj)=norm(f-f2)/norm(f); 28 | [a3,stats]=lasso(phi,f,'Lambda',lambda); f3=phi*a3; E3(jj)=norm(f-f3)/norm(f); 29 | [a4,stats]=lasso(phi,f,'Lambda',lambda,'Alpha',0.8); f4=phi*a4; E4(jj)=norm(f-f4)/norm(f); 30 | a5=robustfit(phi2,f);f5=phi*a5;E5(jj)=norm(f-f5)/norm(f); 31 | a6=ridge(f,phi2,0.5,0);f6=phi*a6;E6(jj)=norm(f-f6)/norm(f); 32 | 33 | A1(:,jj)=a1;A2(:,jj)=a2;A3(:,jj)=a3;A4(:,jj)=a4;A5(:,jj)=a5;A6(:,jj)=a6; 34 | plot(x,f), hold on 35 | end 36 | Err=[E1; E2; E3; E4; E5; E6]; 37 | Err2=[E1; E2; E3; E4; E5]; 38 | 39 | figure(2) 40 | subplot(3,2,1), boxplot(A1.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 41 | subplot(3,2,2), boxplot(A2.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 42 | subplot(3,2,3), boxplot(A3.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 43 | subplot(3,2,4), boxplot(A4.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 44 | subplot(3,2,5), boxplot(A5.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 45 | subplot(3,2,6), boxplot(A6.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 46 | 47 | figure(3) 48 | subplot(3,3,1), boxplot(Err.'), axis([0 7 0.02 0.07]) 49 | 50 | %% 51 | clear phi 52 | M=10; 53 | En=zeros(100,M); 54 | for jj=1:M 55 | for j=1:jj 56 | phi(:,j)=(x.').^(j-1); 57 | end 58 | f=(x.^2).'; 59 | for j=1:100 60 | fn=(x.^2+0.1*randn(1,n)).'; 61 | an=pinv(phi)*fn; fna=phi*an; 62 | En(j,jj)=norm(f-fna)/norm(f); 63 | end 64 | end 65 | 66 | figure(3) 67 | subplot(3,3,2) 68 | boxplot(En) 69 | 70 | subplot(3,3,3) 71 | boxplot(En), axis([0.5 10.5 0 0.008]) 72 | -------------------------------------------------------------------------------- /CH04/CH04_SEC04_1_CompareRegression_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; L=4; 4 | x=linspace(0,L,n); 5 | f=(x.^2).'; % parabola with 100 data points 6 | 7 | M=20; % polynomical degree 8 | for j=1:M 9 | phi(:,j)=(x.').^(j-1); % build matrix A 10 | end 11 | 12 | for j=1:4 13 | fn=(x.^2+0.1*randn(1,n)).'; 14 | an=pinv(phi)*fn; fna=phi*an; % least-square fit 15 | En=norm(f-fna)/norm(f); 16 | subplot(4,2,4+j),bar(an,'FaceColor',[.6 .6 .6],'EdgeColor','k') 17 | end 18 | 19 | subplot(2,1,1), plot(x,f,'k'), hold on 20 | 21 | %% different regressions 22 | subplot(2,1,1) 23 | lambda=0.1; phi2=phi(:,2:end); 24 | for jj=1:100 25 | f=(x.^2+0.2*randn(1,n)).'; 26 | a1=pinv(phi)*f; f1=phi*a1; E1(jj)=norm(f-f1)/norm(f); 27 | a2=phi\f; f2=phi*a2; E2(jj)=norm(f-f2)/norm(f); 28 | [a3,stats]=lasso(phi,f,'Lambda',lambda); f3=phi*a3; E3(jj)=norm(f-f3)/norm(f); 29 | [a4,stats]=lasso(phi,f,'Lambda',lambda,'Alpha',0.8); f4=phi*a4; E4(jj)=norm(f-f4)/norm(f); 30 | a5=robustfit(phi2,f);f5=phi*a5;E5(jj)=norm(f-f5)/norm(f); 31 | a6=ridge(f,phi2,0.5,0);f6=phi*a6;E6(jj)=norm(f-f6)/norm(f); 32 | 33 | A1(:,jj)=a1;A2(:,jj)=a2;A3(:,jj)=a3;A4(:,jj)=a4;A5(:,jj)=a5;A6(:,jj)=a6; 34 | plot(x,f), hold on 35 | end 36 | Err=[E1; E2; E3; E4; E5; E6]; 37 | Err2=[E1; E2; E3; E4; E5]; 38 | 39 | figure(2) 40 | subplot(3,2,1), boxplot(A1.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 41 | subplot(3,2,2), boxplot(A2.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 42 | subplot(3,2,3), boxplot(A3.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 43 | subplot(3,2,4), boxplot(A4.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 44 | subplot(3,2,5), boxplot(A5.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 45 | subplot(3,2,6), boxplot(A6.'), set(gca,'Xtick',1:20,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20'}) 46 | 47 | figure(3) 48 | subplot(3,3,1), boxplot(Err.'), axis([0 7 0.02 0.07]) 49 | 50 | %% 51 | clear phi 52 | M=10; 53 | En=zeros(100,M); 54 | for jj=1:M 55 | for j=1:jj 56 | phi(:,j)=(x.').^(j-1); 57 | end 58 | f=(x.^2).'; 59 | for j=1:100 60 | fn=(x.^2+0.1*randn(1,n)).'; 61 | an=pinv(phi)*fn; fna=phi*an; 62 | En(j,jj)=norm(f-fna)/norm(f); 63 | end 64 | end 65 | 66 | figure(3) 67 | subplot(3,3,2) 68 | boxplot(En) 69 | 70 | subplot(3,3,3) 71 | boxplot(En), axis([0.5 10.5 0 0.008]) 72 | -------------------------------------------------------------------------------- /CH04/CH04_SEC05_0_Fig4p16_Pareto.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | x=0.2:0.1:5; 4 | y=1./(x); 5 | 6 | x2=0.2:0.1:5; n=length(x2); 7 | y2=1./(x2) + 0.5*randn(1,n); 8 | 9 | y3=y2+2*rand(5,1)+1 10 | 11 | 12 | fill([0.5 1.4 1.4 0.5],[0.4 0.4 2 2],[0.8 0.8 0.8]) 13 | hold on 14 | 15 | plot(x,y,'k','Linewidth',[2]) 16 | axis([0.2 4 0 5.5]) 17 | set(gca,'Xtick',[],'Ytick',[]) 18 | 19 | plot(x2,y3,'o','Linewidth',[1],'MarkerEdgeColor','k','MarkerFaceColor',[0 1 0.2],'MarkerSize',8) 20 | plot(x2,y2,'o','Linewidth',[1],'MarkerEdgeColor','k','MarkerFaceColor',[0.9 0 1],'MarkerSize',8) 21 | -------------------------------------------------------------------------------- /CH04/CH04_SEC05_1_CrossValidate.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=200; L=8; 4 | x=linspace(0,L,n); 5 | x1=x(1:100); % train 6 | x2=x(101:200); % test 7 | n1=length(x1); 8 | n2=length(x2); 9 | ftrain=(x1.^2).'; % train parabola x=[0,4] 10 | ftest=(x2.^2).'; % test parbola x=[4,5] 11 | figure(1), subplot(3,1,1), 12 | plot(x1,ftrain,'r',x2,ftest,'b','Linewidth',[2]) 13 | legend('','','Location','Northwest') 14 | legend boxoff 15 | 16 | M=30; % number of model terms 17 | Eni=zeros(100,M); Ene=zeros(100,M); 18 | for jj=1:M 19 | for j=1:jj 20 | phi_i(:,j)=(x1.').^(j-1); % interpolation key 21 | phi_e(:,j)=(x2.').^(j-1); % extrapolation key 22 | end 23 | 24 | f=(x.^2).'; 25 | for j=1:100 26 | fni=(x1.^2+0.1*randn(1,n1)).'; % interpolation 27 | fne=(x2.^2+0.1*randn(1,n2)).'; % extrapolation 28 | 29 | ani=pinv(phi_i)*fni; fnai=phi_i*ani; 30 | Eni(j,jj)=norm(ftrain-fnai)/norm(ftrain); 31 | 32 | fnae=phi_e*ani; % use loadings from x in [0,4] 33 | Ene(j,jj)=norm(ftest-fnae)/norm(ftest); 34 | end 35 | end 36 | 37 | subplot(3,2,3), boxplot(Eni), axis([0.5 30.5 0 0.7]), set(gca,'Xlim',[0.5 30.5],'Xtick',1:30,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20','','','','','25','','','','','30'}) 38 | subplot(3,2,4), boxplot(Eni), axis([0.5 30.5 0 0.02]), set(gca,'Xlim',[0.5 30.5],'Xtick',1:30,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20','','','','','25','','','','','30'}) 39 | subplot(3,2,5), boxplot(Ene), set(gca,'Xlim',[0.5 30.5],'Xtick',1:30,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20','','','','','25','','','','','30'}) 40 | subplot(3,2,6), boxplot(log(Ene+1)), axis([0.5 30.5 0 30]), set(gca,'Xtick',1:30,'Xticklabel',{'1','','','','5','','','','','10','','','','','15','','','','','20','','','','','25','','','','','30'}) 41 | 42 | -------------------------------------------------------------------------------- /CH04/CH04_SEC06_1_kFoldValidation.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; L=4; 4 | x=linspace(0,L,n); 5 | f=(x.^2).'; % parabola with 100 data points 6 | 7 | M=21; % polynomial degree 8 | for j=1:M 9 | phi(:,j)=(x.').^(j-1); % build matrix A 10 | end 11 | 12 | trials=[2 10 100]; 13 | for j=1:3 14 | for jj=1:trials(j) 15 | f=(x.^2+0.2*randn(1,n)).'; 16 | a1=pinv(phi)*f; f1=phi*a1; E1(jj)=norm(f-f1)/norm(f); 17 | a2=phi\f; f2=phi*a2; E2(jj)=norm(f-f2)/norm(f); 18 | [a3,stats]=lasso(phi,f,'Lambda',0.1); f3=phi*a3; E3(jj)=norm(f-f3)/norm(f); 19 | A1(:,jj)=a1; A2(:,jj)=a2; A3(:,jj)=a3; 20 | end 21 | A1m=mean(A1.'); A2m=mean(A2.'); A3m=mean(A3.'); 22 | Err=[E1; E2; E3]; 23 | 24 | subplot(3,3,j), bar(A1m), axis([0 21 -1 1.2]) 25 | subplot(3,3,3+j), bar(A2m), axis([0 21 -1 1.2]) 26 | subplot(3,3,6+j), bar(A3m), axis([0 21 -1 1.2]) 27 | end 28 | 29 | 30 | %subplot(3,3,j), bar(A1m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 31 | %subplot(3,3,3+j), bar(A2m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 32 | %subplot(3,3,6+j), bar(A3m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | %% 43 | figure(2) 44 | 45 | Atot=[A1m; A2m; A3m]; % average loadings of three methods 46 | Atot2=(Atot>0.2).*Atot; % threshold 47 | Atot3=[Atot; Atot2]; % combine both thresholded and not 48 | 49 | figure(3), bar3(Atot.') 50 | figure(4), bar3(Atot2.') 51 | 52 | n=200; L=8; 53 | x=linspace(0,L,n); 54 | x1=x(1:100); % train (interpolation) 55 | x2=x(101:200); % test (extrapolation) 56 | 57 | ftrain=(x1.^2).'; % interpolated parabola x=[0,4] 58 | ftest=(x2.^2).'; % extrapolated parbola x=[4,5] 59 | 60 | for j=1:M 61 | phi_i(:,j)=(x1.').^(j-1); % interpolation key 62 | phi_e(:,j)=(x2.').^(j-1); % extrapolation key 63 | end 64 | 65 | for jj=1:6 % compute inter/extra-polation scores 66 | ani=Atot3(jj,:).'; 67 | fnai=phi_i*ani; 68 | Eni(jj)=norm(ftrain-fnai)/norm(ftrain); 69 | fnae=phi_e*ani; 70 | Ene(jj)=norm(ftest-fnae)/norm(ftest); 71 | end 72 | 73 | figure(5) 74 | subplot(3,2,1), bar(Eni) 75 | subplot(3,2,2), bar(Ene) 76 | 77 | subplot(3,2,3), bar(Eni), axis([0 7 0 0.01]) 78 | subplot(3,2,4), bar(Ene), axis([0 7 0 0.1]) 79 | 80 | figure(3) 81 | legend('','','','Location','NorthEast') 82 | legend boxoff 83 | 84 | %% Dendrograms 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /CH04/CH04_SEC06_1_kFoldValidation_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; L=4; 4 | x=linspace(0,L,n); 5 | f=(x.^2).'; % parabola with 100 data points 6 | 7 | M=21; % polynomial degree 8 | for j=1:M 9 | phi(:,j)=(x.').^(j-1); % build matrix A 10 | end 11 | 12 | trials=[2 10 100]; 13 | for j=1:3 14 | for jj=1:trials(j) 15 | f=(x.^2+0.2*randn(1,n)).'; 16 | a1=pinv(phi)*f; f1=phi*a1; E1(jj)=norm(f-f1)/norm(f); 17 | a2=phi\f; f2=phi*a2; E2(jj)=norm(f-f2)/norm(f); 18 | [a3,stats]=lasso(phi,f,'Lambda',0.1); f3=phi*a3; E3(jj)=norm(f-f3)/norm(f); 19 | A1(:,jj)=a1; A2(:,jj)=a2; A3(:,jj)=a3; 20 | end 21 | A1m=mean(A1.'); A2m=mean(A2.'); A3m=mean(A3.'); 22 | Err=[E1; E2; E3]; 23 | 24 | subplot(3,3,j), bar(A1m), axis([0 21 -1 1.2]) 25 | subplot(3,3,3+j), bar(A2m), axis([0 21 -1 1.2]) 26 | subplot(3,3,6+j), bar(A3m), axis([0 21 -1 1.2]) 27 | end 28 | 29 | 30 | %subplot(3,3,j), bar(A1m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 31 | %subplot(3,3,3+j), bar(A2m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 32 | %subplot(3,3,6+j), bar(A3m,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 21 -1 1.2]) 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | %% 43 | figure(2) 44 | 45 | Atot=[A1m; A2m; A3m]; % average loadings of three methods 46 | Atot2=(Atot>0.2).*Atot; % threshold 47 | Atot3=[Atot; Atot2]; % combine both thresholded and not 48 | 49 | figure(3), bar3(Atot.'), axis([0 4 0 20 0 1]), view(-70,38), set(gca,'Xtick',[],'Fontsize',[18]) 50 | figure(4), bar3(Atot2.'), axis([0 4 0 20 0 1]), view(-70,38), set(gca,'Xtick',[],'Fontsize',[18]) 51 | 52 | n=200; L=8; 53 | x=linspace(0,L,n); 54 | x1=x(1:100); % train (interpolation) 55 | x2=x(101:200); % test (extrapolation) 56 | 57 | ftrain=(x1.^2).'; % interpolated parabola x=[0,4] 58 | ftest=(x2.^2).'; % extrapolated parbola x=[4,5] 59 | 60 | for j=1:M 61 | phi_i(:,j)=(x1.').^(j-1); % interpolation key 62 | phi_e(:,j)=(x2.').^(j-1); % extrapolation key 63 | end 64 | 65 | for jj=1:6 % compute inter/extra-polation scores 66 | ani=Atot3(jj,:).'; 67 | fnai=phi_i*ani; 68 | Eni(jj)=norm(ftrain-fnai)/norm(ftrain); 69 | fnae=phi_e*ani; 70 | Ene(jj)=norm(ftest-fnae)/norm(ftest); 71 | end 72 | 73 | figure(5) 74 | subplot(3,2,1), bar(Eni) 75 | subplot(3,2,2), bar(Ene) 76 | 77 | subplot(3,2,3), bar(Eni), axis([0 7 0 0.01]) 78 | subplot(3,2,4), bar(Ene), axis([0 7 0 0.1]) 79 | 80 | figure(3) 81 | legend('','','','Location','NorthEast') 82 | legend boxoff 83 | 84 | %% Dendrograms 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /CH04/CH04_SEC07_2_RegressAIC_BIC.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | rng(1); % For random data reproducibility 4 | T = 100; % Sample size 5 | DGP = arima('Constant',-4,'AR',[0.2, 0.5],'Variance',2); 6 | y = simulate(DGP,T); 7 | 8 | EstMdl1 = arima('ARLags',1); 9 | EstMdl2 = arima('ARLags',1:2); 10 | EstMdl3 = arima('ARLags',1:3); 11 | 12 | logL = zeros(3,1); % Preallocate loglikelihood vector 13 | [~,~,logL(1)] = estimate(EstMdl1,y,'print',false); 14 | [~,~,logL(2)] = estimate(EstMdl2,y,'print',false); 15 | [~,~,logL(3)] = estimate(EstMdl3,y,'print',false); 16 | 17 | [aic,bic] = aicbic(logL, [3; 4; 5], T*ones(3,1)) -------------------------------------------------------------------------------- /CH04/fit1.m: -------------------------------------------------------------------------------- 1 | function E=fit1(x0,x,y) 2 | E=max(abs( x0(1)*x+x0(2)-y )); -------------------------------------------------------------------------------- /CH04/fit2.m: -------------------------------------------------------------------------------- 1 | function E=fit2(x0,x,y) 2 | E=sum(abs( x0(1)*x+x0(2)-y )); -------------------------------------------------------------------------------- /CH04/fit3.m: -------------------------------------------------------------------------------- 1 | function E=fit3(x0,x,y) 2 | E=sum(abs( x0(1)*x+x0(2)-y ).^2 ); -------------------------------------------------------------------------------- /CH05/AllMethodsCatsDogs.m: -------------------------------------------------------------------------------- 1 | % here's part from class 2 | % load data ... 3 | % do wavelets .. 4 | % take svd ... 5 | data = [5+ 2*randn(32*32, 80), .1*randn(32*32, 80)]; 6 | [u,s,v] = svd(data,0); 7 | 8 | % *** so replace above with the real cat and dog data *** 9 | 10 | 11 | % set up 12 | xdog=v(1:80,2:4); 13 | xcat=v(81:160,2:4); 14 | xtrain=[xdog(1:50,:);xcat(1:50,:)]; 15 | xtest=[xdog(51:80,:);xcat(51:80,:)]; 16 | Ctrain=[ones(50,1); 2*ones(50,1)]; 17 | Ctest=[ones(30,1); 2*ones(30,1)]; 18 | 19 | % k-NN 20 | knn = fitcknn(xtrain, Ctrain); 21 | predKNN = predict(knn,xtest); 22 | 23 | cMatKNN = confusionmat(Ctest, predKNN) 24 | errorKNN = sum(abs(predKNN-Ctest))/60*100 25 | 26 | % LDA 27 | predLDA = classify(xtest, xtrain, Ctrain); 28 | 29 | cMatLDA = confusionmat(Ctest, predLDA) 30 | errorLDA = sum(abs(predLDA-Ctest))/60*100 31 | 32 | 33 | % Naive Bayes 34 | nb=fitNaiveBayes(xtrain,Ctrain); 35 | predNB=nb.predict(xtest); 36 | 37 | cMatNB=confusionmat(Ctest, predNB) 38 | errorNB=sum(abs(predNB-Ctest))/60*100 39 | 40 | % SVM 41 | svm = svmtrain(xtrain,Ctrain); 42 | predSVM = svmclassify(svm,xtest); 43 | 44 | cMatSVM=confusionmat(Ctest,predSVM) 45 | errorSVM=sum(abs(predSVM-Ctest))/60*100 46 | 47 | % AdaBoost 48 | % pick AdaBoost, 100 classifiers, weak learners are Discriminants 49 | ab = fitensemble(xtrain,Ctrain,'AdaBoostM1',100,'Discriminant'); 50 | predAB = predict(ab,xtest); 51 | 52 | cMatAB = confusionmat(Ctest,predAB) 53 | errorAB = sum(abs(predAB-Ctest))/60*100 54 | 55 | % EM / Gaussian mixture models 56 | gm = fitgmdist(xtrain, 2); % choose number of clusters 57 | predKM = cluster(gm,xtest); % can use clustering to classify new data 58 | 59 | cMatKM = confusionmat(Ctest,predKM) 60 | errorKM = sum(abs(predKM-Ctest))/60*100 61 | 62 | % some cool ways to view these clustering results are here: 63 | % http://www.mathworks.com/help/stats/gaussian-mixture-models.html#brajyl2 64 | 65 | % k-means 66 | km = kmeans(xtrain, 2); % choose number of clusters 67 | % I don't think they have a built-in way to evaluate this on new data? 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /CH05/AllMethodsCatsDogs2012.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | % here's part from class 4 | % load data ... 5 | % do wavelets .. 6 | % take svd ... 7 | data = [5+ 2*randn(32*32, 80), .1*randn(32*32, 80)]; 8 | [u,s,v] = svd(data,0); 9 | 10 | % *** so replace above with the real cat and dog data *** 11 | 12 | 13 | % set up 14 | xdog=v(1:80,2:4); 15 | xcat=v(81:160,2:4); 16 | xtrain=[xdog(1:50,:);xcat(1:50,:)]; 17 | xtest=[xdog(51:80,:);xcat(51:80,:)]; 18 | Ctrain=[ones(50,1); 2*ones(50,1)]; 19 | Ctest=[ones(30,1); 2*ones(30,1)]; 20 | 21 | % k-NN - 2014 22 | %knn = fitcknn(xtrain, Ctrain); 23 | %predKNN = predict(knn,xtest); 24 | 25 | % k-NN - 2012b 26 | idx=knnsearch(xtrain,xtest,'K',1) 27 | 28 | 29 | 30 | 31 | %cMatKNN = confusionmat(Ctest, predKNN) 32 | %errorKNN = sum(abs(predKNN-Ctest))/60*100 33 | 34 | % LDA 35 | %predLDA = classify(xtest, xtrain, Ctrain); 36 | 37 | %cMatLDA = confusionmat(Ctest, predLDA) 38 | %errorLDA = sum(abs(predLDA-Ctest))/60*100 39 | 40 | 41 | % Naive Bayes - 2014 42 | %nb=fitNaiveBayes(xtrain,Ctrain); 43 | %predNB=nb.predict(xtest); 44 | 45 | % 2012b 46 | nb=NaiveBayes.fit(xtrain,Ctrain); 47 | predNB=nb.predict(xtest); 48 | 49 | 50 | cMatNB=confusionmat(Ctest, predNB) 51 | errorNB=sum(abs(predNB-Ctest))/60*100 52 | 53 | 54 | % EM / Gaussian mixture models 55 | %gm = fitgmdist(xtrain, 2); % choose number of clusters 56 | gm = gmdistribution.fit(xtrain, 2); % choose number of clusters 57 | predKM = cluster(gm,xtest); % can use clustering to classify new data 58 | 59 | cMatKM = confusionmat(Ctest,predKM) 60 | errorKM = sum(abs(predKM-Ctest))/60*100 61 | 62 | break 63 | 64 | % SVM 65 | svm = svmtrain(xtrain,Ctrain); 66 | predSVM = svmclassify(svm,xtest); 67 | 68 | cMatSVM=confusionmat(Ctest,predSVM) 69 | errorSVM=sum(abs(predSVM-Ctest))/60*100 70 | 71 | % AdaBoost 72 | % pick AdaBoost, 100 classifiers, weak learners are Discriminants 73 | ab = fitensemble(xtrain,Ctrain,'AdaBoostM1',100,'Discriminant'); 74 | predAB = predict(ab,xtest); 75 | 76 | cMatAB = confusionmat(Ctest,predAB) 77 | errorAB = sum(abs(predAB-Ctest))/60*100 78 | 79 | 80 | % some cool ways to view these clustering results are here: 81 | % http://www.mathworks.com/help/stats/gaussian-mixture-models.html#brajyl2 82 | 83 | % k-means 84 | km = kmeans(xtrain, 2); % choose number of clusters 85 | % I don't think they have a built-in way to evaluate this on new data? 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /CH05/CH05_SEC04_1_Dendrogram.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | % training & testing set sizes 4 | n1=100; % training set size 5 | n2=50; % test set size 6 | 7 | % random ellipse 1 centered at (0,0) 8 | x=randn(n1+n2,1); 9 | y=0.5*randn(n1+n2,1); 10 | 11 | % random ellipse 2 centered at (1,-2) 12 | x2=randn(n1+n2,1)+2; 13 | y2=0.2*randn(n1+n2,1)-2; 14 | 15 | % rotate ellipse 2 by theta 16 | theta=pi/4; 17 | A=[cos(theta) -sin(theta); sin(theta) cos(theta)]; 18 | x3=A(1,1)*x2+A(1,2)*y2; 19 | y3=A(2,1)*x2+A(2,2)*y2; 20 | 21 | subplot(2,2,1) 22 | plot(x(1:n1),y(1:n1),'ro','Linewidth',[1],'MarkerEdgeColor','k',... 23 | 'MarkerFaceColor',[0 1 0.2],... 24 | 'MarkerSize',8), hold on % [0.49 1 .63] 25 | plot(x3(1:n1),y3(1:n1),'bo','Linewidth',[1],'MarkerEdgeColor','k',... 26 | 'MarkerFaceColor',[0.9 0 1],... 27 | 'MarkerSize',8) 28 | 29 | % training set: first 200 of 240 points 30 | X1=[x3(1:n1) y3(1:n1)]; 31 | X2=[x(1:n1) y(1:n1)]; 32 | 33 | figure(1) 34 | Y3=[X1(1:50,:); X2(1:50,:)]; 35 | Y2 = pdist(Y3,'euclidean'); 36 | Z = linkage(Y2,'average'); 37 | thresh=0.85*max(Z(:,3)); 38 | [H,T,O]=dendrogram(Z,100,'ColorThreshold',thresh); 39 | axis off 40 | 41 | figure(5) 42 | bar(O), hold on 43 | plot([0 100],[50 50],'r:','Linewidth',2) 44 | plot([50.5 50.5],[0 100],'r:','Linewidth',2) 45 | 46 | 47 | 48 | 49 | 50 | thresh=0.25*max(Z(:,3)); 51 | [H,T,O]=dendrogram(Z,100,'ColorThreshold',thresh); 52 | axis off 53 | -------------------------------------------------------------------------------- /CH05/CH05_SEC04_1_Dendrogram_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | % training & testing set sizes 4 | n1=100; % training set size 5 | n2=50; % test set size 6 | 7 | % random ellipse 1 centered at (0,0) 8 | x=randn(n1+n2,1); 9 | y=0.5*randn(n1+n2,1); 10 | 11 | % random ellipse 2 centered at (1,-2) 12 | x2=randn(n1+n2,1)+2; 13 | y2=0.2*randn(n1+n2,1)-2; 14 | 15 | % rotate ellipse 2 by theta 16 | theta=pi/4; 17 | A=[cos(theta) -sin(theta); sin(theta) cos(theta)]; 18 | x3=A(1,1)*x2+A(1,2)*y2; 19 | y3=A(2,1)*x2+A(2,2)*y2; 20 | 21 | subplot(2,2,1) 22 | plot(x(1:n1),y(1:n1),'ro','Linewidth',[1],'MarkerEdgeColor','k',... 23 | 'MarkerFaceColor',[0 1 0.2],... 24 | 'MarkerSize',8), hold on % [0.49 1 .63] 25 | plot(x3(1:n1),y3(1:n1),'bo','Linewidth',[1],'MarkerEdgeColor','k',... 26 | 'MarkerFaceColor',[0.9 0 1],... 27 | 'MarkerSize',8) 28 | 29 | % training set: first 200 of 240 points 30 | X1=[x3(1:n1) y3(1:n1)]; 31 | X2=[x(1:n1) y(1:n1)]; 32 | 33 | figure(1) 34 | Y3=[X1(1:50,:); X2(1:50,:)]; 35 | Y2 = pdist(Y3,'euclidean'); 36 | Z = linkage(Y2,'average'); 37 | thresh=0.85*max(Z(:,3)); 38 | [H,T,O]=dendrogram(Z,100,'ColorThreshold',thresh); 39 | axis off 40 | 41 | figure(5) 42 | bar(O,'FaceColor',[.6 .6 .6],'EdgeColor','k') 43 | axis([0 100 0 100]) 44 | set(gca,'Fontsize',[15]) 45 | hold on 46 | plot([0 100],[50 50],'r:','Linewidth',[2]) 47 | plot([50.5 50.5],[0 100],'r:','Linewidth',[2]) 48 | 49 | 50 | thresh=0.25*max(Z(:,3)); 51 | [H,T,O]=dendrogram(Z,100,'ColorThreshold',thresh); 52 | axis off 53 | -------------------------------------------------------------------------------- /CH05/CH05_SEC05_1_GaussianMixtureModels.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | close all 4 | figure(1) 5 | load catData_w.mat 6 | load dogData_w.mat 7 | CD=[dog_wave cat_wave]; 8 | [u,s,v]=svd(CD-mean(CD(:))); 9 | 10 | 11 | subplot(2,2,1) 12 | plot(v(1:80,2),v(1:80,4),'ro','Linewidth',[1],'MarkerEdgeColor','k',... 13 | 'MarkerFaceColor',[0 1 0.2],... 14 | 'MarkerSize',8), hold on % [0.49 1 .63] 15 | plot(v(81:end,2),v(81:end,4),'bo','Linewidth',[1],'MarkerEdgeColor','k',... 16 | 'MarkerFaceColor',[0.9 0 1],... 17 | 'MarkerSize',8) 18 | set(gca,'Fontsize',[15]), hold on 19 | 20 | 21 | dogcat=v(:,2:2:4); 22 | GMModel=fitgmdist(dogcat,2) 23 | AIC= GMModel.AIC 24 | 25 | subplot(2,2,1) 26 | h=ezcontour(@(x1,x2)pdf(GMModel,[x1 x2])); 27 | xlabel(''),ylabel(''),title('') 28 | set(h,'Linecolor','k','Linewidth',[2]) 29 | axis([-.25 .25 -.25 .25]) 30 | 31 | subplot(2,2,2) 32 | h=ezmesh(@(x1,x2)pdf(GMModel,[x1 x2])); 33 | xlabel(''),ylabel(''),title(''), view(-16,40) 34 | set(gca,'Fontsize',[15]), colormap(gray) 35 | axis([-.25 .25 -.25 .25 0 30]) 36 | 37 | 38 | %% AIC scores 39 | 40 | figure(2) 41 | AIC = zeros(1,4); 42 | GMModels = cell(1,4); 43 | options = statset('MaxIter',500); 44 | for k = 1:4 45 | GMModels = fitgmdist(dogcat,k,'Options',options,'CovarianceType','diagonal'); 46 | AIC(k)= GMModels.AIC 47 | subplot(2,2,k) 48 | h=ezmeshc(@(x1,x2)pdf(GMModels,[x1 x2])); 49 | xlabel(''),ylabel(''),title('') 50 | % set(h,'Linecolor','k','Linewidth',[2]) 51 | % axis([-.25 .25 -.25 .25 0 30]) 52 | end 53 | 54 | 55 | -------------------------------------------------------------------------------- /CH05/catData.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH05/catData.mat -------------------------------------------------------------------------------- /CH05/catData_w.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH05/catData_w.mat -------------------------------------------------------------------------------- /CH05/dc_wavelet.m: -------------------------------------------------------------------------------- 1 | function dcData=dc_wavelet(dcfile) 2 | [m,n]=size(dcfile); % 4096x80 3 | nw=32*32; 4 | nbcol=size(colormap(gray),1); 5 | 6 | for j=1:n 7 | X=double(reshape(dcfile(:,j),64,64)); 8 | [cA,cH,cV,cD]=dwt2(X,'haar'); 9 | cod_cH1=wcodemat(cH,nbcol); 10 | cod_cV1=wcodemat(cV,nbcol); 11 | cod_edge=cod_cH1+cod_cV1; 12 | dcData(:,j)=reshape(cod_edge,nw,1); 13 | end -------------------------------------------------------------------------------- /CH05/dogData.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH05/dogData.mat -------------------------------------------------------------------------------- /CH05/dogData_w.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH05/dogData_w.mat -------------------------------------------------------------------------------- /CH06/CH06_SEC01_1_NN.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | load catData_w.mat; load dogData_w.mat; CD=[dog_wave cat_wave]; 4 | train=[dog_wave(:,1:60) cat_wave(:,1:60)]; 5 | test=[dog_wave(:,61:80) cat_wave(:,61:80)]; 6 | label=[ones(60,1); -1*ones(60,1)].'; 7 | 8 | A=label*pinv(train); test_labels=sign(A*test); 9 | subplot(4,1,1), bar(test_labels) 10 | subplot(4,1,2), bar(A) 11 | figure(2), subplot(2,2,1) 12 | A2=flipud(reshape(A,32,32)); pcolor(A2), colormap(gray) 13 | 14 | figure(1), subplot(4,1,3) 15 | A=lasso(train.',label.','Lambda',0.1).'; 16 | test_labels=sign(A*test); 17 | bar(test_labels) 18 | subplot(4,1,4) 19 | bar(A) 20 | figure(2), subplot(2,2,2) 21 | A2=flipud(reshape(A,32,32)); pcolor(A2), colormap(gray) -------------------------------------------------------------------------------- /CH06/CH06_SEC01_1_NN_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | load catData_w.mat; load dogData_w.mat; CD=[dog_wave cat_wave]; 4 | train=[dog_wave(:,1:60) cat_wave(:,1:60)]; 5 | test=[dog_wave(:,61:80) cat_wave(:,61:80)]; 6 | label=[ones(60,1); -1*ones(60,1)].'; 7 | 8 | A=label*pinv(train); test_labels=sign(A*test); 9 | subplot(4,1,1), bar(test_labels,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis off 10 | subplot(4,1,2), bar(A,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 1024 -0.002 0.002]), axis off 11 | figure(2), subplot(2,2,1) 12 | A2=flipud(reshape(A,32,32)); pcolor(A2), colormap(gray), axis off 13 | 14 | figure(1), subplot(4,1,3) 15 | A=lasso(train.',label.','Lambda',0.1).'; 16 | test_labels=sign(A*test); 17 | bar(test_labels,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis off 18 | subplot(4,1,4) 19 | bar(A,'FaceColor',[.6 .6 .6],'EdgeColor','k'), axis([0 1024 -0.008 0.008]), axis off 20 | figure(2), subplot(2,2,2) 21 | A2=flipud(reshape(A,32,32)); pcolor(A2), colormap(gray), axis off 22 | 23 | 24 | -------------------------------------------------------------------------------- /CH06/CH06_SEC02_1_NN.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | load catData_w.mat; load dogData_w.mat; 4 | CD=[dog_wave cat_wave]; 5 | 6 | x=[dog_wave(:,1:40) cat_wave(:,1:40)]; 7 | x2=[dog_wave(:,41:80) cat_wave(:,41:80)]; 8 | label=[ones(40,1) zeros(40,1); 9 | zeros(40,1) ones(40,1)].'; 10 | 11 | net = patternnet(2,'trainscg'); 12 | net.layers{1}.transferFcn = 'tansig'; 13 | 14 | net = train(net,x,label); 15 | view(net) 16 | y = net(x); 17 | y2= net(x2); 18 | perf = perform(net,label,y); 19 | classes2 = vec2ind(y); 20 | classes3 = vec2ind(y2); 21 | 22 | subplot(4,1,1), bar(y(1,:),'FaceColor',[.6 .6 .6],'EdgeColor','k') 23 | subplot(4,1,2), bar(y(2,:),'FaceColor',[.6 .6 .6],'EdgeColor','k') 24 | subplot(4,1,3), bar(y2(1,:),'FaceColor',[.6 .6 .6],'EdgeColor','k') 25 | subplot(4,1,4), bar(y2(2,:),'FaceColor',[.6 .6 .6],'EdgeColor','k') 26 | 27 | %% 28 | % [x,t] = iris_dataset; 29 | % net = patternnet(10); 30 | % net = train(net,x,t); 31 | % view(net) 32 | % y = net(x); 33 | % perf = perform(net,t,y); 34 | % classes = vec2ind(y); 35 | % 36 | 37 | -------------------------------------------------------------------------------- /CH06/CH06_SEC04_1_StochasticGradientDescent.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | h=0.1; x=-6:h:6; y=-6:h:6; n=length(x); 4 | [X,Y]=meshgrid(x,y); clear x, clear y 5 | 6 | F1=1.5-1.6*exp(-0.05*(3*(X+3).^2+(Y+3).^2)); 7 | F=F1 + (0.5-exp(-0.1*(3*(X-3).^2+(Y-3).^2))); 8 | [dFx,dFy]=gradient(F,h,h); 9 | 10 | x0=[4 0 -5]; y0=[0 -5 2]; col=['ro','bo','mo']; 11 | for jj=1:3 12 | q=randperm(n); i1=sort(q(1:10)); 13 | q2=randperm(n); i2=sort(q2(1:10)); 14 | x(1)=x0(jj); y(1)=y0(jj); 15 | f(1)=interp2(X(i1,i2),Y(i1,i2),F(i1,i2),x(1),y(1)); 16 | dfx=interp2(X(i1,i2),Y(i1,i2),dFx(i1,i2),x(1),y(1)); 17 | dfy=interp2(X(i1,i2),Y(i1,i2),dFy(i1,i2),x(1),y(1)); 18 | 19 | tau=2; 20 | for j=1:50 21 | x(j+1)=x(j)-tau*dfx; % update x, y, and f 22 | y(j+1)=y(j)-tau*dfy; 23 | q=randperm(n); ind1=sort(q(1:10)); 24 | q2=randperm(n); ind2=sort(q2(1:10)); 25 | f(j+1)=interp2(X(i1,i2),Y(i1,i2),F(i1,i2),x(j+1),y(j+1)) 26 | dfx=interp2(X(i1,i2),Y(i1,i2),dFx(i1,i2),x(j+1),y(j+1)); 27 | dfy=interp2(X(i1,i2),Y(i1,i2),dFy(i1,i2),x(j+1),y(j+1)); 28 | if abs(f(j+1)-f(j))<10^(-6) % check convergence 29 | break 30 | end 31 | end 32 | if jj==1; x1=x; y1=y; f1=f; end 33 | if jj==2; x2=x; y2=y; f2=f; end 34 | if jj==3; x3=x; y3=y; f3=f; end 35 | clear x, clear y, clear f 36 | end 37 | 38 | figure(1) 39 | contour(X,Y,F-1,10), colormap([0 0 0]), hold on 40 | plot(x1,y1,'ro',x1,y1,'k:',x2,y2,'mo',x2,y2,'k:',x3,y3,'bo',x3,y3,'k:','Linewidth',[2]) 41 | set(gca,'Fontsize',[18]) 42 | 43 | figure(2) 44 | surfl(X,Y,F), shading interp, colormap(gray), hold on 45 | plot3(x1,y1,f1+.1,'ro',x1,y1,f1,'k:',x2,y2,f2+0.1,'mo',x2,y2,f2,'k:',x3,y3,f3+0.1,'bo',x3,y3,f3,'k:','Linewidth',[2]) 46 | set(gca,'Fontsize',[18]) 47 | axis([-6 6 -6 6]), view(-25,60) 48 | 49 | 50 | -------------------------------------------------------------------------------- /CH06/CH06_SEC05_1_DeepCNN.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | load lettersTrainSet 4 | perm = randperm(1500,20); 5 | for j = 1:20 6 | subplot(4,5,j); 7 | imshow(XTrain(:,:,:,perm(j))); 8 | end 9 | 10 | %% 11 | 12 | layers = [imageInputLayer([28 28 1]); 13 | convolution2dLayer(5,16); 14 | reluLayer(); 15 | maxPooling2dLayer(2,'Stride',2); 16 | fullyConnectedLayer(3); 17 | softmaxLayer(); 18 | classificationLayer()]; 19 | options = trainingOptions('sgdm'); 20 | rng('default') % For reproducibility 21 | net = trainNetwork(XTrain,TTrain,layers,options); 22 | 23 | load lettersTestSet; 24 | YTest = classify(net,XTest); 25 | accuracy = sum(YTest == TTest)/numel(TTest) -------------------------------------------------------------------------------- /CH06/CH06_SEC06_1_NNLorenz.m: -------------------------------------------------------------------------------- 1 | clear all, close all 2 | 3 | % Simulate Lorenz system 4 | dt=0.01; T=8; t=0:dt:T; 5 | b=8/3; sig=10; r=28; 6 | 7 | Lorenz = @(t,x)([ sig * (x(2) - x(1)) ; ... 8 | r * x(1)-x(1) * x(3) - x(2) ; ... 9 | x(1) * x(2) - b*x(3) ]); 10 | ode_options = odeset('RelTol',1e-10, 'AbsTol',1e-11); 11 | 12 | input=[]; output=[]; 13 | for j=1:100 % training trajectories 14 | x0=30*(rand(3,1)-0.5); 15 | [t,y] = ode45(Lorenz,t,x0); 16 | input=[input; y(1:end-1,:)]; 17 | output=[output; y(2:end,:)]; 18 | plot3(y(:,1),y(:,2),y(:,3)), hold on 19 | plot3(x0(1),x0(2),x0(3),'ro') 20 | end 21 | grid on, view(-23,18) 22 | 23 | %% 24 | net = feedforwardnet([10 10 10]); 25 | net.layers{1}.transferFcn = 'logsig'; 26 | net.layers{2}.transferFcn = 'radbas'; 27 | net.layers{3}.transferFcn = 'purelin'; 28 | net = train(net,input.',output.'); 29 | 30 | 31 | %% 32 | figure(2) 33 | x0=20*(rand(3,1)-0.5); 34 | [t,y] = ode45(Lorenz,t,x0); 35 | plot3(y(:,1),y(:,2),y(:,3)), hold on 36 | plot3(x0(1),x0(2),x0(3),'ro','Linewidth',[2]) 37 | grid on 38 | 39 | ynn(1,:)=x0; 40 | for jj=2:length(t) 41 | y0=net(x0); 42 | ynn(jj,:)=y0.'; x0=y0; 43 | end 44 | plot3(ynn(:,1),ynn(:,2),ynn(:,3),':','Linewidth',[2]) 45 | 46 | figure(3) 47 | subplot(3,2,1), plot(t,y(:,1),t,ynn(:,1),'Linewidth',[2]) 48 | subplot(3,2,3), plot(t,y(:,2),t,ynn(:,2),'Linewidth',[2]) 49 | subplot(3,2,5), plot(t,y(:,3),t,ynn(:,3),'Linewidth',[2]) 50 | 51 | 52 | figure(2) 53 | x0=20*(rand(3,1)-0.5); 54 | [t,y] = ode45(Lorenz,t,x0); 55 | plot3(y(:,1),y(:,2),y(:,3)), hold on 56 | plot3(x0(1),x0(2),x0(3),'ro','Linewidth',[2]) 57 | grid on 58 | 59 | ynn(1,:)=x0; 60 | for jj=2:length(t) 61 | y0=net(x0); 62 | ynn(jj,:)=y0.'; x0=y0; 63 | end 64 | plot3(ynn(:,1),ynn(:,2),ynn(:,3),':','Linewidth',[2]) 65 | 66 | figure(3) 67 | subplot(3,2,2), plot(t,y(:,1),t,ynn(:,1),'Linewidth',[2]) 68 | subplot(3,2,4), plot(t,y(:,2),t,ynn(:,2),'Linewidth',[2]) 69 | subplot(3,2,6), plot(t,y(:,3),t,ynn(:,3),'Linewidth',[2]) 70 | 71 | %% 72 | figure(2), view(-75,15) 73 | figure(3) 74 | subplot(3,2,1), set(gca,'Fontsize',[15],'Xlim',[0 8]) 75 | subplot(3,2,2), set(gca,'Fontsize',[15],'Xlim',[0 8]) 76 | subplot(3,2,3), set(gca,'Fontsize',[15],'Xlim',[0 8]) 77 | subplot(3,2,4), set(gca,'Fontsize',[15],'Xlim',[0 8]) 78 | subplot(3,2,5), set(gca,'Fontsize',[15],'Xlim',[0 8]) 79 | subplot(3,2,6), set(gca,'Fontsize',[15],'Xlim',[0 8]) 80 | legend('Lorenz','NN') -------------------------------------------------------------------------------- /CH06/NLactivation.m: -------------------------------------------------------------------------------- 1 | function E=NLactivation(A,train,label) 2 | E= norm(label - 1./(1+exp(-A*train))); 3 | -------------------------------------------------------------------------------- /CH06/catData_w.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH06/catData_w.mat -------------------------------------------------------------------------------- /CH06/dogData_w.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH06/dogData_w.mat -------------------------------------------------------------------------------- /CH06/extra/am582_test2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | t=0:0.01:100; 4 | x0=[5 5 5]; 5 | 6 | [t,Y]=ode45('lrz_rhs',t,x0); 7 | 8 | x=Y(:,1); y=Y(:,2); z=Y(:,3); 9 | plot3(x,y,z,'Linewidth',[2]), grid on 10 | 11 | figure(2) 12 | plot(t,x,t,y,t,z,'Linewidth',[2]) 13 | 14 | %% 15 | figure(2) 16 | plot(t,x,'Linewidth',[2]) 17 | 18 | %% 19 | H=[x(1:5000).' 20 | x(2:5001).' 21 | x(3:5002).' 22 | x(4:5003).' 23 | x(5:5004).' 24 | x(6:5005).' 25 | x(7:5006).' 26 | x(8:5007).']; 27 | 28 | [u,s,v]=svd(H,'econ'); 29 | figure(3), plot(diag(s)/(sum(diag(s))),'ro','Linewidth',[3]) 30 | 31 | %% 32 | figure(4), plot(u(:,1:5),'Linewidth',[2]) 33 | figure(5), plot3(v(:,1),v(:,2),v(:,3),'Linewidth',[2]) 34 | 35 | figure(6), plot(v(:,4),'Linewidth',[2]) 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /CH06/extra/burgers.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH06/extra/burgers.mat -------------------------------------------------------------------------------- /CH06/extra/ml_dcnn.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos',... 4 | 'nndatasets','DigitDataset'); 5 | digitData = imageDatastore(digitDatasetPath,... 6 | 'IncludeSubfolders',true,'LabelSource','foldernames'); 7 | 8 | figure; 9 | perm = randperm(10000,20); 10 | for i = 1:20 11 | subplot(4,5,i); 12 | imshow(digitData.Files{perm(i)}); 13 | end 14 | 15 | digitData.countEachLabel 16 | 17 | trainingNumFiles = 750; 18 | rng(1) % For reproducibility 19 | [trainDigitData,testDigitData] = splitEachLabel(digitData,... 20 | trainingNumFiles,'randomize'); 21 | 22 | 23 | layers = [imageInputLayer([28 28 1]); 24 | convolution2dLayer(5,20); 25 | reluLayer(); 26 | maxPooling2dLayer(2,'Stride',2); 27 | fullyConnectedLayer(10); 28 | softmaxLayer(); 29 | classificationLayer()]; 30 | 31 | options = trainingOptions('sgdm','MaxEpochs',20,... 32 | 'InitialLearnRate',0.0001); 33 | 34 | convnet = trainNetwork(trainDigitData,layers,options); 35 | 36 | YTest = classify(convnet,testDigitData); 37 | TTest = testDigitData.Labels; 38 | 39 | accuracy = sum(YTest == TTest)/numel(TTest) 40 | -------------------------------------------------------------------------------- /CH06/lrz_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=lrz_rhs(t,x) 2 | sigma=10; beta=8/3; rho=28; 3 | rhs=[sigma*(x(2)-x(1)) 4 | x(1)*(rho-x(3)) 5 | x(1)*x(2)-beta*x(3)]; 6 | -------------------------------------------------------------------------------- /CH06/tausearch.m: -------------------------------------------------------------------------------- 1 | function mintau=tausearch(tau,x,y,dfx,dfy,X,Y,F) 2 | x0=x-tau*dfx; 3 | y0=y-tau*dfy; 4 | mintau=interp2(X,Y,F,x0,y0); 5 | -------------------------------------------------------------------------------- /CH07/CH07_SEC01_SimulateLogistic.m: -------------------------------------------------------------------------------- 1 | clear all; 2 | figure 3 | 4 | xvals=[]; 5 | startval = 1; 6 | endval = 4; 7 | for r=startval:.00025:endval 8 | r 9 | x = 0.5; 10 | for i=1:10000 11 | x = logistic(x,r); 12 | end 13 | xss = x; % steady state x 14 | for i=1:1000 15 | x = logistic(x,r); 16 | xvals(1,length(xvals)+1) = r; 17 | xvals(2,length(xvals)) = x; 18 | if(abs(x-xss)<.001) 19 | break 20 | end 21 | end 22 | 23 | end 24 | figure(1) 25 | plot(xvals(2,:),xvals(1,:),'.','LineWidth',.1,'MarkerSize',1.2,'Color',[0 0 0]) 26 | axis([0 1 1 endval]) 27 | axis ij 28 | figure(2) 29 | plot(xvals(2,:),xvals(1,:),'.','LineWidth',.1,'MarkerSize',1.2,'Color',[0 0 0]) 30 | axis([0 1 3.45 4]) 31 | axis ij -------------------------------------------------------------------------------- /CH07/CH07_SEC01_SimulateLorenz.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | Beta = [10; 28; 8/3]; % Lorenz's parameters (chaotic) 4 | 5 | x0=[0; 1; 20]; % Initial condition 6 | dt = 0.001; 7 | tspan=dt:dt:50; 8 | options = odeset('RelTol',1e-12,'AbsTol',1e-12*ones(1,3)); 9 | 10 | [t,x]=ode45(@(t,x) lorenz(t,x,Beta),tspan,x0,options); 11 | plot3(x(:,1),x(:,2),x(:,3)); 12 | 13 | %% 14 | hline = findobj(gcf, 'type', 'line') 15 | 16 | set(hline(1),'Color','k','LineWidth',2) 17 | xlabel('x') 18 | ylabel('y') 19 | zlabel('z') 20 | view(50,10) 21 | xlabel('x') 22 | ylabel('y') 23 | zlabel('z') 24 | % axis off 25 | x0 = -20 26 | y0 = -25 27 | z0 = 5; 28 | hold on 29 | scatter3(x0,y0,z0,5,'k.'); 30 | 31 | plot3([x0 x0+10],[y0 y0],[z0 z0],'k-','LineWidth',2) 32 | plot3([x0 x0],[y0 y0+10],[z0 z0],'k-','LineWidth',2) 33 | plot3([x0 x0],[y0 y0],[z0 z0+10],'k-','LineWidth',2) 34 | axis equal 35 | axis off 36 | 37 | set(gcf,'PaperPositionMode','auto') 38 | print('-depsc2', '-loose', '../figures/OverviewLorenz.eps'); -------------------------------------------------------------------------------- /CH07/CH07_SEC02_DMD_Cylinder.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | load ../DATA/VORTALL; 3 | % VORTALL contains flow fields reshaped into column vectors 4 | X = VORTALL; 5 | [Phi, Lambda, b] = DMD(X(:,1:end-1),X(:,2:end),21); 6 | % Code to plot the second mode 7 | fhandle = plotCylinder(real(reshape(Phi(:,2),199,449))); -------------------------------------------------------------------------------- /CH07/CH07_SEC03_SINDY_Lorenz.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | %% Generate Data 3 | Beta = [10; 28; 8/3]; % Lorenz's parameters (chaotic) 4 | n = 3; 5 | x0=[-8; 8; 27]; % Initial condition 6 | tspan=[.01:.01:50]; 7 | options = odeset('RelTol',1e-12,'AbsTol',1e-12*ones(1,n)); 8 | [t,x]=ode45(@(t,x) lorenz(t,x,Beta),tspan,x0,options); 9 | 10 | %% Compute Derivative 11 | for i=1:length(x) 12 | dx(i,:) = lorenz(0,x(i,:),Beta); 13 | end 14 | 15 | %% Build library and compute sparse regression 16 | Theta = poolData(x,n,3); % up to third order polynomials 17 | lambda = 0.025; % lambda is our sparsification knob. 18 | Xi = sparsifyDynamics(Theta,dx,lambda,n) 19 | poolDataLIST({'x','y','z'},Xi,n,polyorder); -------------------------------------------------------------------------------- /CH07/CH07_SEC03_SINDY_Lorenz_production.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | %% Generate Data 3 | Beta = [10; 28; 8/3]; % Lorenz's parameters (chaotic) 4 | n = 3; 5 | x0=[-8; 8; 27]; % Initial condition 6 | tspan=[.01:.01:50]; 7 | options = odeset('RelTol',1e-12,'AbsTol',1e-12*ones(1,3)); 8 | [t,x]=ode45(@(t,x) lorenz(t,x,Beta),tspan,x0,options); 9 | 10 | %% Compute Derivative 11 | for i=1:length(x) 12 | dx(i,:) = lorenz(0,x(i,:),Beta); 13 | end 14 | 15 | %% Pool Data (i.e., build library of nonlinear time series) 16 | polyorder = 3; 17 | Theta = poolData(x,n,polyorder); 18 | m = size(Theta,2); 19 | 20 | %% Compute Sparse regression: sequential least squares 21 | lambda = 0.025; % lambda is our sparsification knob. 22 | Xi = sparsifyDynamics(Theta,dx,lambda,n) 23 | poolDataLIST({'x','y','z'},Xi,n,polyorder); -------------------------------------------------------------------------------- /CH07/CH07_SEC04_Koopman.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | %% System 3 | mu = -.05; 4 | lambda = -1; 5 | A = [mu 0 0; 0 lambda -lambda; 0 0 2*mu]; % Koopman linear dynamics 6 | [T,D] = eig(A); 7 | slope_stab_man = T(3,3)/T(2,3); % slope of stable subspace (green) 8 | 9 | %% Integrate Koopman trajectories 10 | y0A = [1.5; -1; 2.25]; 11 | y0B = [1; -1; 1]; 12 | y0C = [2; -1; 4]; 13 | tspan = 0:.01:1000; 14 | [t,yA] = ode45(@(t,y)A*y,tspan,y0A); 15 | [t,yB] = ode45(@(t,y)A*y,tspan,y0B); 16 | [t,yC] = ode45(@(t,y)A*y,tspan,y0C); 17 | 18 | %% Plot invariant surfaces 19 | % Attracting manifold $y_2=y_1^2$ (red manifold) 20 | [X,Z] = meshgrid(-2:.01:2,-1:.01:4); 21 | Y = X.^2; 22 | surf(X,Y,Z,'EdgeColor','None','FaceColor','r','FaceAlpha',.1) 23 | hold on, grid on, view(-15,8), lighting gouraud 24 | 25 | % Invariant set $y_3=y_1^2$ (blue manifold) 26 | [X1,Y1] = meshgrid(-2:.01:2,-1:.01:4); 27 | Z1 = X1.^2; 28 | surf(X1,Y1,Z1,'EdgeColor','None','FaceColor','b','FaceAlpha',.1) 29 | 30 | % Stable invariant subspace of Koopman linear system (green plane) 31 | [X2,Y2]=meshgrid(-2:0.01:2,0:.01:4); 32 | Z2 = slope_stab_man*Y2; % for mu=-.2 33 | surf(X2,Y2,Z2,'EdgeColor','None','FaceColor',[.3 .7 .3],'FaceAlpha',.7) 34 | 35 | x = -2:.01:2; 36 | % intersection of green and blue surfaces (below) 37 | plot3(x,(1/slope_stab_man)*x.^2,x.^2,'-g','LineWidth',2) 38 | % intersection of red and blue surfaces (below) 39 | plot3(x,x.^2,x.^2,'--r','LineWidth',2) 40 | plot3(x,x.^2,-1+0*x,'r--','LineWidth',2); 41 | 42 | %% Plot Koopman Trajectories (from lines 15-17) 43 | plot3(yA(:,1),yA(:,2),-1+0*yA,'k-','LineWidth',1); 44 | plot3(yB(:,1),yB(:,2),-1+0*yB,'k-','LineWidth',1); 45 | plot3(yC(:,1),yC(:,2),-1+0*yC,'k-','LineWidth',1); 46 | plot3(yA(:,1),yA(:,2),yA(:,3),'k','LineWidth',1.5) 47 | plot3(yB(:,1),yB(:,2),yB(:,3),'k','LineWidth',1.5) 48 | plot3(yC(:,1),yC(:,2),yC(:,3),'k','LineWidth',1.5) 49 | plot3([0 0],[0 0],[0 -1],'ko','LineWidth',4) 50 | set(gca,'ztick',[0 1 2 3 4 5]) 51 | axis([-4 4 -1 4 -1 4]) 52 | xlabel('y_1'), ylabel('y_2'), zlabel('y_3'); -------------------------------------------------------------------------------- /CH07/CH07_SEC05_HAVOK_Lorenz.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % generate Data 4 | Beta = [10;28;8/3]; % Lorenz's parameters (chaotic) 5 | n = 3; 6 | x0=[-8; 8; 27]; % Initial condition 7 | dt = 0.01; 8 | tspan=[dt:dt:50]; 9 | options = odeset('RelTol',1e-12,'AbsTol',1e-12*ones(1,n)); 10 | [t,x]=ode45(@(t,x) lorenz(t,x,Beta),tspan,x0,options); 11 | 12 | %% EIGEN-TIME DELAY COORDINATES 13 | stackmax = 10; % Number of shift-stacked rows 14 | r=10; % Rank of HAVOK Model 15 | H = zeros(stackmax,size(x,1)-stackmax); 16 | for k=1:stackmax 17 | H(k,:) = x(k:end-stackmax-1+k,1); 18 | end 19 | [U,S,V] = svd(H,'econ'); % Eigen delay coordinates 20 | 21 | %% COMPUTE DERIVATIVES (4TH ORDER CENTRAL DIFFERENCE) 22 | dV = zeros(length(V)-5,r); 23 | for i=3:length(V)-3 24 | for k=1:r 25 | dV(i-2,k) = (1/(12*dt))*(-V(i+2,k)+8*V(i+1,k)-8*V(i-1,k)+V(i-2,k)); 26 | end 27 | end 28 | % trim first and last two that are lost in derivative 29 | V = V(3:end-3,1:r); 30 | 31 | %% BUILD HAVOK REGRESSION MODEL ON TIME DELAY COORDINATES 32 | Xi = V\dV; 33 | A = Xi(1:r-1,1:r-1)'; 34 | B = Xi(end,1:r-1)'; -------------------------------------------------------------------------------- /CH07/DMD.m: -------------------------------------------------------------------------------- 1 | function [Phi, Lambda, b] = DMD(X,Xprime,r) 2 | 3 | [U,Sigma,V] = svd(X,'econ'); % Step 1 4 | Ur = U(:,1:r); 5 | Sigmar = Sigma(1:r,1:r); 6 | Vr = V(:,1:r); 7 | 8 | Atilde = Ur'*Xprime*Vr/Sigmar; % Step 2 9 | [W,Lambda] = eig(Atilde); % Step 3 10 | 11 | Phi = Xprime*(Vr/Sigmar)*W; % Step 4 12 | alpha1 = Sigmar*Vr(1,:)'; 13 | b = (W*Lambda)\alpha1; -------------------------------------------------------------------------------- /CH07/LorenzMovie/MOVIE_LORENZ.m: -------------------------------------------------------------------------------- 1 | clear all 2 | 3 | load Lorenz.mat 4 | 5 | v = VideoWriter('Lorenz_mix2.avi'); 6 | open(v); 7 | % Lorenz's parameters (chaotic) 8 | sigma = 10; 9 | beta = 8/3; 10 | rho = 28; 11 | figure 12 | set(gcf,'Position',[100 100 1280 1000]) 13 | for k=1:20 14 | % Initial condition 1 - Large cube of data 15 | % y0=[0 0 0]; 16 | % xvec = -20:2:20; 17 | % yvec = -20:2:20; 18 | % zvec = -20:2:20; 19 | % % Initial condition 2 - small cube around initial condition from last class 20 | y0=[-8; 8; 27]; 21 | xvec = -1:.1:1; 22 | yvec = -1:.1:1; 23 | zvec = -1:.1:1; 24 | % % Initial condition 3 - even smaller cube around initial condition 25 | 26 | % y0=[-8; 8; 27]; 27 | % xvec = -.1:.01:.1; 28 | % yvec = -.1:.01:.1; 29 | % zvec = -.1:.01:.1; 30 | [x0,y0,z0] = meshgrid(xvec+y0(1),yvec+y0(2),zvec+y0(3)); 31 | yIC(1,:,:,:) = x0; 32 | yIC(2,:,:,:) = y0; 33 | yIC(3,:,:,:) = z0; 34 | plot3(xdat(:,1),xdat(:,2),xdat(:,3),'k-','LineWidth',1), hold on 35 | plot3(yIC(1,:),yIC(2,:),yIC(3,:),'r.','LineWidth',2,'MarkerSize',10), hold off 36 | axis([-40 40 -40 40 -10 50]) 37 | view(-200,24); 38 | axis off 39 | drawnow 40 | frame=getframe; 41 | writeVideo(v,frame); 42 | end 43 | 44 | %% Compute trajectory 45 | dt =0.01; 46 | duration = 4 47 | tspan=[0,duration]; 48 | L = duration/dt; 49 | yin = yIC; 50 | 51 | for step = 1:L 52 | time = step*dt 53 | yout = rk4singlestep(@(t,y)lorenz3D(t,y,sigma,beta,rho),dt,time,yin); 54 | yin = yout; 55 | plot3(xdat(:,1),xdat(:,2),xdat(:,3),'k-','LineWidth',1), hold on 56 | plot3(yout(1,:),yout(2,:),yout(3,:),'r.','LineWidth',2,'MarkerSize',10), hold off 57 | view(-200+230*step/L,24); 58 | axis([-40 40 -40 40 -10 50]) 59 | % set(gca,'FontSize',16) 60 | axis off 61 | axis([-40 40 -40 40 -10 50]) 62 | drawnow 63 | frame=getframe; 64 | writeVideo(v,frame); 65 | writeVideo(v,frame); 66 | 67 | end 68 | close(v); -------------------------------------------------------------------------------- /CH07/LorenzMovie/MOVIE_LORENZ_BW.m: -------------------------------------------------------------------------------- 1 | clear all 2 | 3 | load('../DATA/FIG01_LORENZ.mat') 4 | 5 | % v = VideoWriter('../MOVIES/Lorenz_mix2_bw.avi'); 6 | % open(v); 7 | % Lorenz's parameters (chaotic) 8 | sigma = 10; 9 | beta = 8/3; 10 | rho = 28; 11 | figure 12 | set(gcf,'Position',[100 100 2500 1500]) 13 | for k=1:20 14 | % Initial condition 1 - Large cube of data 15 | % y0=[0 0 0]; 16 | % xvec = -20:2:20; 17 | % yvec = -20:2:20; 18 | % zvec = -20:2:20; 19 | % % Initial condition 2 - small cube around initial condition from last class 20 | y0=[-8; 8; 27]; 21 | xvec = -1:.1:1; 22 | yvec = -1:.1:1; 23 | zvec = -1:.1:1; 24 | % % Initial condition 3 - even smaller cube around initial condition 25 | 26 | % y0=[-8; 8; 27]; 27 | % xvec = -.1:.01:.1; 28 | % yvec = -.1:.01:.1; 29 | % zvec = -.1:.01:.1; 30 | [x0,y0,z0] = meshgrid(xvec+y0(1),yvec+y0(2),zvec+y0(3)); 31 | yIC(1,:,:,:) = x0; 32 | yIC(2,:,:,:) = y0; 33 | yIC(3,:,:,:) = z0; 34 | plot3(xdat(:,1),xdat(:,2),xdat(:,3),'w-','LineWidth',1), hold on 35 | plot3(yIC(1,:),yIC(2,:),yIC(3,:),'.','Color',[1 .1 .1],'LineWidth',2,'MarkerSize',10), hold off 36 | axis([-40 40 -40 40 -10 50]) 37 | view(-200,24); 38 | axis off 39 | set(gcf,'Color','k') 40 | set(gcf,'InvertHardcopy','off') 41 | drawnow 42 | % frame=getframe; 43 | % writeVideo(v,frame); 44 | end 45 | 46 | %% Compute trajectory 47 | dt =0.01; 48 | duration = 4 49 | tspan=[0,duration]; 50 | L = duration/dt; 51 | yin = yIC; 52 | 53 | for step = 1:L 54 | time = step*dt 55 | yout = rk4singlestep(@(t,y)lorenz3D(t,y,sigma,beta,rho),dt,time,yin); 56 | yin = yout; 57 | plot3(xdat(:,1),xdat(:,2),xdat(:,3),'w-','LineWidth',1), hold on 58 | plot3(yout(1,:),yout(2,:),yout(3,:),'.','Color',[1 .1 .1],'LineWidth',2,'MarkerSize',10), hold off 59 | view(-200+230*step/L,24); 60 | axis([-40 40 -40 40 -10 50]) 61 | % set(gca,'FontSize',16) 62 | axis off 63 | axis([-40 40 -40 40 -10 50]) 64 | set(gcf,'Color','k') 65 | set(gcf,'InvertHardcopy','off') 66 | drawnow 67 | % frame=getframe; 68 | % writeVideo(v,frame); 69 | % writeVideo(v,frame); 70 | 71 | end 72 | % close(v); -------------------------------------------------------------------------------- /CH07/LorenzMovie/QuiverLorenz.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | sigma = 10; % Lorenz's parameters (chaotic) 4 | beta = 8/3; 5 | rho = 28; 6 | n = 3; % State dimension 7 | 8 | x0=[0; 1; 20]; % Initial condition 9 | 10 | % Integrate 11 | dt = 0.001; 12 | tspan=[dt:dt:50]; 13 | options = odeset('RelTol',1e-12,'AbsTol',1e-12*ones(1,n)); 14 | [t,x]=ode45(@(t,x) lorenz(t,x,sigma,beta,rho),tspan,x0,options); 15 | 16 | 17 | y0=[0; 0; 25]; 18 | xvec = -20:5:20; 19 | yvec = -25:5:25; 20 | zvec = -20:5:20; 21 | 22 | [x0,y0,z0] = meshgrid(xvec+y0(1),yvec+y0(2),zvec+y0(3)); 23 | yIC(1,:,:,:) = x0; 24 | yIC(2,:,:,:) = y0; 25 | yIC(3,:,:,:) = z0; 26 | 27 | dy = lorenz3D(0,yIC,sigma,beta,rho); 28 | %% 29 | h=quiver3(x0,y0,z0,squeeze(dy(1,:,:,:)),squeeze(dy(2,:,:,:)),squeeze(dy(3,:,:,:)),'Color',[.5 .5 .5],'LineWidth',1); 30 | hold on 31 | plot3(x(:,1),x(:,2),x(:,3),'r','LineWidth',1); 32 | 33 | % plot3(x(:,1),x(:,2),x(:,3),'Color',[.5 .5 .5],'LineWidth',1); 34 | count = 1; 35 | for k=1:200:length(x) 36 | xC(count,:) = x(k,:); 37 | dyC(count,:) = squeeze(lorenz(0,x(k,:),sigma,beta,rho)); 38 | count = count + 1; 39 | end 40 | % quiver3(xC(:,1),xC(:,2),xC(:,3),dyC(:,1),dyC(:,2),dyC(:,3),'r','LineWidth',1.5,'AutoScaleFactor',2,'MarkerSize',10,'MaxHeadSize',1); 41 | axis tight -------------------------------------------------------------------------------- /CH07/LorenzMovie/lorenz3D.m: -------------------------------------------------------------------------------- 1 | function dy = lorenz3D(t,y,sigma,beta,rho) 2 | % y is a three dimensional state-vector 3 | dy = [ 4 | sigma*(y(2,:,:,:)-y(1,:,:,:)); 5 | y(1,:,:,:).*(rho-y(3,:,:,:))-y(2,:,:,:); 6 | y(1,:,:,:).*y(2,:,:,:)-beta*y(3,:,:,:); 7 | ]; -------------------------------------------------------------------------------- /CH07/logistic.m: -------------------------------------------------------------------------------- 1 | function xkplusone = logistic(xk,r) 2 | xkplusone = r*xk*(1-xk); -------------------------------------------------------------------------------- /CH07/lorenz.m: -------------------------------------------------------------------------------- 1 | function dx = lorenz(t,x,Beta) 2 | dx = [ 3 | Beta(1)*(x(2)-x(1)); 4 | x(1)*(Beta(2)-x(3))-x(2); 5 | x(1)*x(2)-Beta(3)*x(3); 6 | ]; -------------------------------------------------------------------------------- /CH07/plotCylinder.m: -------------------------------------------------------------------------------- 1 | function fhandle = plotCylinder(VORT) 2 | 3 | fhandle = figure 4 | 5 | vortmin = -5; 6 | vortmax = 5; 7 | V2 = VORT; 8 | % normalize values... not symmetric 9 | minval = min(V2(:)); 10 | maxval = max(V2(:)); 11 | if(abs(minval)<5 && abs(maxval)<5) 12 | if(abs(minval)>abs(maxval)) 13 | vortmax = maxval; 14 | vortmin = -maxval; 15 | else 16 | vortmin = minval; 17 | vortmax = -minval; 18 | end 19 | end 20 | V2(V2>vortmax) = vortmax; 21 | V2(V2=2) % poly order 2 16 | for i=1:nVars 17 | for j=i:nVars 18 | yout(:,ind) = yin(:,i).*yin(:,j); 19 | ind = ind+1; 20 | end 21 | end 22 | end 23 | 24 | if(polyorder>=3) % poly order 3 25 | for i=1:nVars 26 | for j=i:nVars 27 | for k=j:nVars 28 | yout(:,ind) = yin(:,i).*yin(:,j).*yin(:,k); 29 | ind = ind+1; 30 | end 31 | end 32 | end 33 | end -------------------------------------------------------------------------------- /CH07/poolDataLIST.m: -------------------------------------------------------------------------------- 1 | function yout = poolDataLIST(yin,ahat,nVars,polyorder) 2 | n = size(yin,1); 3 | 4 | ind = 1; 5 | % poly order 0 6 | yout{ind,1} = ['1']; 7 | ind = ind+1; 8 | 9 | % poly order 1 10 | for i=1:nVars 11 | yout(ind,1) = yin(i); 12 | ind = ind+1; 13 | end 14 | 15 | if(polyorder>=2) 16 | % poly order 2 17 | for i=1:nVars 18 | for j=i:nVars 19 | yout{ind,1} = [yin{i},yin{j}]; 20 | ind = ind+1; 21 | end 22 | end 23 | end 24 | 25 | if(polyorder>=3) 26 | % poly order 3 27 | for i=1:nVars 28 | for j=i:nVars 29 | for k=j:nVars 30 | yout{ind,1} = [yin{i},yin{j},yin{k}]; 31 | ind = ind+1; 32 | end 33 | end 34 | end 35 | end 36 | 37 | output = yout; 38 | newout(1) = {''}; 39 | for k=1:length(yin) 40 | newout{1,1+k} = [yin{k},'dot']; 41 | end 42 | % newout = {'','xdot','ydot','udot'}; 43 | for k=1:size(ahat,1) 44 | newout(k+1,1) = output(k); 45 | for j=1:length(yin) 46 | newout{k+1,1+j} = ahat(k,j); 47 | end 48 | end 49 | newout -------------------------------------------------------------------------------- /CH07/sparsifyDynamics.m: -------------------------------------------------------------------------------- 1 | function Xi = sparsifyDynamics(Theta,dXdt,lambda,n) 2 | % Copyright 2015, All Rights Reserved 3 | % Code by Steven L. Brunton 4 | % For Paper, "Discovering Governing Equations from Data: 5 | % Sparse Identification of Nonlinear Dynamical Systems" 6 | % by S. L. Brunton, J. L. Proctor, and J. N. Kutz 7 | 8 | % compute Sparse regression: sequential least squares 9 | Xi = Theta\dXdt; % initial guess: Least-squares 10 | 11 | % lambda is our sparsification knob. 12 | for k=1:10 13 | smallinds = (abs(Xi) 16 | /* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */ 17 | #define u_width 1 18 | #define y_width 1 19 | /* 20 | * Create external references here. 21 | * 22 | */ 23 | /* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */ 24 | /* extern double func(double a); */ 25 | /* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */ 26 | 27 | /* 28 | * Output functions 29 | * 30 | */ 31 | void Cart_Pendulum_Outputs_wrapper(const real_T *u, 32 | real_T *x, 33 | const real_T *xC) 34 | { 35 | /* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */ 36 | /* This sample sets the output equal to the input 37 | y0[0] = u0[0]; 38 | For complex signals use: y0[0].re = u0[0].re; 39 | y0[0].im = u0[0].im; 40 | y1[0].re = u1[0].re; 41 | y1[0].im = u1[0].im; 42 | */ 43 | y0[0] = xC[0]; 44 | y0[1] = xC[1]; 45 | y0[2] = xC[2]; 46 | y0[3] = xC[3]; 47 | /* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */ 48 | } 49 | 50 | /* 51 | * Derivatives function 52 | * 53 | */ 54 | void Cart_Pendulum_Derivatives_wrapper(const real_T *u, 55 | const real_T *x, 56 | real_T *dx, 57 | real_T *xC) 58 | { 59 | /* %%%-SFUNWIZ_wrapper_Derivatives_Changes_BEGIN --- EDIT HERE TO _END */ 60 | double m = 1; 61 | double M = 5; 62 | double L = 2; 63 | double g = -10; 64 | double d = 1; 65 | double Sy; 66 | double Cy; 67 | Sy = sin(xC[2]); 68 | Cy = cos(xC[2]); 69 | dx[0] = xC[1]; 70 | dx[1] = (1/(m*L*L*(M+m*(1-Cy*Cy))))*(-1*m*m*L*L*g*Cy*Sy + m*L*L*(m*L*xC[3]*xC[3]*Sy - d*xC[1])) + m*L*L*(1/(m*L*L*(M+m*(1-Cy*Cy))))*u0[0]; 71 | dx[2] = xC[3]; 72 | dx[3] = (1/(m*L*L*(M+m*(1-Cy*Cy))))*((m+M)*m*g*L*Sy - m*L*Cy*(m*L*xC[3]*xC[3]*Sy - d*xC[1])) - m*L*Cy*(1/(m*L*L*(M+m*(1-Cy*Cy))))*u0[0]; 73 | /* %%%-SFUNWIZ_wrapper_Derivatives_Changes_END --- EDIT HERE TO _BEGIN */ 74 | } 75 | -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/SFB__Cart_Pendulum__SFB.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/SFB__Cart_Pendulum__SFB.mat -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/SFB__cartpend2__SFB.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/SFB__cartpend2__SFB.mat -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/SFB__cartpend_sim__SFB.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/SFB__cartpend_sim__SFB.mat -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/cartpend_sim.mexmaci64: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/cartpend_sim.mexmaci64 -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/cartpend_sim_wrapper.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | * Include Files 5 | * 6 | */ 7 | #if defined(MATLAB_MEX_FILE) 8 | #include "tmwtypes.h" 9 | #include "simstruc_types.h" 10 | #else 11 | #include "rtwtypes.h" 12 | #endif 13 | 14 | /* %%%-SFUNWIZ_wrapper_includes_Changes_BEGIN --- EDIT HERE TO _END */ 15 | #include 16 | /* %%%-SFUNWIZ_wrapper_includes_Changes_END --- EDIT HERE TO _BEGIN */ 17 | #define u_width 1 18 | #define y_width 1 19 | /* 20 | * Create external references here. 21 | * 22 | */ 23 | /* %%%-SFUNWIZ_wrapper_externs_Changes_BEGIN --- EDIT HERE TO _END */ 24 | /* extern double func(double a); */ 25 | /* %%%-SFUNWIZ_wrapper_externs_Changes_END --- EDIT HERE TO _BEGIN */ 26 | 27 | /* 28 | * Output functions 29 | * 30 | */ 31 | void cartpend_sim_Outputs_wrapper(const real_T *u, 32 | const real_T *d, 33 | real_T *y, 34 | const real_T *xC) 35 | { 36 | /* %%%-SFUNWIZ_wrapper_Outputs_Changes_BEGIN --- EDIT HERE TO _END */ 37 | /* This sample sets the output equal to the input 38 | y0[0] = u0[0]; 39 | For complex signals use: y0[0].re = u0[0].re; 40 | y0[0].im = u0[0].im; 41 | y1[0].re = u1[0].re; 42 | y1[0].im = u1[0].im; 43 | */ 44 | y[0] = xC[0]; 45 | y[1] = xC[1]; 46 | y[2] = xC[2]; 47 | y[3] = xC[3]; 48 | /* %%%-SFUNWIZ_wrapper_Outputs_Changes_END --- EDIT HERE TO _BEGIN */ 49 | } 50 | 51 | /* 52 | * Derivatives function 53 | * 54 | */ 55 | void cartpend_sim_Derivatives_wrapper(const real_T *u, 56 | const real_T *d, 57 | real_T *y, 58 | real_T *dx, 59 | real_T *xC) 60 | { 61 | /* %%%-SFUNWIZ_wrapper_Derivatives_Changes_BEGIN --- EDIT HERE TO _END */ 62 | double m = 1; 63 | double M = 5; 64 | double L = 2; 65 | double g = -10; 66 | double delta = 1; 67 | double Sy; 68 | double Cy; 69 | Sy = sin(xC[2]); 70 | Cy = cos(xC[2]); 71 | dx[0] = xC[1] + d[0]; 72 | dx[1] = (1/(m*L*L*(M+m*(1-Cy*Cy))))*(-1*m*m*L*L*g*Cy*Sy + m*L*L*(m*L*xC[3]*xC[3]*Sy - delta*xC[1])) + m*L*L*(1/(m*L*L*(M+m*(1-Cy*Cy))))*u[0] + d[1]; 73 | dx[2] = xC[3] + d[3]; 74 | dx[3] = (1/(m*L*L*(M+m*(1-Cy*Cy))))*((m+M)*m*g*L*Sy - m*L*Cy*(m*L*xC[3]*xC[3]*Sy - delta*xC[1])) - m*L*Cy*(1/(m*L*L*(M+m*(1-Cy*Cy))))*u[0] + d[3]; 75 | /* %%%-SFUNWIZ_wrapper_Derivatives_Changes_END --- EDIT HERE TO _BEGIN */ 76 | } 77 | -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.pdf -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.slx -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.slx.r2015b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/lqg_cartpend_sim.slx.r2015b -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqg_simulinkINIT.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | m = 1; 4 | M = 5; 5 | L = 2; 6 | g = -10; 7 | d = 1; 8 | 9 | b = 1; % pendulum down (b=-1) 10 | 11 | A = [0 1 0 0; 12 | 0 -d/M b*m*g/M 0; 13 | 0 0 0 1; 14 | 0 -b*d/(M*L) -b*(m+M)*g/(M*L) 0]; 15 | B = [0; 1/M; 0; b*1/(M*L)]; 16 | 17 | 18 | C = [1 0 0 0]; % measure cart position, x 19 | 20 | D = zeros(size(C,1),size(B,2)); 21 | 22 | Q = [1 0 0 0; 23 | 0 1 0 0; 24 | 0 0 1 0; 25 | 0 0 0 1]; 26 | R = .0001; 27 | K = lqr(A,B,Q,R); % design controller u = -K*x 28 | 29 | % Augment system with disturbances and noise 30 | Vd = .0000001*eye(4); % disturbance covariance 31 | Vn = .0000001; % noise covariance 32 | 33 | BF = [B Vd 0*B]; % augment inputs to include disturbance and noise 34 | 35 | sysC = ss(A,BF,C,[0 0 0 0 0 Vn]); % build big state space system... with single output 36 | 37 | sysFullOutput = ss(A,BF,eye(4),zeros(4,size(BF,2))); % system with full state output, disturbance, no noise 38 | 39 | % Build Kalman filter 40 | [L,P,E] = lqe(A,Vd,C,Vd,Vn); % design Kalman filter 41 | Kf = (lqr(A',C',Vd,Vn))'; % alternatively, possible to design using "LQR" code 42 | 43 | sysKF = ss(A-Kf*C,[B Kf],eye(4),0*[B Kf]); % Kalman filter estimator 44 | -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqg_simulinkINITBOOK.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | m = 1; 4 | M = 5; 5 | L = 2; 6 | g = -10; 7 | d = 1; 8 | 9 | s = 1; % pendulum up (s=1) 10 | A = [0 1 0 0; 11 | 0 -d/M -m*g/M 0; 12 | 0 0 0 1; 13 | 0 -s*d/(M*L) -s*(m+M)*g/(M*L) 0]; 14 | 15 | B = [0; 1/M; 0; s*1/(M*L)]; 16 | 17 | C = [1 0 0 0]; 18 | 19 | D = zeros(size(C,1),size(B,2)); 20 | 21 | 22 | Q = [1 0 0 0; 23 | 0 1 0 0; 24 | 0 0 1 0; 25 | 0 0 0 1]; 26 | R = .000001; 27 | K = lqr(A,B,Q,R); % design controller u = -K*x 28 | 29 | 30 | % Augment system with disturbances and noise 31 | Vdmag = .04; 32 | Vd = Vdmag*eye(4); % disturbance covariance 33 | Vn = .0002; % noise covariance 34 | 35 | BF = [B Vd 0*B]; % augment inputs to include disturbance and noise 36 | 37 | sysC = ss(A,BF,C,[0 0 0 0 0 Vn]); % build big state space system... with single output 38 | 39 | sysFullOutput = ss(A,BF,eye(4),zeros(4,size(BF,2))); % system with full state output, disturbance, no noise 40 | 41 | % Build Kalman filter 42 | [L,P,E] = lqe(A,eye(4),C,Vd,Vn); % design Kalman filter 43 | Kf = (lqr(A',C',Vd,Vn))'; % alternatively, possible to design using "LQR" code 44 | 45 | sysKF = ss(A-Kf*C,[B Kf],eye(4),0*[B Kf]); % Kalman filter estimator 46 | -------------------------------------------------------------------------------- /CH08/CH08_SEC07_3_LQG_Simulink/lqr_cartpend_sim.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH08/CH08_SEC07_3_LQG_Simulink/lqr_cartpend_sim.slx -------------------------------------------------------------------------------- /CH08/CH08_SEC08_1_TransferFunction.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | s = tf('s') 4 | G = 1/(s^2 + s + 2); 5 | 6 | bode(G) 7 | h = findobj(gcf,'type','line'); 8 | set(h,'linewidth',2); 9 | grid on 10 | set(gcf,'Position',[100 100 500 300]) 11 | set(gcf,'PaperPositionMode','auto') 12 | print('-depsc2', '-loose', 'figures/FIG_Bode'); 13 | 14 | 15 | 16 | A = [0 1; -2 -1]; 17 | B = [0; 1]; 18 | C = [1 0]; 19 | D = 0; 20 | 21 | [num,den] = ss2tf(A,B,C,D); 22 | G = tf(num,den) 23 | 24 | impulse(G) 25 | h = findobj(gcf,'type','line'); 26 | set(h,'linewidth',2); 27 | grid on 28 | set(gcf,'Position',[100 100 500 250]) 29 | set(gcf,'PaperPositionMode','auto') 30 | print('-depsc2', '-loose', 'figures/FIG_Impulse'); 31 | step(G) 32 | h = findobj(gcf,'type','line'); 33 | set(h,'linewidth',2); 34 | grid on 35 | set(gcf,'Position',[100 100 500 250]) 36 | set(gcf,'PaperPositionMode','auto') 37 | print('-depsc2', '-loose', 'figures/FIG_Step'); 38 | -------------------------------------------------------------------------------- /CH08/CH08_SEC08_2_SandT.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | s = tf('s'); 4 | 5 | L = 1/(s); 6 | S = 1/(1+L); 7 | T = L/(1+L); 8 | 9 | bode(L); 10 | hold on 11 | bode(S) 12 | bode(T) 13 | l1=legend('L','S','T'); 14 | h = findobj(gcf,'type','line'); 15 | set(h,'linewidth',2); 16 | % h = findobj(l1,'type','line'); 17 | % set(h,'linewidth',2); 18 | grid on 19 | set(gcf,'Position',[100 100 500 350]) 20 | set(gcf,'PaperPositionMode','auto') 21 | print('-depsc2', '-loose', 'figures/FIG_LST'); -------------------------------------------------------------------------------- /CH08/CH08_SEC08_3_PlantInversion.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | s = tf('s'); 4 | 5 | G = (s+1)/(s-2); 6 | Gtrue = (s+.9)/(s-1.9); 7 | 8 | K = 1/G; 9 | 10 | L = K*Gtrue; 11 | 12 | margin(L) 13 | 14 | CL = feedback(L,1); 15 | -------------------------------------------------------------------------------- /CH08/drawpend.m: -------------------------------------------------------------------------------- 1 | function drawpend(state,m,M,L) 2 | x = state(1); 3 | th = state(3); 4 | 5 | % dimensions 6 | W = 1*sqrt(M/5); % cart width 7 | H = .5*sqrt(M/5); % cart height 8 | wr = .2; % wheel radius 9 | mr = .3*sqrt(m); % mass radius 10 | 11 | % positions 12 | y = wr/2+H/2; % cart vertical position 13 | pendx = x + L*sin(th); 14 | pendy = y - L*cos(th); 15 | 16 | plot([-10 10],[0 0],'k','LineWidth',2), hold on 17 | rectangle('Position',[x-W/2,y-H/2,W,H],'Curvature',.1,'FaceColor',[.5 0.5 1],'LineWidth',1.5); % Draw cart 18 | rectangle('Position',[x-.9*W/2,0,wr,wr],'Curvature',1,'FaceColor',[0 0 0],'LineWidth',1.5); % Draw wheel 19 | rectangle('Position',[x+.9*W/2-wr,0,wr,wr],'Curvature',1,'FaceColor',[0 0 0],'LineWidth',1.5); % Draw wheel 20 | plot([x pendx],[y pendy],'k','LineWidth',2); % Draw pendulum 21 | rectangle('Position',[pendx-mr/2,pendy-mr/2,mr,mr],'Curvature',1,'FaceColor',[1 0.1 .1],'LineWidth',1.5); 22 | 23 | axis([-5 5 -2 2.5]);, axis equal 24 | set(gcf,'Position',[100 100 1000 400]) 25 | drawnow, hold off -------------------------------------------------------------------------------- /CH08/drawpend_FIG.m: -------------------------------------------------------------------------------- 1 | function drawpend(y,m,M,L) 2 | x = y(1); 3 | th = y(3); 4 | 5 | % kinematics 6 | % x = 3; % cart position 7 | % th = 3*pi/2; % pendulum angle 8 | 9 | % dimensions 10 | % L = 2; % pendulum length 11 | W = 1*sqrt(M/5); % cart width 12 | H = .5*sqrt(M/5); % cart height 13 | wr = .2; % wheel radius 14 | mr = .3*sqrt(m); % mass radius 15 | 16 | % positions 17 | % y = wr/2; % cart vertical position 18 | y = wr/2+H/2; % cart vertical position 19 | w1x = x-.9*W/2; 20 | w1y = 0; 21 | w2x = x+.9*W/2-wr; 22 | w2y = 0; 23 | 24 | px = x + L*sin(th); 25 | py = y - L*cos(th); 26 | 27 | plot([-10 10],[0 0],'k','LineWidth',2) 28 | hold on 29 | rectangle('Position',[x-W/2,y-H/2,W,H],'Curvature',.1,'FaceColor',[.5 0.5 1],'LineWidth',1.5) 30 | rectangle('Position',[w1x,w1y,wr,wr],'Curvature',1,'FaceColor',[0 0 0],'LineWidth',1.5) 31 | rectangle('Position',[w2x,w2y,wr,wr],'Curvature',1,'FaceColor',[0 0 0],'LineWidth',1.5) 32 | 33 | plot([x px],[y py],'k','LineWidth',2) 34 | 35 | rectangle('Position',[px-mr/2,py-mr/2,mr,mr],'Curvature',1,'FaceColor',[1 0.1 .1],'LineWidth',1.5) 36 | 37 | % set(gca,'YTick',[]) 38 | % set(gca,'XTick',[]) 39 | xlim([-5 5]); 40 | ylim([-2 2.5]); 41 | axis equal 42 | set(gcf,'Position',[100 550 1000 400]) 43 | % box off 44 | drawnow 45 | hold off -------------------------------------------------------------------------------- /CH08/pendcart.m: -------------------------------------------------------------------------------- 1 | function dx = pendcart(x,m,M,L,g,d,u) 2 | 3 | Sx = sin(x(3)); 4 | Cx = cos(x(3)); 5 | D = m*L*L*(M+m*(1-Cx^2)); 6 | 7 | dx(1,1) = x(2); 8 | dx(2,1) = (1/D)*(-m^2*L^2*g*Cx*Sx + m*L^2*(m*L*x(4)^2*Sx - d*x(2))) + m*L*L*(1/D)*u; 9 | dx(3,1) = x(4); 10 | dx(4,1) = (1/D)*((m+M)*m*g*L*Sx - m*L*Cx*(m*L*x(4)^2*Sx - d*x(2))) - m*L*Cx*(1/D)*u; -------------------------------------------------------------------------------- /CH08/simpend.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | m = 1; 4 | M = 5; 5 | L = 2; 6 | g = -10; 7 | d = 1; 8 | 9 | tspan = 0:.1:10; 10 | y0 = [0; 0; pi; .5]; 11 | [t,y] = ode45(@(t,y)pendcart(y,m,M,L,g,d,0),tspan,y0); 12 | 13 | for k=1:length(t) 14 | drawpend(y(k,:),m,M,L); 15 | end 16 | 17 | % function dy = pendcart(y,m,M,L,g,d,u) -------------------------------------------------------------------------------- /CH09/BPOD.m: -------------------------------------------------------------------------------- 1 | sysBPOD = BPOD(sysFull,sysAdj,r) 2 | 3 | [yFull,t,xFull] = impulse(sysFull,0:1:(r*5)+1); 4 | sysAdj = ss(sysFull.A',sysFull.C',sysFull.B',sysFull.D',-1); 5 | [yAdj,t,xAdj] = impulse(sysAdj,0:1:(r*5)+1); 6 | % Not the fastest way to compute, but illustrative 7 | % Both xAdj and xFull are size m x n x 2 8 | HankelOC = []; % Compute Hankel matrix H=OC 9 | for i=2:size(xAdj,1) % Start at 2 to avoid the D matrix 10 | Hrow = []; 11 | for j=2:size(xFull,1) 12 | Ystar = permute(squeeze(xAdj(i,:,:)),[2 1]); 13 | MarkovParameter = Ystar*squeeze(xFull(j,:,:)); 14 | Hrow = [Hrow MarkovParameter]; 15 | end 16 | HankelOC = [HankelOC; Hrow]; 17 | end 18 | [U,Sig,V] = svd(HankelOC); 19 | Xdata = []; 20 | Ydata = []; 21 | for i=2:size(xFull,1) % Start at 2 to avoid the D matrix 22 | Xdata = [Xdata squeeze(xFull(i,:,:))]; 23 | Ydata = [Ydata squeeze(xAdj(i,:,:))]; 24 | end 25 | Phi = Xdata*V*Sig^(-1/2); 26 | Psi = Ydata*U*Sig^(-1/2); 27 | Ar = Psi(:,1:r)'*sysFull.a*Phi(:,1:r); 28 | Br = Psi(:,1:r)'*sysFull.b; 29 | Cr = sysFull.c*Phi(:,1:r); 30 | Dr = sysFull.d; 31 | sysBPOD = ss(Ar,Br,Cr,Dr,-1); 32 | -------------------------------------------------------------------------------- /CH09/CH09_SEC02_1_GramianPlot.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | A = [-.75 1; -.3 -.75]; 4 | B = [2; 1]; 5 | C = [1 2]; 6 | D = 0; 7 | 8 | sys = ss(A,B,C,D); 9 | 10 | Wc = gram(sys,'c'); % Controllability Gramian 11 | Wo = gram(sys,'o'); % Observability Gramian 12 | 13 | [sysb,g,Ti,T] = balreal(sys); % Balance the system 14 | 15 | BWc = gram(sysb,'c') % Balanced Gramians 16 | BWo = gram(sysb,'o') 17 | 18 | %% Plot Gramians 19 | theta = 0:.01:2*pi; 20 | xc = cos(theta); 21 | yc = sin(theta); 22 | CIRC = [xc; yc]; 23 | 24 | ELLIPb = Ti*sqrt(BWc)*T*CIRC; 25 | ELLIPc = sqrt(Wc)*CIRC; 26 | ELLIPo = sqrt(Wo)*CIRC; 27 | 28 | plot(xc,yc,'k--','LineWidth',2) 29 | hold on 30 | 31 | % Draw controllability Gramian (unbalanced) 32 | plot(ELLIPc(1,:),ELLIPc(2,:),'r','LineWidth',2) 33 | patch(ELLIPc(1,:),ELLIPc(2,:),'r','FaceAlpha',.75) 34 | 35 | % Draw observability Gramian (unbalanced) 36 | plot(ELLIPo(1,:),ELLIPo(2,:),'b','LineWidth',2) 37 | patch(ELLIPo(1,:),ELLIPo(2,:),'b','FaceAlpha',.75) 38 | 39 | % Draw balanced Gramians 40 | patch(ELLIPb(1,:),ELLIPb(2,:),'k','FaceColor',[.5 0 .5],'FaceAlpha',.25) 41 | plot(ELLIPb(1,:),ELLIPb(2,:),'Color',[.35 0 .35],'LineWidth',2) 42 | 43 | %% Formatting 44 | axis equal, grid on 45 | set(gcf,'Position',[1 1 550 400]) 46 | set(gcf,'PaperPositionMode','auto') 47 | % print('-depsc2', '-loose', '../figures/FIG_BT_GRAM'); 48 | 49 | %% Manually compute scaled balancing transformation 50 | [Tu,D] = eig(Wc*Wo); % Tu are unscaled e-vecs 51 | 52 | Atu = inv(Tu)*A*Tu; 53 | Btu = inv(Tu)*B; 54 | Ctu = C*Tu; 55 | Dtu = 0; 56 | syst = ss(Atu,Btu,Ctu,Dtu); 57 | 58 | Sigmac = gram(syst,'c') % Diagonal Gramians 59 | Sigmao = gram(syst,'o') % (but not equal) 60 | Sigmas = diag(Sigmac)./diag(Sigmao); 61 | 62 | % Scaled balancing transformation 63 | T = Tu*diag(Sigmas.^(1/4)); 64 | 65 | % Permute columns of T to order Sigma 66 | Sigma = diag(Sigmac).^(1/2).*diag(Sigmao).^(1/2); 67 | [sigsort,permind] = sort(Sigma,'descend'); 68 | T = T(:,permind); % Hierarchical 69 | 70 | % Compute balanced system 71 | At = inv(T)*A*T; 72 | Bt = inv(T)*B; 73 | Ct = C*T; 74 | Dt = 0; 75 | sysBal = ss(At,Bt,Ct,Dt); 76 | 77 | BWc = gram(sysBal,'c') % Balanced Gramians 78 | BWo = gram(sysBal,'o') 79 | 80 | 81 | 82 | %% 83 | % ELLIPb = sqrt(BWc)*CIRC; 84 | 85 | % A = [-1 2; 0 -1]; 86 | % B = [1; 1]; 87 | % C = [1 1]; 88 | % D = 0; 89 | 90 | 91 | % A = [-1 2; -.5 -1]; 92 | % B = [1; 1]; 93 | % C = [1 1]; 94 | % D = 0; -------------------------------------------------------------------------------- /CH09/CH09_SEC03_Fig9p5.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | %% Figure 1 - simple impulse response 4 | % sys = drss(10,1,1); 5 | load testSys_Fig9p5 6 | t = 0:1:100; 7 | [y,t]=impulse(sys,t); 8 | 9 | t = [(-10:1:-1)'; t]; 10 | y = [zeros(10,1); y]; 11 | u = y*0; 12 | u(11) = 1; 13 | 14 | figure 15 | subplot(2,1,1) 16 | stairs(t,u,'k','LineWidth',1.5) 17 | grid on 18 | axis([-10 100 -.1 1.1]) 19 | set(gca,'Color','none') 20 | 21 | subplot(2,1,2) 22 | stairs(t,y,'k','LineWidth',1.5) 23 | grid on 24 | axis([-10 100 -.25 2.5]) 25 | set(gca,'Color','none') 26 | set(gcf,'Position',[100 100 250 175]) 27 | set(gcf,'PaperPositionMode','auto') 28 | print('-depsc2', '-loose', '../figures/FIG_ERAOKID1'); 29 | 30 | 31 | %% Figure 2 - OKID response 32 | t = 0:1:100; 33 | u = 0*t; 34 | u(1:50) = randn(50,1); 35 | [y,t]=lsim(sys,u,t); 36 | 37 | t = [(-10:1:-1)'; t]; 38 | y = [zeros(10,1); y]; 39 | u = [zeros(1,10) u]; 40 | 41 | figure 42 | subplot(2,1,1) 43 | stairs(t,u,'k','LineWidth',1.5) 44 | set(gca,'Color','none') 45 | axis([-10 100 min(u)-.5 max(u)+.5]) 46 | grid on 47 | 48 | subplot(2,1,2) 49 | stairs(t,y,'k','LineWidth',1.5) 50 | axis([-10 100 min(y)-.5 max(y)+.5]) 51 | set(gca,'Color','none') 52 | grid on 53 | set(gcf,'Position',[100 100 250 175]) 54 | set(gcf,'PaperPositionMode','auto') 55 | print('-depsc2', '-loose', '../figures/FIG_ERAOKID2'); -------------------------------------------------------------------------------- /CH09/ERA.m: -------------------------------------------------------------------------------- 1 | function [Ar,Br,Cr,Dr,HSVs] = ERA(YY,m,n,nin,nout,r) 2 | for i=1:nout 3 | for j=1:nin 4 | Dr(i,j) = YY(i,j,1); 5 | Y(i,j,:) = YY(i,j,2:end); 6 | end 7 | end 8 | 9 | % Yss = Y(1,1,end); 10 | % Y = Y-Yss; 11 | % Y(i,j,k):: 12 | % i refers to i-th output 13 | % j refers to j-th input 14 | % k refers to k-th timestep 15 | 16 | % nin,nout number of inputs and outputs 17 | % m,n dimensions of Hankel matrix 18 | % r, dimensions of reduced model 19 | 20 | assert(length(Y(:,1,1))==nout); 21 | assert(length(Y(1,:,1))==nin); 22 | assert(length(Y(1,1,:))>=m+n); 23 | 24 | for i=1:m 25 | for j=1:n 26 | for Q=1:nout 27 | for P=1:nin 28 | H(nout*i-nout+Q,nin*j-nin+P) = Y(Q,P,i+j-1); 29 | H2(nout*i-nout+Q,nin*j-nin+P) = Y(Q,P,i+j); 30 | end 31 | end 32 | end 33 | end 34 | 35 | [U,S,V] = svd(H,'econ'); 36 | Sigma = S(1:r,1:r); 37 | Ur = U(:,1:r); 38 | Vr = V(:,1:r); 39 | Ar = Sigma^(-.5)*Ur'*H2*Vr*Sigma^(-.5); 40 | Br = Sigma^(-.5)*Ur'*H(:,1:nin); 41 | Cr = H(1:nout,:)*Vr*Sigma^(-.5); 42 | HSVs = diag(S); -------------------------------------------------------------------------------- /CH09/OKID.m: -------------------------------------------------------------------------------- 1 | function [H, M] = OKID(y,u,r) 2 | 3 | % Steve Brunton, November 2010 4 | % OKID code, based on 1991 NASA TM-104069 by Juang, Phan, Horta and Longman 5 | % inputs: y (sampled output), u (sampled input), r (effective system order) 6 | % outputs: H (Markov parameters), M (Observer gain) 7 | 8 | % lowercase u,y indicate sampled data 9 | % double uppercase UU, YY indicate bold-faced quantities in paper 10 | % single uppercase U, Y indicate script quantities in paper 11 | 12 | % Step 0, check shapes of y,u 13 | yshape = size(y); 14 | PP = yshape(1); % q is the number of outputs 15 | MM = yshape(2); % L is the number of output samples 16 | ushape = size(u); 17 | QQ = ushape(1); % m is the number of inputs 18 | lu = ushape(2); % Lu i the number of input samples 19 | assert(MM==lu); % L and Lu need to be the same length 20 | 21 | 22 | % Step 1, choose p (4 or 5 times effective system order) 23 | LL = r*5; 24 | 25 | 26 | % Step 2, form data matrices y and V as shown in Eq. (7), solve for observer Markov parameters, Ybar 27 | V = zeros(QQ + (QQ+PP)*LL,MM); 28 | for i=1:MM 29 | V(1:QQ,i) = u(1:QQ,i); 30 | end 31 | for i=2:LL+1 32 | for j=1:MM+1-i 33 | vtemp = [u(:,j);y(:,j)]; 34 | V(QQ+(i-2)*(QQ+PP)+1:QQ+(i-1)*(QQ+PP),i+j-1) = vtemp; 35 | % V((i-1)*(m+q):i*(m+q)-1,i+j-1) = vtemp; 36 | end 37 | end 38 | Ybar = y*pinv(V,1.e-3); 39 | 40 | % Step 3, isolate system Markov parameters H, and observer gain M 41 | D = Ybar(:,1:QQ); % feed-through term (or D matrix) is the first term 42 | 43 | for i=1:LL 44 | Ybar1(1:PP,1:QQ,i) = Ybar(:,QQ+1+(QQ+PP)*(i-1):QQ+(QQ+PP)*(i-1)+QQ); 45 | Ybar2(1:PP,1:QQ,i) = Ybar(:,QQ+1+(QQ+PP)*(i-1)+QQ:QQ+(QQ+PP)*i); 46 | end 47 | Y(:,:,1) = Ybar1(:,:,1) + Ybar2(:,:,1)*D; 48 | for k=2:LL 49 | Y(:,:,k) = Ybar1(:,:,k) + Ybar2(:,:,k)*D; 50 | for i=1:k-1 51 | Y(:,:,k) = Y(:,:,k) + Ybar2(:,:,i)*Y(:,:,k-i); 52 | end 53 | end 54 | 55 | H = D; 56 | H(:,:,1) = D; 57 | for k=2:LL+1 58 | H(:,:,k) = Y(:,:,k-1); 59 | end 60 | 61 | % H = Ybar; 62 | M = 0; % not computed yet! -------------------------------------------------------------------------------- /CH09/testSys.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH09/testSys.mat -------------------------------------------------------------------------------- /CH09/testSys_Fig9p5.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH09/testSys_Fig9p5.mat -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/CH10_SEC02_Fig10p6.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | L = 2; 3 | dx = .25; 4 | [X,Y,Z] = meshgrid(-L:dx:L,-L:dx:L,-L:dx:L); 5 | V = randn(size(X)); 6 | 7 | Vstruct = peaks(size(X,1)); 8 | V(1,:,:) = squeeze(V(1,:,:)) + Vstruct; 9 | V(:,1,:) = squeeze(V(:,1,:)) + 1.5*flipud(Vstruct); 10 | V(:,:,end) = squeeze(V(:,:,end)) + fliplr(flipud(Vstruct)); 11 | 12 | Sx = -L; 13 | Sy = -L; 14 | Sz = L; 15 | figure 16 | slice(X,Y,Z,V,Sx,Sy,Sz) 17 | 18 | axis([-L,L,-L,L,-L,L]) 19 | xtickvals = [-L:2*dx:L]; 20 | set(gca,'XTick',xtickvals,'YTick',xtickvals,'ZTick',xtickvals); 21 | set(gca,'XTickLabels',{},'YTickLabels',{},'ZTickLabels',{}); 22 | set(gca,'LineWidth',1.5) 23 | daspect([1,1,1]) 24 | hold on 25 | shading interp 26 | lighting phong 27 | box on 28 | grid on 29 | 30 | plot3(squeeze(X(1,1:2:end,1:2:end)),squeeze(Y(1,1:2:end,1:2:end)),squeeze(Z(1,1:2:end,1:2:end)),'k','LineWidth',1) 31 | plot3(squeeze(Z(1,1:2:end,1:2:end)),squeeze(Y(1,1:2:end,1:2:end)),squeeze(X(1,1:2:end,1:2:end)),'k','LineWidth',1) 32 | 33 | plot3(squeeze(X(1:2:end,1,1:2:end)),squeeze(Z(1:2:end,1,1:2:end)),squeeze(Y(1:2:end,1,1:2:end)),'k','LineWidth',1) 34 | plot3(squeeze(X(1:2:end,1,1:2:end)),squeeze(Y(1:2:end,1,1:2:end)),squeeze(Z(1:2:end,1,1:2:end)),'k','LineWidth',1) 35 | 36 | plot3(squeeze(X(1:2:end,1:2:end,1)),squeeze(Y(1:2:end,1:2:end,1)),squeeze(Z(1:2:end,1:2:end,end)),'k','LineWidth',1) 37 | plot3(squeeze(Y(1:2:end,1:2:end,1)),squeeze(X(1:2:end,1:2:end,1)),squeeze(Z(1:2:end,1:2:end,end)),'k','LineWidth',1) 38 | 39 | colormap hot 40 | 41 | set(gcf,'Position',[100 100 375 400]) 42 | set(gcf,'PaperPositionMode','auto') 43 | print('-dpng', '-loose', '../figures/GA_PID_CUBE'); 44 | -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/CH10_SEC02_GA_PID.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | dt = 0.001; 4 | PopSize = 25; 5 | MaxGenerations = 10; 6 | s = tf('s'); 7 | G = 1/(s*(s*s+s+1)); 8 | 9 | options = optimoptions(@ga,'PopulationSize',PopSize,'MaxGenerations',MaxGenerations,'OutputFcn',@myfun); 10 | [x,fval] = ga(@(K)pidtest(G,dt,K),3,-eye(3),zeros(3,1),[],[],[],[],[],options); -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/history.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH10/CH10_SEC02_GA_PID/history.mat -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/myfun.m: -------------------------------------------------------------------------------- 1 | function [state, options,optchanged] = myfun(options,state,flag) 2 | persistent history 3 | persistent cost 4 | optchanged = false; 5 | 6 | switch flag 7 | case 'init' 8 | history(:,:,1) = state.Population; 9 | cost(:,1) = state.Score; 10 | case {'iter','interrupt'} 11 | ss = size(history,3); 12 | history(:,:,ss+1) = state.Population; 13 | cost(:,ss+1) = state.Score; 14 | case 'done' 15 | ss = size(history,3); 16 | history(:,:,ss+1) = state.Population; 17 | cost(:,ss+1) = state.Score; 18 | save history.mat history cost 19 | end -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/pidtest.m: -------------------------------------------------------------------------------- 1 | function J = pidtest(G,dt,parms) 2 | 3 | s = tf('s'); 4 | K = parms(1) + parms(2)/s + parms(3)*s/(1+.001*s); 5 | Loop = series(K,G); 6 | ClosedLoop = feedback(Loop,1); 7 | t = 0:dt:20; 8 | [y,t] = step(ClosedLoop,t); 9 | 10 | CTRLtf = K/(1+K*G); 11 | u = lsim(K,1-y,t); 12 | 13 | Q = 1; 14 | R = .001; 15 | J = dt*sum(Q*(1-y(:)).^2 + R*u(:).^2) 16 | 17 | step(ClosedLoop,t) 18 | h = findobj(gcf,'type','line'); 19 | set(h,'linewidth',2); 20 | drawnow -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/randpop.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH10/CH10_SEC02_GA_PID/randpop.mat -------------------------------------------------------------------------------- /CH10/CH10_SEC02_GA_PID/testLQR.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | s = tf('s'); 4 | % G = 1/(s*(s*s+s+1)); 5 | G = 1/(s+1); 6 | 7 | [A,B,C,D] = tf2ss(G.num{1},G.den{1}); 8 | sys = ss(A,B,C,D); 9 | 10 | Q = 1; 11 | R = .001; 12 | 13 | K = lqr(A,B,Q,R); 14 | 15 | % compare with PID... integral reduces ss error, but requries ss control, 16 | % so not optimal... -------------------------------------------------------------------------------- /CH10/CH10_SEC03_ESC_Krstic_ex1p3/NEWkrstic_example_1p3_wnoise.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH10/CH10_SEC03_ESC_Krstic_ex1p3/NEWkrstic_example_1p3_wnoise.slx -------------------------------------------------------------------------------- /CH10/CH10_SEC03_ESC_Krstic_ex1p3/krstic_1p3.m: -------------------------------------------------------------------------------- 1 | s = tf([1 0],1); 2 | Fi = (s-1)/(s^2+3*s+2); 3 | Fo = 1/(s+1); 4 | 5 | F = Fi*Fo; 6 | step(F) 7 | bode(F) 8 | % clear all 9 | % t = 0:.001:100; 10 | % thetastar(:,1) = t; 11 | % thetastar(:,2) = .01*exp(.01*t); 12 | % fstar(:,1) = t; 13 | % fstar(:,2) = [(0:.001:10)*0 (10.001:.001:100)*0+.01]; -------------------------------------------------------------------------------- /CH10/CH10_SEC03_ESC_Krstic_ex1p3/plotresponse.m: -------------------------------------------------------------------------------- 1 | clear all 2 | 3 | s = tf('s'); 4 | Fi = (s-2)/(s^2+4*s+3); 5 | Fo = 1/(s+2); 6 | 7 | F = Fi*Fo; 8 | 9 | t = 0:.001:100; 10 | thetastar(:,1) = t; 11 | thetastar(:,2) = .01 + .001*t; 12 | fstar(:,1) = t; 13 | fstar(:,2) = [(0:.001:10)*0 (10.001:.001:t(end))*0+.05]; 14 | %% 15 | sim('NEWkrstic_example_1p3_wnoise',[0 t(end)]); 16 | 17 | %% 18 | figure 19 | subplot(2,1,2) 20 | plot(tout,y,'LineWidth',1.2); 21 | hold on 22 | plot(fstar(:,1),fstar(:,2),'LineWidth',1.2); 23 | grid on 24 | l1=legend('J','J^*'); 25 | set(l1,'Location','SouthEast') 26 | % xlabel('Time (s)') 27 | % ylabel('Output') 28 | subplot(2,1,1) 29 | plot(tout,theta,'LineWidth',1.2); 30 | hold on 31 | plot(thetastar(:,1),thetastar(:,2),'LineWidth',1.2); 32 | grid on 33 | l1=legend('\theta','\theta^*'); 34 | set(l1,'Location','SouthEast') 35 | 36 | 37 | % xlabel('Time (s)') 38 | % ylabel('Theta') 39 | % 40 | % subplot(2,2,[2 4]) 41 | % bode(F) 42 | set(gcf,'Position',[100 100 500 350]) 43 | set(gcf,'PaperPositionMode','auto') 44 | print('-depsc2', '-loose', '../../../figures/ESC_ResponseHard'); 45 | -------------------------------------------------------------------------------- /CH10/CH10_SEC03_ESCfixed.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % J = @(u,t)(25-(5-(u-t))^2); 4 | 5 | J = @(u,t)(25-(5-(u)).^2); 6 | y0 = J(0,0); % u = 0 7 | 8 | % Extremum Seeking Control Parameters 9 | freq = 10*2*pi; % sample frequency 10 | dt = 1/freq; 11 | T = 10; % total period of simulation (in seconds) 12 | A = .2; % amplitude 13 | omega = 10*2*pi; % 10 Hz 14 | phase = 0; 15 | K = 5; % integration gain 16 | 17 | % High pass filter (Butterworth filter) 18 | butterorder=1; 19 | butterfreq=2; % in Hz for 'high' 20 | [b,a] = butter(butterorder,butterfreq*dt*2,'high') 21 | ys = zeros(1,butterorder+1)+y0; 22 | HPF=zeros(1,butterorder+1); 23 | 24 | uhat=u; 25 | for i=1:T/dt 26 | t = (i-1)*dt; 27 | yvals(i)=J(u,t); 28 | 29 | for k=1:butterorder 30 | ys(k) = ys(k+1); 31 | HPF(k) = HPF(k+1); 32 | end 33 | ys(butterorder+1) = yvals(i); 34 | HPFnew = 0; 35 | for k=1:butterorder+1 36 | HPFnew = HPFnew + b(k)*ys(butterorder+2-k); 37 | end 38 | for k=2:butterorder+1 39 | HPFnew = HPFnew - a(k)*HPF(butterorder+2-k); 40 | end 41 | HPF(butterorder+1) = HPFnew; 42 | 43 | xi = HPFnew*sin(omega*t + phase); 44 | uhat = uhat + xi*K*dt; 45 | u = uhat + A*sin(omega*t + phase); 46 | uhats(i) = uhat; 47 | uvals(i) = u; 48 | end 49 | 50 | %% 51 | figure 52 | subplot(2,1,1) 53 | plot(t,uvals,t,uhats,'LineWidth',1.2) 54 | l1=legend('$u$','$\hat{u}$') 55 | set(l1,'interpreter','latex','Location','NorthWest') 56 | grid on 57 | subplot(2,1,2) 58 | plot(t,yvals,'LineWidth',1.2) 59 | ylim([-1 26]) 60 | grid on 61 | 62 | set(gcf,'Position',[100 100 500 350]) 63 | set(gcf,'PaperPositionMode','auto') 64 | % print('-depsc2', '-loose', '../../../figures/ESC_Response'); -------------------------------------------------------------------------------- /CH10/CH10_SEC03_ESCsinusoidal.m: -------------------------------------------------------------------------------- 1 | clear all, close all, clc 2 | 3 | % J = @(u,t)(25-(5-(u-t))^2); 4 | 5 | J = @(u,t)(25-(5-(u)-sin(t)).^2); 6 | u = 0; 7 | y0 = J(u,0); 8 | 9 | % Extremum Seeking Control Parameters 10 | freq = 100; % sample frequency 11 | dt = 1/freq; 12 | T = 100; % total period of simulation (in seconds) 13 | A = .2; % amplitude 14 | omega = 10*2*pi; % 10 Hz 15 | phase = 0; 16 | K = 5; % integration gain 17 | 18 | % high pass filter 19 | butterorder=1; 20 | butterfreq=2; % in Hz for 'high' 21 | [b,a] = butter(butterorder,butterfreq*dt*2,'high') 22 | ys = zeros(1,butterorder+1)+y0; 23 | HPF=zeros(1,butterorder+1); 24 | 25 | uhat=u; 26 | for i=1:T/dt 27 | t = (i-1)*dt; 28 | time(i) = t; 29 | yvals(i)=J(u,t); 30 | 31 | for k=1:butterorder 32 | ys(k) = ys(k+1); 33 | HPF(k) = HPF(k+1); 34 | end 35 | ys(butterorder+1) = yvals(i); 36 | HPFnew = 0; 37 | for k=1:butterorder+1 38 | HPFnew = HPFnew + b(k)*ys(butterorder+2-k); 39 | end 40 | for k=2:butterorder+1 41 | HPFnew = HPFnew - a(k)*HPF(butterorder+2-k); 42 | end 43 | HPF(butterorder+1) = HPFnew; 44 | 45 | xi = HPFnew*sin(omega*t + phase); 46 | uhat = uhat + xi*K*dt; 47 | u = uhat + A*sin(omega*t + phase); 48 | uhats(i) = uhat; 49 | uvals(i) = u; 50 | end 51 | 52 | %% 53 | figure 54 | subplot(2,1,1) 55 | plot(time,uvals,time,uhats,'LineWidth',1.2) 56 | l1=legend('$u$','$\hat{u}$') 57 | set(l1,'interpreter','latex','Location','SouthEast') 58 | grid on 59 | subplot(2,1,2) 60 | plot(time,yvals,'LineWidth',1.2) 61 | ylim([-1 26]) 62 | grid on 63 | 64 | set(gcf,'Position',[100 100 500 350]) 65 | set(gcf,'PaperPositionMode','auto') 66 | print('-depsc2', '-loose', '../../../figures/ESC_ResponseVarying'); -------------------------------------------------------------------------------- /CH11/CH11_SEC01_1_Fig11p1.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=20; n=80; 4 | x2=linspace(-L/2,L/2,n+1); 5 | x=x2(1:n); 6 | u=exp(-x.^2); u1=exp(-0.1*x.^2); u2=exp(-10*x.^2); 7 | ut=fft(u); uts=fftshift(ut); 8 | ut1=fft(u1); uts1=fftshift(ut1); 9 | ut2=fft(u2); uts2=fftshift(ut2); 10 | 11 | k=(2*pi/L)*[0:(n/2-1) (-n/2):-1]; 12 | km=[0:n/2-1 -n/2:-1]; 13 | 14 | 15 | subplot(3,2,1), plot(x,u2,'b-o',x,u1,'r-o',x,u,'k-o') 16 | set(gca,'Xlim',[-10 10],'Xtick',[-10 0 10],'Fontsize',[15]) 17 | text(-13.5,0.8,'u(x)','Fontsize',[15]) 18 | text(7,-0.2,'x','Fontsize',[15]) 19 | text(-8,0.8,'(a)','Fontsize',[15]) 20 | 21 | 22 | 23 | 24 | subplot(3,2,2), plot(km,abs(ut2)/max(abs(ut2)),'b-o',km,abs(ut1)/max(abs(ut1)),'r-o',km,abs(ut)/max(abs(ut)),'k-o') 25 | set(gca,'Xlim',[-40 40],'Xtick',[-40 0 40],'Fontsize',[15]) 26 | text(-53,0.8,'u(k)','Fontsize',[15]) 27 | text(-53,0.86,'^','Fontsize',[15]) 28 | text(18,-0.2,'mode','Fontsize',[15]) 29 | text(-36,0.8,'(b)','Fontsize',[15]) 30 | 31 | 32 | ut21=zeros(1,n); 33 | ut22=zeros(1,n); 34 | ut23=zeros(1,n); 35 | 36 | for j=1:19 37 | 38 | for jj=1:j 39 | 40 | ut21(n/2+1-jj:n/2+1+jj) = uts(n/2+1-jj:n/2+1+jj); 41 | ut22(n/2+1-jj:n/2+1+jj) = uts1(n/2+1-jj:n/2+1+jj); 42 | ut23(n/2+1-jj:n/2+1+jj) = uts2(n/2+1-jj:n/2+1+jj); 43 | 44 | end 45 | ut31=fftshift(ut21); 46 | ut32=fftshift(ut22); 47 | ut33=fftshift(ut23); 48 | u31=ifft(ut31); 49 | u32=ifft(ut32); 50 | u33=ifft(ut33); 51 | subplot(3,1,2) 52 | plot(x,u31,'k'), hold on 53 | 54 | % figure(2) 55 | % subplot(2,1,1) 56 | % plot(x,u32,'k'), hold on 57 | % 58 | % subplot(2,1,2) 59 | % plot(x,u33,'k'), hold on 60 | % figure(1) 61 | 62 | 63 | 64 | 65 | erx(j)=2*j+1 66 | er1(j)=norm(u-u31); 67 | er2(j)=norm(u1-u32); 68 | er3(j)=norm(u2-u33); 69 | 70 | 71 | end 72 | subplot(3,1,3) 73 | semilogy(erx,er3,'b',erx,er2,'r',erx,er1,'k','LineWidth',[2]), grid on 74 | 75 | subplot(3,1,2), set(gca,'Xlim',[-10 10],'Xtick',[-10 -5 0 5 10],'Ylim',[-.2 1],'Ytick',[0 0.5 1],'Fontsize',[15]) 76 | text(-13,0.8,'u(x)','Fontsize',[15]) 77 | text(-0.5,0.1,'N=3','Fontsize',[15]) 78 | text(1,0.85,'N=39','Fontsize',[15]) 79 | A=annotation('textarrow',[0.52 0.52],[0.48 0.60]); set(A,'String','','Fontsize',15) 80 | text(7,-0.4,'x','Fontsize',[15]) 81 | text(-9,0.8,'(c)','Fontsize',[15]) 82 | 83 | 84 | 85 | subplot(3,1,3), set(gca,'Fontsize',[15],'Ylim',[10^(-5) 10^1],'Ytick',[10^(-5) 10^(-4) 10^(-3) 10^(-2) 10^(-1) 10^(0) 10^(1)] ) 86 | text(-6,1,'Error','Fontsize',[15]) 87 | text(32,10^(-7),'# of modes','Fontsize',[15]) 88 | text(2,10^(-4),'(d)','Fontsize',[15]) 89 | 90 | figure(2) 91 | subplot(2,1,1) 92 | 93 | subplot(2,1,2) 94 | figure(1) 95 | 96 | -------------------------------------------------------------------------------- /CH11/CH11_SEC05_1_Invariance.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; 4 | L=20; x=linspace(-L,L,n); y=x; 5 | [X,Y]=meshgrid(x,y); 6 | 7 | Xd=[]; 8 | for j=1:100 9 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))+j/10); 10 | f=exp(-0.01*(X.^2+Y.^2)); 11 | uf=u.*f; 12 | Xd(:,j)=reshape(uf,n^2,1); 13 | pcolor(x,y,uf), shading interp, colormap(hot) 14 | end 15 | 16 | 17 | 18 | %% 19 | [U,S,V]=svd(Xd,0); 20 | 21 | figure(2) 22 | subplot(4,1,3) 23 | plot(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 24 | subplot(4,1,4) 25 | semilogy(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 26 | subplot(2,1,1) 27 | plot(V(:,1:4),'Linewidth',[2]) 28 | legend('mode1','mode2','mode3','mode4') 29 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 30 | subplot(4,1,3), set(gca,'Fontsize',[15],'Ylim',[0 60],'Ytick',[0 20 40 60],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 31 | subplot(4,1,4), set(gca,'Fontsize',[15],'Ylim',[10^(-20) 10^2],'Ytick',[10^(-20) 10^(-10) 10^2],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 32 | 33 | figure(3) 34 | for j=1:4 35 | subplot(4,4,j) 36 | mode=reshape(U(:,j),n,n); 37 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 38 | axis off 39 | end 40 | 41 | %% 42 | 43 | figure(11) 44 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))); 45 | f=exp(-0.01*(X.^2+Y.^2)); 46 | uf=u.*f; 47 | subplot(3,3,1),pcolor(x,y,uf), shading interp, caxis([-1 1]), axis off 48 | subplot(3,3,2),pcolor(x,y,abs(uf)), shading interp, caxis([-1 1]), axis off 49 | subplot(3,3,3),pcolor(x,y,uf.^5), shading interp, caxis([-1 1]), axis off 50 | colormap(gray) 51 | 52 | 53 | 54 | 55 | %% TRANSLATION 56 | figure(5) 57 | n=200; L=20; x=linspace(-L,L,n); y=x; % space 58 | m=41; T=10; t=linspace(0,T,m); % time 59 | c=3; % wave speed 60 | 61 | X=[]; 62 | for j=1:m 63 | X(:,j)=exp(-(x+15-c*t(j)).^2).'; % data snapshots 64 | end 65 | [U,S,V]=svd(X); % SVD decomposition 66 | 67 | 68 | %% 69 | figure(6) 70 | subplot(2,2,1) 71 | waterfall(x,t,X.'),colormap([0 0 0]) 72 | view(20,75) 73 | set(gca,'Xlim',[-20 20],'Xtick',[-20 -10 0 10 20],'Ylim',[0 10], ... 74 | 'Ytick',[0 5 10],'Zlim',[0 1],'Ztick',[0 1],'Fontsize',[12]) 75 | 76 | 77 | [U2,S2,V2]=svd(X); 78 | 79 | subplot(4,2,2) 80 | plot(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 81 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[0 8],'Ytick',[0 4 8]) 82 | subplot(4,2,4) 83 | semilogy(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 84 | grid on 85 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[10^(-3) 2*10^1],'Ytick',[10^(-3) 10^(-2) 10^(-1) 10^0 10^1]) 86 | 87 | figure(8) 88 | subplot(2,1,1) 89 | plot(x,U2(:,1:4),'Linewidth',[2]); 90 | legend('mode1','mode2','mode3','mode4','Location','SouthEast') 91 | set(gca,'Fontsize',[15],'Ylim',[-0.15 0.15],'Ytick',[-0.15 0 0.15]) 92 | subplot(2,1,2) 93 | plot(t,V2(:,1:4),'Linewidth',[2]) 94 | set(gca,'Fontsize',[15],'Ylim',[-.3 0.3],'Ytick',[-0.3 0 0.3]) 95 | 96 | -------------------------------------------------------------------------------- /CH11/CH11_SEC05_1_Invariance_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; 4 | L=20; x=linspace(-L,L,n); y=x; 5 | [X,Y]=meshgrid(x,y); 6 | 7 | Xd=[]; 8 | for j=1:100 9 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))+j/10); 10 | f=exp(-0.01*(X.^2+Y.^2)); 11 | uf=u.*f; 12 | Xd(:,j)=reshape(uf,n^2,1); 13 | pcolor(x,y,uf), shading interp, colormap(hot), caxis([-1 1]), drawnow 14 | end 15 | 16 | 17 | 18 | %% 19 | [U,S,V]=svd(Xd,0); 20 | 21 | figure(2) 22 | subplot(4,1,3) 23 | plot(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 24 | subplot(4,1,4) 25 | semilogy(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 26 | subplot(2,1,1) 27 | plot(V(:,1:4),'Linewidth',[2]) 28 | legend('mode1','mode2','mode3','mode4') 29 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 30 | subplot(4,1,3), set(gca,'Fontsize',[15],'Ylim',[0 60],'Ytick',[0 20 40 60],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 31 | subplot(4,1,4), set(gca,'Fontsize',[15],'Ylim',[10^(-20) 10^2],'Ytick',[10^(-20) 10^(-10) 10^2],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 32 | 33 | figure(3) 34 | for j=1:4 35 | subplot(4,4,j) 36 | mode=reshape(U(:,j),n,n); 37 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 38 | axis off 39 | end 40 | 41 | %% 42 | 43 | figure(11) 44 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))); 45 | f=exp(-0.01*(X.^2+Y.^2)); 46 | uf=u.*f; 47 | subplot(3,3,1),pcolor(x,y,uf), shading interp, caxis([-1 1]), axis off 48 | subplot(3,3,2),pcolor(x,y,abs(uf)), shading interp, caxis([-1 1]), axis off 49 | subplot(3,3,3),pcolor(x,y,uf.^5), shading interp, caxis([-1 1]), axis off 50 | colormap(gray) 51 | 52 | 53 | 54 | 55 | %% TRANSLATION 56 | figure(5) 57 | n=200; L=20; x=linspace(-L,L,n); y=x; % space 58 | m=41; T=10; t=linspace(0,T,m); % time 59 | c=3; % wave speed 60 | 61 | X=[]; 62 | for j=1:m 63 | X(:,j)=exp(-(x+15-c*t(j)).^2).'; % data snapshots 64 | end 65 | [U,S,V]=svd(X); % SVD decomposition 66 | 67 | 68 | %% 69 | figure(6) 70 | subplot(2,2,1) 71 | waterfall(x,t,X.'),colormap([0 0 0]) 72 | view(20,75) 73 | set(gca,'Xlim',[-20 20],'Xtick',[-20 -10 0 10 20],'Ylim',[0 10], ... 74 | 'Ytick',[0 5 10],'Zlim',[0 1],'Ztick',[0 1],'Fontsize',[12]) 75 | 76 | 77 | [U2,S2,V2]=svd(X); 78 | 79 | subplot(4,2,2) 80 | plot(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 81 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[0 8],'Ytick',[0 4 8]) 82 | subplot(4,2,4) 83 | semilogy(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 84 | grid on 85 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[10^(-3) 2*10^1],'Ytick',[10^(-3) 10^(-2) 10^(-1) 10^0 10^1]) 86 | 87 | figure(8) 88 | subplot(2,1,1) 89 | plot(x,U2(:,1:4),'Linewidth',[2]); 90 | legend('mode1','mode2','mode3','mode4','Location','SouthEast') 91 | set(gca,'Fontsize',[15],'Ylim',[-0.15 0.15],'Ytick',[-0.15 0 0.15]) 92 | subplot(2,1,2) 93 | plot(t,V2(:,1:4),'Linewidth',[2]) 94 | set(gca,'Fontsize',[15],'Ylim',[-.3 0.3],'Ytick',[-0.3 0 0.3]) 95 | 96 | -------------------------------------------------------------------------------- /CH11/extra/POD_invariance2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; 4 | L=20; x=linspace(-L,L,n); y=x; 5 | [X,Y]=meshgrid(x,y); 6 | 7 | Xd=[]; 8 | for j=1:100 9 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))+j/10); 10 | f=exp(-0.01*(X.^2+Y.^2)); 11 | uf=u.*f; 12 | Xd1(:,j)=reshape(abs(uf),n^2,1); 13 | Xd2(:,j)=reshape(uf.^5,n^2,1); 14 | %pcolor(x,y,uf), shading interp, colormap(hot), caxis([-1 1]), drawnow 15 | end 16 | 17 | 18 | 19 | %% 20 | [U1,S1,V1]=svd(Xd1,0); 21 | [U2,S2,V2]=svd(Xd2,0); 22 | 23 | figure(2) 24 | subplot(4,1,1) 25 | plot(100*diag(S1)/sum(diag(S1)),'ro','Linewidth',[2]), hold on 26 | plot(100*diag(S2)/sum(diag(S2)),'bo','Linewidth',[2]) 27 | legend('|u|','u^5') 28 | subplot(4,1,2) 29 | semilogy(100*diag(S1)/sum(diag(S1)),'ro','Linewidth',[2]), hold on 30 | semilogy(100*diag(S2)/sum(diag(S2)),'bo','Linewidth',[2]) 31 | grid on 32 | subplot(4,1,3) 33 | plot(V1(:,1:4),'Linewidth',[2]) 34 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 35 | subplot(4,1,4) 36 | plot(V2(:,1:4),'Linewidth',[2]) 37 | legend('mode1','mode2','mode3','mode4') 38 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 39 | subplot(4,1,1), set(gca,'Fontsize',[15],'Ylim',[0 60],'Ytick',[0 20 40 60],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 40 | subplot(4,1,2), set(gca,'Fontsize',[15],'Ylim',[10^(-20) 10^2],'Ytick',[10^(-20) 10^(-10) 10^2],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 41 | 42 | 43 | figure(3) 44 | for j=1:4 45 | subplot(4,4,j) 46 | mode=reshape(U1(:,j),n,n); 47 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 48 | axis off 49 | subplot(4,4,j+4) 50 | mode=reshape(U2(:,j),n,n); 51 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 52 | axis off 53 | end 54 | 55 | -------------------------------------------------------------------------------- /CH11/extra/ch_ROM_rom_nls.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=30; n=256; 4 | x2=linspace(-L/2,L/2,n+1); x=x2(1:n); 5 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; 6 | t=linspace(0,2*pi,51); 7 | 8 | u=2*sech(x); 9 | ut=fft(u); 10 | 11 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 12 | 13 | for j=1:length(t) 14 | usol(j,:)=ifft(utsol(j,:)); 15 | end 16 | 17 | figure(1), subplot(2,2,1),surfl(x,t,abs(usol)); shading interp, colormap(gray); 18 | 19 | %% SVD 20 | 21 | X=usol.'; 22 | [u,s,v]=svd(X); 23 | figure(2) 24 | plot(diag(s)/sum(diag(s)),'ko','Linewidth',[2]) 25 | 26 | figure(3) 27 | plot(real(u(:,1:3)),'Linewidth',[2]), figure(4) 28 | 29 | %% 30 | r=4; % rank of projection 31 | phi=u(:,1:r); % Phi_r POD modes 32 | 33 | for j=1:r 34 | phixx(:,j)= -ifft((k.^2).*fft(phi(:,j))); % second derivatives 35 | a0(j)= 2*sech(x)*conj(phi(:,j)); % projection of initial conditions 36 | end 37 | L= (i/2)* phi'*phixx; % Low-rank approximation of linear term 38 | 39 | [t,asol]=ode45('ch_rom_a_rhs',t,a0,[],phi,L); 40 | 41 | us=zeros(n,length(t)); 42 | for j=1:length(t) 43 | for jj=1:r 44 | us(:,j)=us(:,j) + asol(j,jj)*phi(:,jj); % r-rank reconstruction 45 | end 46 | end 47 | 48 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 49 | 50 | figure(1) 51 | subplot(2,2,2) 52 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 53 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 54 | % zlabel('|u|','Fontsize',[15]) 55 | % xlabel('x','Fontsize',[15]) 56 | % ylabel('t','Fontsize',[15]) 57 | % text(-20,5,'(b)','Fontsize',[15]) 58 | 59 | subplot(2,2,1) 60 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 61 | % zlabel('|u|','Fontsize',[15]) 62 | % xlabel('x','Fontsize',[15]) 63 | % ylabel('t','Fontsize',[15]) 64 | % text(-20,10,'(a)','Fontsize',[15]) 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /CH11/extra/ch_ROM_rom_nls2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=30; n=256; 4 | x2=linspace(-L/2,L/2,n+1); x=x2(1:n); 5 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; 6 | t=linspace(0,2*pi,41); 7 | 8 | u=2*sech(x); 9 | ut=fft(u); 10 | 11 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 12 | 13 | for j=1:length(t) 14 | usol(j,:)=ifft(utsol(j,:)); 15 | end 16 | 17 | figure(1), subplot(2,2,1),surfl(x,t,abs(usol)); shading interp, colormap(gray); 18 | 19 | %% SVD 20 | 21 | X=usol.'; 22 | [u,s,v]=svd(X); 23 | figure(2) 24 | plot(diag(s)/sum(diag(s)),'ko','Linewidth',[2]) 25 | 26 | figure(3) 27 | plot(real(u(:,1:3)),'Linewidth',[2]), figure(4) 28 | 29 | %% 30 | 31 | for r=1:4 32 | %r=4; % rank of projection 33 | phi=u(:,1:r); % Phi_r POD modes 34 | 35 | for j=1:r 36 | phixx(:,j)= -ifft((k.^2).*fft(phi(:,j))); % second derivatives 37 | a0(j)= 2*sech(x)*conj(phi(:,j)); % projection of initial conditions 38 | end 39 | L= (i/2)* phi'*phixx; % Low-rank approximation of linear term 40 | 41 | [t,asol]=ode45('ch_rom_a_rhs',t,a0,[],phi,L); 42 | 43 | us=zeros(n,length(t)); 44 | for j=1:length(t) 45 | for jj=1:r 46 | us(:,j)=us(:,j) + asol(j,jj)*phi(:,jj); % r-rank reconstruction 47 | end 48 | end 49 | 50 | figure(1) 51 | subplot(2,2,r) 52 | waterfall(x,t,abs(us.')); colormap([0 0 0]); 53 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 54 | 55 | end 56 | 57 | 58 | 59 | break 60 | 61 | figure(1) 62 | subplot(2,2,2) 63 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 64 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 65 | % zlabel('|u|','Fontsize',[15]) 66 | % xlabel('x','Fontsize',[15]) 67 | % ylabel('t','Fontsize',[15]) 68 | % text(-20,5,'(b)','Fontsize',[15]) 69 | 70 | subplot(2,2,1) 71 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 72 | % zlabel('|u|','Fontsize',[15]) 73 | % xlabel('x','Fontsize',[15]) 74 | % ylabel('t','Fontsize',[15]) 75 | % text(-20,10,'(a)','Fontsize',[15]) 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /CH11/extra/ch_rom_a_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=a_rhs(t,a,dummy,phi,L) 2 | rhs= L*a + i*phi'*( (abs(phi*a).^2).*(phi*a) ); -------------------------------------------------------------------------------- /CH11/extra/ch_rom_deim.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | L=40; n=256; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); % spatial discretization 4 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; % wavenumbers for FFT 5 | t=linspace(0,2*pi,61); % time domain collection points 6 | 7 | N=2; 8 | u0=N*sech(x).'; 9 | ut=fft(u0); 10 | [t,utsol]=ode45('ch_pod_sol_rhs',t,ut,[],k); 11 | for j=1:length(t) 12 | usol(j,:)=ifft(utsol(j,:)); 13 | end 14 | 15 | figure(1) 16 | subplot(2,2,1) 17 | waterfall(x,t,abs(usol)), shading interp, colormap([0 0 0]) 18 | set(gca,'Ylim',[0 2*pi]), figure(1), subplot(2,2,2) 19 | 20 | %% 21 | X=usol.'; % data matrix X 22 | [U,S,W]=svd(X,0); % SVD reduction 23 | 24 | r=3; % select rank truncation 25 | Psi=U(:,1:r); % select POD modes 26 | a=Psi'*u0; % project initial conditions 27 | 28 | 29 | %% 30 | NL=i*(abs(X).^2).*X; 31 | [XI,S_NL,W]=svd(NL,0); 32 | 33 | % first DEIM point 34 | [Xi_max,nmax]=max(abs(XI(:,1))); 35 | XI_m=XI(:,1); 36 | z=zeros(n,1); 37 | P=z; P(nmax)=1; 38 | 39 | % DEIM points 2 to r 40 | for j=2:r 41 | c=(P'*XI_m)\(P'*XI(:,j)); 42 | res=XI(:,j)-XI_m*c; 43 | [Xi_max,nmax]=max(abs(res)); 44 | XI_m=[XI_m,XI(:,j)]; 45 | P=[P,z]; P(nmax,j)=1; 46 | end 47 | 48 | P_NL=Psi'*( XI_m*inv(P'*XI_m) ); % nonlinear projection 49 | P_Psi=P'*Psi; % interpolation of Psi 50 | 51 | for j=1:r % linear derivative terms 52 | Lxx(:,j)=ifft(-k.^2.*fft(Psi(:,j))); 53 | end 54 | L=(i/2)*(Psi')*Lxx; % projected linear term 55 | 56 | [tt,a]=ode45('ch_rom_deim_rhs',t,a,[],P_NL,P_Psi,L); 57 | Xtilde=Psi*a'; % DEIM approximation 58 | waterfall(x,t,abs(Xtilde')), shading interp, colormap([0 0 0]) 59 | set(gca,'Ylim',[0 2*pi]) 60 | 61 | 62 | -------------------------------------------------------------------------------- /CH11/extra/ch_rom_deim2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | L=40; n=512; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); % spatial discretization 4 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; % wavenumbers for FFT 5 | t=linspace(0,2*pi,61); % time domain collection points 6 | 7 | N=2; 8 | u0=N*sech(x).'; 9 | ut=fft(u0); 10 | [t,utsol]=ode45('ch_pod_sol_rhs',t,ut,[],k); 11 | for j=1:length(t) 12 | usol(j,:)=ifft(utsol(j,:)); 13 | end 14 | 15 | figure(1) 16 | subplot(2,2,1) 17 | waterfall(x,t,abs(usol)), shading interp, colormap([0 0 0]) 18 | set(gca,'Ylim',[0 2*pi]), figure(1), subplot(2,2,2) 19 | 20 | %% 21 | X=usol.'; % data matrix X 22 | [U,S,W]=svd(X,0); % SVD reduction 23 | 24 | r=3; % select rank truncation 25 | Psi=U(:,1:r); % select POD modes 26 | a=Psi'*u0; % project initial conditions 27 | 28 | 29 | %% 30 | NL=i*(abs(X).^2).*X; 31 | [XI,S_NL,W]=svd(NL,0); 32 | 33 | % first DEIM point 34 | [Xi_max,nmax]=max(abs(XI(:,1))); 35 | XI_m=XI(:,1); 36 | z=zeros(n,1); 37 | P=z; P(nmax)=1; 38 | 39 | % DEIM points 2 to r 40 | for j=2:r 41 | c=(P'*XI_m)\(P'*XI(:,j)); 42 | res=XI(:,j)-XI_m*c; 43 | [Xi_max,nmax]=max(abs(res)); 44 | XI_m=[XI_m,XI(:,j)]; 45 | P=[P,z]; P(nmax,j)=1; 46 | end 47 | 48 | P_NL=Psi'*( XI_m*inv(P'*XI_m) ); % nonlinear projection 49 | P_Psi=P'*Psi; % interpolation of Psi 50 | 51 | for j=1:r % linear derivative terms 52 | Lxx(:,j)=ifft(-k.^2.*fft(Psi(:,j))); 53 | end 54 | L=(i/2)*(Psi')*Lxx; % projected linear term 55 | 56 | [tt,a]=ode45('ch_rom_deim_rhs',t,a,[],P_NL,P_Psi,L); 57 | Xtilde=Psi*a'; % DEIM approximation 58 | waterfall(x,t,abs(Xtilde')), shading interp, colormap([0 0 0]) 59 | set(gca,'Ylim',[0 2*pi]) 60 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 61 | subplot(2,2,1) 62 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 63 | 64 | 65 | figure(1) 66 | subplot(2,1,2), plot(x,-Psi(:,1:3)) 67 | legend('mode 1','mode 2','mode 3','Fontsize',[15]) 68 | 69 | set(gca,'Xlim',[-4 4],'Xtick',[-4 -2 0 2 4],'Ylim',[-0.4 0.4],'Ytick',[-0.4 0 0.4],'Fontsize',[15]) 70 | hold on 71 | bar(x,P(:,1:3)) 72 | bar(x,-P(:,1:3)) 73 | 74 | -------------------------------------------------------------------------------- /CH11/extra/ch_rom_deim_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=ch_rom_deim_rhs(tspan, a,dummy,P_NL,P_Psi,L) 2 | N=P_Psi*a; 3 | rhs=L*a + i*P_NL*((abs(N).^2).*N); 4 | -------------------------------------------------------------------------------- /CH11/harm_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=pod_harm_rhs(t,ut,dummy,k,V) 2 | u=ifft(ut); 3 | rhs=-(i/2)*(k.^2).*ut - 0.5*i*fft(V.*u); -------------------------------------------------------------------------------- /CH11/nls_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=pod_sol_rhs(t,ut,dummy,k) 2 | u=ifft(ut); 3 | rhs=-(i/2)*(k.^2).*ut + i*fft( (abs(u).^2).*u ); -------------------------------------------------------------------------------- /CH12/CH12_SEC06_1_DEIM.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | L=40; n=256; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); % spatial discretization 4 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; % wavenumbers for FFT 5 | t=linspace(0,2*pi,61); % time domain collection points 6 | 7 | N=2; 8 | u0=N*sech(x).'; 9 | ut=fft(u0); 10 | [t,utsol]=ode45('ch_pod_sol_rhs',t,ut,[],k); 11 | for j=1:length(t) 12 | usol(j,:)=ifft(utsol(j,:)); 13 | end 14 | 15 | figure(1) 16 | subplot(2,2,1) 17 | waterfall(x,t,abs(usol)), shading interp, colormap([0 0 0]) 18 | set(gca,'Ylim',[0 2*pi]), figure(1), subplot(2,2,2) 19 | 20 | %% 21 | X=usol.'; % data matrix X 22 | [U,S,W]=svd(X,0); % SVD reduction 23 | 24 | r=3; % select rank truncation 25 | Psi=U(:,1:r); % select POD modes 26 | a=Psi'*u0; % project initial conditions 27 | 28 | 29 | %% 30 | NL=i*(abs(X).^2).*X; 31 | [XI,S_NL,W]=svd(NL,0); 32 | 33 | % first DEIM point 34 | [Xi_max,nmax]=max(abs(XI(:,1))); 35 | XI_m=XI(:,1); 36 | z=zeros(n,1); 37 | P=z; P(nmax)=1; 38 | 39 | % DEIM points 2 to r 40 | for j=2:r 41 | c=(P'*XI_m)\(P'*XI(:,j)); 42 | res=XI(:,j)-XI_m*c; 43 | [Xi_max,nmax]=max(abs(res)); 44 | XI_m=[XI_m,XI(:,j)]; 45 | P=[P,z]; P(nmax,j)=1; 46 | end 47 | 48 | P_NL=Psi'*( XI_m*inv(P'*XI_m) ); % nonlinear projection 49 | P_Psi=P'*Psi; % interpolation of Psi 50 | 51 | for j=1:r % linear derivative terms 52 | Lxx(:,j)=ifft(-k.^2.*fft(Psi(:,j))); 53 | end 54 | L=(i/2)*(Psi')*Lxx; % projected linear term 55 | 56 | [tt,a]=ode45('rom_deim_rhs',t,a,[],P_NL,P_Psi,L); 57 | Xtilde=Psi*a'; % DEIM approximation 58 | waterfall(x,t,abs(Xtilde')), shading interp, colormap gray 59 | set(gca,'Ylim',[0 2*pi]) 60 | 61 | %% QR DEIM 62 | [Q,R,pivot]=qr(NL.'); 63 | P_qr=pivot(:,1:3); 64 | figure(2), bar(P_qr) 65 | 66 | 67 | -------------------------------------------------------------------------------- /CH12/CH12_SEC06_1_DEIM_production.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | L=40; n=256; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); % spatial discretization 4 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; % wavenumbers for FFT 5 | t=linspace(0,2*pi,61); % time domain collection points 6 | 7 | N=2; 8 | u0=N*sech(x).'; 9 | ut=fft(u0); 10 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 11 | for j=1:length(t) 12 | usol(j,:)=ifft(utsol(j,:)); 13 | end 14 | 15 | figure(1) 16 | subplot(2,2,1) 17 | waterfall(x,t,abs(usol)), shading interp, colormap([0 0 0]) 18 | set(gca,'Ylim',[0 2*pi]), figure(1), subplot(2,2,2) 19 | 20 | %% 21 | X=usol.'; % data matrix X 22 | [U,S,W]=svd(X,0); % SVD reduction 23 | 24 | r=3; % select rank truncation 25 | Psi=U(:,1:r); % select POD modes 26 | a=Psi'*u0; % project initial conditions 27 | 28 | 29 | %% 30 | NL=i*(abs(X).^2).*X; 31 | [XI,S_NL,W]=svd(NL,0); 32 | 33 | % first DEIM point 34 | [Xi_max,nmax]=max(abs(XI(:,1))); 35 | XI_m=XI(:,1); 36 | z=zeros(n,1); 37 | P=z; P(nmax)=1; 38 | 39 | % DEIM points 2 to r 40 | for j=2:r 41 | c=(P'*XI_m)\(P'*XI(:,j)); 42 | res=XI(:,j)-XI_m*c; 43 | [Xi_max,nmax]=max(abs(res)); 44 | XI_m=[XI_m,XI(:,j)]; 45 | P=[P,z]; P(nmax,j)=1; 46 | end 47 | 48 | P_NL=Psi'*( XI_m*inv(P'*XI_m) ); % nonlinear projection 49 | P_Psi=P'*Psi; % interpolation of Psi 50 | 51 | for j=1:r % linear derivative terms 52 | Lxx(:,j)=ifft(-k.^2.*fft(Psi(:,j))); 53 | end 54 | L=(i/2)*(Psi')*Lxx; % projected linear term 55 | 56 | [tt,a]=ode45('rom_deim_rhs',t,a,[],P_NL,P_Psi,L); 57 | Xtilde=Psi*a'; % DEIM approximation 58 | waterfall(x,t,abs(Xtilde')), shading interp, colormap([0 0 0]) 59 | set(gca,'Ylim',[0 2*pi]) 60 | 61 | %% QR DEIM 62 | [Q,R,pivot]=qr(NL.'); 63 | P_qr=pivot(:,1:3); 64 | figure(2), bar(P_qr) 65 | 66 | 67 | -------------------------------------------------------------------------------- /CH12/CH12_SEC06_2_DEIM.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | L=40; n=512; x2=linspace(-L/2,L/2,n+1); x=x2(1:n); % spatial discretization 4 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; % wavenumbers for FFT 5 | t=linspace(0,2*pi,61); % time domain collection points 6 | 7 | N=2; 8 | u0=N*sech(x).'; 9 | ut=fft(u0); 10 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 11 | for j=1:length(t) 12 | usol(j,:)=ifft(utsol(j,:)); 13 | end 14 | 15 | figure(1) 16 | subplot(2,2,1) 17 | waterfall(x,t,abs(usol)), shading interp, colormap([0 0 0]) 18 | set(gca,'Ylim',[0 2*pi]), figure(1), subplot(2,2,2) 19 | 20 | %% 21 | X=usol.'; % data matrix X 22 | [U,S,W]=svd(X,0); % SVD reduction 23 | 24 | r=3; % select rank truncation 25 | Psi=U(:,1:r); % select POD modes 26 | a=Psi'*u0; % project initial conditions 27 | 28 | 29 | %% 30 | NL=i*(abs(X).^2).*X; 31 | [XI,S_NL,W]=svd(NL,0); 32 | 33 | % first DEIM point 34 | [Xi_max,nmax]=max(abs(XI(:,1))); 35 | XI_m=XI(:,1); 36 | z=zeros(n,1); 37 | P=z; P(nmax)=1; 38 | 39 | % DEIM points 2 to r 40 | for j=2:r 41 | c=(P'*XI_m)\(P'*XI(:,j)); 42 | res=XI(:,j)-XI_m*c; 43 | [Xi_max,nmax]=max(abs(res)); 44 | XI_m=[XI_m,XI(:,j)]; 45 | P=[P,z]; P(nmax,j)=1; 46 | end 47 | 48 | P_NL=Psi'*( XI_m*inv(P'*XI_m) ); % nonlinear projection 49 | P_Psi=P'*Psi; % interpolation of Psi 50 | 51 | for j=1:r % linear derivative terms 52 | Lxx(:,j)=ifft(-k.^2.*fft(Psi(:,j))); 53 | end 54 | L=(i/2)*(Psi')*Lxx; % projected linear term 55 | 56 | [tt,a]=ode45('rom_deim_rhs',t,a,[],P_NL,P_Psi,L); 57 | Xtilde=Psi*a'; % DEIM approximation 58 | waterfall(x,t,abs(Xtilde')), shading interp, colormap([0 0 0]) 59 | set(gca,'Ylim',[0 2*pi]) 60 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 61 | subplot(2,2,1) 62 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 63 | 64 | 65 | figure(1) 66 | subplot(2,1,2), plot(x,-real(Psi(:,1:3))) 67 | %legend('mode 1','mode 2','mode 3','Fontsize',[15]) 68 | 69 | set(gca,'Xlim',[-4 4],'Xtick',[-4 -2 0 2 4],'Ylim',[-0.4 0.4],'Ytick',[-0.4 0 0.4],'Fontsize',[15]) 70 | hold on 71 | bar(x,P(:,1:3),'k') 72 | bar(x,-P(:,1:3),'k') 73 | 74 | 75 | %% QR compare 76 | figure(2) 77 | 78 | subplot(2,1,1), plot(x,-real(Psi(:,1:3))) 79 | %legend('mode 1','mode 2','mode 3','Fontsize',[15]) 80 | 81 | set(gca,'Xlim',[-2 2],'Xtick',[-4 -2 0 2 4],'Ylim',[-0.4 0.4],'Ytick',[-0.4 0 0.4],'Fontsize',[15]) 82 | hold on 83 | bar(x,P(:,1:3),'k') 84 | bar(x,-P(:,1:3),'k') 85 | 86 | [Q,R,pivot]=qr(NL.'); 87 | P=pivot(:,1:r); 88 | 89 | subplot(2,1,2), plot(x,-real(Psi(:,1:3))) 90 | %legend('mode 1','mode 2','mode 3','Fontsize',[15]) 91 | 92 | set(gca,'Xlim',[-2 2],'Xtick',[-4 -2 0 2 4],'Ylim',[-0.4 0.4],'Ytick',[-0.4 0 0.4],'Fontsize',[15]) 93 | hold on 94 | bar(x,P(:,1:3),'k') 95 | bar(x,-P(:,1:3),'k') 96 | 97 | -------------------------------------------------------------------------------- /CH12/nls_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=ch_pod_sol_rhs(t,ut,dummy,k) 2 | u=ifft(ut); 3 | rhs=-(i/2)*(k.^2).*ut + i*fft( (abs(u).^2).*u ); -------------------------------------------------------------------------------- /CH12/old_extra/POD_invariance.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; 4 | L=20; x=linspace(-L,L,n); y=x; 5 | [X,Y]=meshgrid(x,y); 6 | 7 | Xd=[]; 8 | for j=1:100 9 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))+j/10); 10 | f=exp(-0.01*(X.^2+Y.^2)); 11 | uf=u.*f; 12 | Xd(:,j)=reshape(uf,n^2,1); 13 | pcolor(x,y,uf), shading interp, colormap(hot), caxis([-1 1]), drawnow 14 | end 15 | 16 | 17 | 18 | %% 19 | [U,S,V]=svd(Xd,0); 20 | 21 | figure(2) 22 | subplot(4,1,3) 23 | plot(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 24 | subplot(4,1,4) 25 | semilogy(100*diag(S)/sum(diag(S)),'ko','Linewidth',[2]) 26 | subplot(2,1,1) 27 | plot(V(:,1:4),'Linewidth',[2]) 28 | legend('mode1','mode2','mode3','mode4') 29 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 30 | subplot(4,1,3), set(gca,'Fontsize',[15],'Ylim',[0 60],'Ytick',[0 20 40 60],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 31 | subplot(4,1,4), set(gca,'Fontsize',[15],'Ylim',[10^(-20) 10^2],'Ytick',[10^(-20) 10^(-10) 10^2],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 32 | 33 | figure(3) 34 | for j=1:4 35 | subplot(4,4,j) 36 | mode=reshape(U(:,j),n,n); 37 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 38 | axis off 39 | end 40 | 41 | %% 42 | 43 | figure(11) 44 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))); 45 | f=exp(-0.01*(X.^2+Y.^2)); 46 | uf=u.*f; 47 | subplot(3,3,1),pcolor(x,y,uf), shading interp, caxis([-1 1]), axis off 48 | subplot(3,3,2),pcolor(x,y,abs(uf)), shading interp, caxis([-1 1]), axis off 49 | subplot(3,3,3),pcolor(x,y,uf.^5), shading interp, caxis([-1 1]), axis off 50 | colormap(gray) 51 | 52 | 53 | 54 | 55 | %% TRANSLATION 56 | figure(5) 57 | n=200; L=20; x=linspace(-L,L,n); y=x; % space 58 | m=41; T=10; t=linspace(0,T,m); % time 59 | c=3; % wave speed 60 | 61 | X=[]; 62 | for j=1:m 63 | X(:,j)=exp(-(x+15-c*t(j)).^2).'; % data snapshots 64 | end 65 | [U,S,V]=svd(X); % SVD decomposition 66 | 67 | 68 | %% 69 | figure(6) 70 | subplot(2,2,1) 71 | waterfall(x,t,X.'),colormap([0 0 0]) 72 | view(20,75) 73 | set(gca,'Xlim',[-20 20],'Xtick',[-20 -10 0 10 20],'Ylim',[0 10], ... 74 | 'Ytick',[0 5 10],'Zlim',[0 1],'Ztick',[0 1],'Fontsize',[12]) 75 | 76 | 77 | [U2,S2,V2]=svd(X); 78 | 79 | subplot(4,2,2) 80 | plot(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 81 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[0 8],'Ytick',[0 4 8]) 82 | subplot(4,2,4) 83 | semilogy(100*diag(S2)/sum(diag(S2)),'ko','Linewidth',[2]) 84 | grid on 85 | set(gca,'Xlim',[0 40],'Xtick',0:10:40,'Ylim',[10^(-3) 2*10^1],'Ytick',[10^(-3) 10^(-2) 10^(-1) 10^0 10^1]) 86 | 87 | figure(8) 88 | subplot(2,1,1) 89 | plot(x,U2(:,1:4),'Linewidth',[2]); 90 | legend('mode1','mode2','mode3','mode4','Location','SouthEast') 91 | set(gca,'Fontsize',[15],'Ylim',[-0.15 0.15],'Ytick',[-0.15 0 0.15]) 92 | subplot(2,1,2) 93 | plot(t,V2(:,1:4),'Linewidth',[2]) 94 | set(gca,'Fontsize',[15],'Ylim',[-.3 0.3],'Ytick',[-0.3 0 0.3]) 95 | 96 | -------------------------------------------------------------------------------- /CH12/old_extra/POD_invariance2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | n=100; 4 | L=20; x=linspace(-L,L,n); y=x; 5 | [X,Y]=meshgrid(x,y); 6 | 7 | Xd=[]; 8 | for j=1:100 9 | u=tanh(sqrt(X.^2+Y.^2)).*cos(angle(X+i*Y)-(sqrt(X.^2+Y.^2))+j/10); 10 | f=exp(-0.01*(X.^2+Y.^2)); 11 | uf=u.*f; 12 | Xd1(:,j)=reshape(abs(uf),n^2,1); 13 | Xd2(:,j)=reshape(uf.^5,n^2,1); 14 | %pcolor(x,y,uf), shading interp, colormap(hot), caxis([-1 1]), drawnow 15 | end 16 | 17 | 18 | 19 | %% 20 | [U1,S1,V1]=svd(Xd1,0); 21 | [U2,S2,V2]=svd(Xd2,0); 22 | 23 | figure(2) 24 | subplot(4,1,1) 25 | plot(100*diag(S1)/sum(diag(S1)),'ro','Linewidth',[2]), hold on 26 | plot(100*diag(S2)/sum(diag(S2)),'bo','Linewidth',[2]) 27 | legend('|u|','u^5') 28 | subplot(4,1,2) 29 | semilogy(100*diag(S1)/sum(diag(S1)),'ro','Linewidth',[2]), hold on 30 | semilogy(100*diag(S2)/sum(diag(S2)),'bo','Linewidth',[2]) 31 | grid on 32 | subplot(4,1,3) 33 | plot(V1(:,1:4),'Linewidth',[2]) 34 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 35 | subplot(4,1,4) 36 | plot(V2(:,1:4),'Linewidth',[2]) 37 | legend('mode1','mode2','mode3','mode4') 38 | set(gca,'Fontsize',[15],'Xtick',[0 20 40 60 80 100]) 39 | subplot(4,1,1), set(gca,'Fontsize',[15],'Ylim',[0 60],'Ytick',[0 20 40 60],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 40 | subplot(4,1,2), set(gca,'Fontsize',[15],'Ylim',[10^(-20) 10^2],'Ytick',[10^(-20) 10^(-10) 10^2],'Xlim',[0 40],'Xtick',[0 10 20 30 40]) 41 | 42 | 43 | figure(3) 44 | for j=1:4 45 | subplot(4,4,j) 46 | mode=reshape(U1(:,j),n,n); 47 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 48 | axis off 49 | subplot(4,4,j+4) 50 | mode=reshape(U2(:,j),n,n); 51 | pcolor(X,Y,mode), shading interp,caxis([-0.03 0.03]), colormap(gray) 52 | axis off 53 | end 54 | 55 | -------------------------------------------------------------------------------- /CH12/old_extra/ch_ROM_rom_nls.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=30; n=256; 4 | x2=linspace(-L/2,L/2,n+1); x=x2(1:n); 5 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; 6 | t=linspace(0,2*pi,51); 7 | 8 | u=2*sech(x); 9 | ut=fft(u); 10 | 11 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 12 | 13 | for j=1:length(t) 14 | usol(j,:)=ifft(utsol(j,:)); 15 | end 16 | 17 | figure(1), subplot(2,2,1),surfl(x,t,abs(usol)); shading interp, colormap(gray); 18 | 19 | %% SVD 20 | 21 | X=usol.'; 22 | [u,s,v]=svd(X); 23 | figure(2) 24 | plot(diag(s)/sum(diag(s)),'ko','Linewidth',[2]) 25 | 26 | figure(3) 27 | plot(real(u(:,1:3)),'Linewidth',[2]), figure(4) 28 | 29 | %% 30 | r=4; % rank of projection 31 | phi=u(:,1:r); % Phi_r POD modes 32 | 33 | for j=1:r 34 | phixx(:,j)= -ifft((k.^2).*fft(phi(:,j))); % second derivatives 35 | a0(j)= 2*sech(x)*conj(phi(:,j)); % projection of initial conditions 36 | end 37 | L= (i/2)* phi'*phixx; % Low-rank approximation of linear term 38 | 39 | [t,asol]=ode45('ch_rom_a_rhs',t,a0,[],phi,L); 40 | 41 | us=zeros(n,length(t)); 42 | for j=1:length(t) 43 | for jj=1:r 44 | us(:,j)=us(:,j) + asol(j,jj)*phi(:,jj); % r-rank reconstruction 45 | end 46 | end 47 | 48 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 49 | 50 | figure(1) 51 | subplot(2,2,2) 52 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 53 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 54 | % zlabel('|u|','Fontsize',[15]) 55 | % xlabel('x','Fontsize',[15]) 56 | % ylabel('t','Fontsize',[15]) 57 | % text(-20,5,'(b)','Fontsize',[15]) 58 | 59 | subplot(2,2,1) 60 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 61 | % zlabel('|u|','Fontsize',[15]) 62 | % xlabel('x','Fontsize',[15]) 63 | % ylabel('t','Fontsize',[15]) 64 | % text(-20,10,'(a)','Fontsize',[15]) 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /CH12/old_extra/ch_ROM_rom_nls2.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=30; n=256; 4 | x2=linspace(-L/2,L/2,n+1); x=x2(1:n); 5 | k=(2*pi/L)*[0:n/2-1 -n/2:-1].'; 6 | t=linspace(0,2*pi,41); 7 | 8 | u=2*sech(x); 9 | ut=fft(u); 10 | 11 | [t,utsol]=ode45('nls_rhs',t,ut,[],k); 12 | 13 | for j=1:length(t) 14 | usol(j,:)=ifft(utsol(j,:)); 15 | end 16 | 17 | figure(1), subplot(2,2,1),surfl(x,t,abs(usol)); shading interp, colormap(gray); 18 | 19 | %% SVD 20 | 21 | X=usol.'; 22 | [u,s,v]=svd(X); 23 | figure(2) 24 | plot(diag(s)/sum(diag(s)),'ko','Linewidth',[2]) 25 | 26 | figure(3) 27 | plot(real(u(:,1:3)),'Linewidth',[2]), figure(4) 28 | 29 | %% 30 | 31 | for r=1:4 32 | %r=4; % rank of projection 33 | phi=u(:,1:r); % Phi_r POD modes 34 | 35 | for j=1:r 36 | phixx(:,j)= -ifft((k.^2).*fft(phi(:,j))); % second derivatives 37 | a0(j)= 2*sech(x)*conj(phi(:,j)); % projection of initial conditions 38 | end 39 | L= (i/2)* phi'*phixx; % Low-rank approximation of linear term 40 | 41 | [t,asol]=ode45('ch_rom_a_rhs',t,a0,[],phi,L); 42 | 43 | us=zeros(n,length(t)); 44 | for j=1:length(t) 45 | for jj=1:r 46 | us(:,j)=us(:,j) + asol(j,jj)*phi(:,jj); % r-rank reconstruction 47 | end 48 | end 49 | 50 | figure(1) 51 | subplot(2,2,r) 52 | waterfall(x,t,abs(us.')); colormap([0 0 0]); 53 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 54 | 55 | end 56 | 57 | 58 | 59 | break 60 | 61 | figure(1) 62 | subplot(2,2,2) 63 | surfl(x,t,abs(us.')); shading interp, colormap(gray); 64 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 65 | % zlabel('|u|','Fontsize',[15]) 66 | % xlabel('x','Fontsize',[15]) 67 | % ylabel('t','Fontsize',[15]) 68 | % text(-20,5,'(b)','Fontsize',[15]) 69 | 70 | subplot(2,2,1) 71 | set(gca,'Xlim',[-15 15],'Ylim',[0 2*pi],'Zlim',[0 4],'Xtick',[-15 0 15],'Ytick',[0 pi 2*pi],'Yticklabel',{'0','\pi','2\pi'},'Ztick',[0 2 4],'Fontsize',[15]) 72 | % zlabel('|u|','Fontsize',[15]) 73 | % xlabel('x','Fontsize',[15]) 74 | % ylabel('t','Fontsize',[15]) 75 | % text(-20,10,'(a)','Fontsize',[15]) 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /CH12/old_extra/ch_pod.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc 2 | 3 | L=20; n=80; 4 | x2=linspace(-L/2,L/2,n+1); 5 | x=x2(1:n); 6 | u=exp(-x.^2); u1=exp(-0.1*x.^2); u2=exp(-10*x.^2); 7 | ut=fft(u); uts=fftshift(ut); 8 | ut1=fft(u1); uts1=fftshift(ut1); 9 | ut2=fft(u2); uts2=fftshift(ut2); 10 | 11 | k=(2*pi/L)*[0:(n/2-1) (-n/2):-1]; 12 | km=[0:n/2-1 -n/2:-1]; 13 | 14 | 15 | subplot(3,2,1), plot(x,u2,'b-o',x,u1,'r-o',x,u,'k-o') 16 | set(gca,'Xlim',[-10 10],'Xtick',[-10 0 10],'Fontsize',[15]) 17 | text(-13.5,0.8,'u(x)','Fontsize',[15]) 18 | text(7,-0.2,'x','Fontsize',[15]) 19 | text(-8,0.8,'(a)','Fontsize',[15]) 20 | 21 | 22 | 23 | 24 | subplot(3,2,2), plot(km,abs(ut2)/max(abs(ut2)),'b-o',km,abs(ut1)/max(abs(ut1)),'r-o',km,abs(ut)/max(abs(ut)),'k-o') 25 | set(gca,'Xlim',[-40 40],'Xtick',[-40 0 40],'Fontsize',[15]) 26 | text(-53,0.8,'u(k)','Fontsize',[15]) 27 | text(-53,0.86,'^','Fontsize',[15]) 28 | text(18,-0.2,'mode','Fontsize',[15]) 29 | text(-36,0.8,'(b)','Fontsize',[15]) 30 | 31 | 32 | ut21=zeros(1,n); 33 | ut22=zeros(1,n); 34 | ut23=zeros(1,n); 35 | 36 | for j=1:19 37 | 38 | for jj=1:j 39 | 40 | ut21(n/2+1-jj:n/2+1+jj) = uts(n/2+1-jj:n/2+1+jj); 41 | ut22(n/2+1-jj:n/2+1+jj) = uts1(n/2+1-jj:n/2+1+jj); 42 | ut23(n/2+1-jj:n/2+1+jj) = uts2(n/2+1-jj:n/2+1+jj); 43 | 44 | end 45 | ut31=fftshift(ut21); 46 | ut32=fftshift(ut22); 47 | ut33=fftshift(ut23); 48 | u31=ifft(ut31); 49 | u32=ifft(ut32); 50 | u33=ifft(ut33); 51 | subplot(3,1,2) 52 | plot(x,u31,'k'), hold on 53 | 54 | % figure(2) 55 | % subplot(2,1,1) 56 | % plot(x,u32,'k'), hold on 57 | % 58 | % subplot(2,1,2) 59 | % plot(x,u33,'k'), hold on 60 | % figure(1) 61 | 62 | 63 | 64 | 65 | erx(j)=2*j+1 66 | er1(j)=norm(u-u31); 67 | er2(j)=norm(u1-u32); 68 | er3(j)=norm(u2-u33); 69 | 70 | 71 | end 72 | subplot(3,1,3) 73 | semilogy(erx,er3,'b',erx,er2,'r',erx,er1,'k','LineWidth',[2]), grid on 74 | 75 | subplot(3,1,2), set(gca,'Xlim',[-10 10],'Xtick',[-10 -5 0 5 10],'Ylim',[-.2 1],'Ytick',[0 0.5 1],'Fontsize',[15]) 76 | text(-13,0.8,'u(x)','Fontsize',[15]) 77 | text(-0.5,0.1,'N=3','Fontsize',[15]) 78 | text(1,0.85,'N=39','Fontsize',[15]) 79 | A=annotation('textarrow',[0.52 0.52],[0.48 0.60]); set(A,'String','','Fontsize',15) 80 | text(7,-0.4,'x','Fontsize',[15]) 81 | text(-9,0.8,'(c)','Fontsize',[15]) 82 | 83 | 84 | 85 | subplot(3,1,3), set(gca,'Fontsize',[15],'Ylim',[10^(-5) 10^1],'Ytick',[10^(-5) 10^(-4) 10^(-3) 10^(-2) 10^(-1) 10^(0) 10^(1)] ) 86 | text(-6,1,'Error','Fontsize',[15]) 87 | text(32,10^(-7),'# of modes','Fontsize',[15]) 88 | text(2,10^(-4),'(d)','Fontsize',[15]) 89 | 90 | figure(2) 91 | subplot(2,1,1) 92 | 93 | subplot(2,1,2) 94 | figure(1) 95 | 96 | -------------------------------------------------------------------------------- /CH12/old_extra/ch_pod_harm_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=ch_pod_harm_rhs(t,ut,dummy,k,V) 2 | u=ifft(ut); 3 | rhs=-(i/2)*(k.^2).*ut - 0.5*i*fft(V.*u); -------------------------------------------------------------------------------- /CH12/old_extra/ch_rom_a_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=a_rhs(t,a,dummy,phi,L) 2 | rhs= L*a + i*phi'*( (abs(phi*a).^2).*(phi*a) ); -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/ccmat.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH12/old_extra/deimPLOT/ccmat.mat -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/data.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH12/old_extra/deimPLOT/data.mat -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/dataexam.m: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/deim.m: -------------------------------------------------------------------------------- 1 | clear all; close all; clc; 2 | 3 | load data.mat 4 | st=1; 5 | fin=100; % 95 6 | U=usol(1:100,:)'; 7 | figure(1) 8 | waterfall(abs(U')) 9 | %surfl(abs(U')), shading interp 10 | title('true soln') 11 | 12 | L=40; n=512; 13 | x2=linspace(-L/2,L/2,n+1); x=x2(1:n); 14 | h=x(2)-x(1); 15 | u0=2*sech(x)'; 16 | 17 | 18 | %% 19 | %get modes for the system 20 | [VX,S,W]=svd((U),0); 21 | figure(2) 22 | plot(cumsum(diag(S)/sum(diag(S))), 'ko') 23 | title('cumsum of svals of full data matrix') 24 | xlim([0 20]) 25 | %3 modes enought 26 | vk=3; 27 | VX=VX(:,1:vk); 28 | a=VX'*u0; 29 | % break 30 | 31 | %% 32 | %compute the nonlinear part 33 | NL=zeros(n,fin-st+1); 34 | 35 | for j=1:fin-st+1 36 | NL(:,j)=i*(abs(U(:,j))).^2.*U(:,j); 37 | 38 | end 39 | 40 | % p=512-128:512+128; 41 | % ss=st:1:fin; 42 | % NN=NL.';%NL(p,:)'; 43 | % 44 | % figure(2) 45 | % waterfall(abs(NN)) 46 | % title('nonlinear part') 47 | % 48 | 49 | [u,s,v]=svd(NL,0); 50 | figure(3) 51 | plot(cumsum(diag(s)/sum(diag(s))), 'ko') 52 | %three modes are enough 53 | 54 | m=vk; 55 | 56 | %% 57 | %Apply DEIM algorithm 58 | 59 | %compute prjection matrix and indices 60 | [ro,g(1)]=max(abs(u(:,1))); 61 | U=u(:,1); 62 | z=zeros(n,1); 63 | P=z; P(g(1))=1; 64 | 65 | 66 | for l=2:m 67 | c=(P'*U)\(P'*u(:,l)); 68 | r=u(:,l)-U*c; 69 | [ro,g(l)]=max(abs(r)); 70 | U=[U,u(:,l)]; 71 | P=[P,z]; P(g(l),l)=1; 72 | 73 | end 74 | 75 | figure(5), bar3(P) 76 | 77 | B =U*inv(P'*U); 78 | VB=VX'*B; 79 | PV=P'*VX; 80 | k = (2*pi/L)*[0:n/2-1 -n/2:-1].'; 81 | 82 | for j=1:vk 83 | LV(:,j)=ifft(-k.^2.*fft(VX(:,j))); 84 | end 85 | 86 | VLV=i/2*VX'*LV; 87 | dt=0.1; 88 | tspan=0:0.1:9.5; 89 | [tt, a]=ode45('rhs',tspan, a,[],VB, PV, VLV); 90 | 91 | % figure(3) 92 | % plot(tt, abs(a(:,1))) 93 | % size(a) 94 | 95 | recon=VX*a'; 96 | 97 | 98 | figure(4) 99 | waterfall(abs(recon')) 100 | %surfl(abs(recon')), shading interp 101 | 102 | % norm(recon-X) 103 | 104 | -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/psi_L.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/CH12/old_extra/deimPLOT/psi_L.mat -------------------------------------------------------------------------------- /CH12/old_extra/deimPLOT/rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=rhs(tspan, a,dummy,VB, PV, VLV) 2 | 3 | N=PV*a; 4 | rhs=VLV*a+(i* VB*((abs(N).^2).*N)); 5 | 6 | end -------------------------------------------------------------------------------- /CH12/rom_deim_rhs.m: -------------------------------------------------------------------------------- 1 | function rhs=rom_deim_rhs(tspan, a,dummy,P_NL,P_Psi,L) 2 | N=P_Psi*a; 3 | rhs=L*a + i*P_NL*((abs(N).^2).*N); 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Data_Driven_Science_MATLAB_Demos 2 | 3 | Data Driven Book Cover 4 | 5 | MATLAB demo code intended as a companion to the book: 6 | 7 | 8 | Data Driven Science & Engineering: Machine Learning, Dynamical Systems, and Control 9 | by S. L. Brunton and J. N. Kutz 10 | Cambridge Textbook, 2019 11 | Copyright 2019, All Rights Reserved 12 | http://databookuw.com/ 13 | 14 | Please cite this book when using this code/data. 15 | 16 | MATLAB code library by S. L. Brunton and J. N. Kutz. 17 | 18 | **This code makes use of data files not packaged in the repository. These may be downloaded here: http://databookuw.com/DATA.zip (unzip into same directory)** 19 | 20 | Python versions of these demos are available at https://github.com/dynamicslab/databook_python 21 | 22 | Links to useful Github codes: 23 | 24 | Chapter 1 25 | 26 | — rSVD (N. Ben Erichson): https://github.com/erichson/rSVD 27 | 28 | Chapter 3 29 | 30 | — SSPOR (Krithika Manohar): https://github.com/kmanohar/SSPOR_pub 31 | 32 | Chapter 7 33 | 34 | — PDE-FIND (Sam Rudy): https://github.com/snagcliffs/PDE-FIND 35 | 36 | — DeepKoopman (Bethany Lusch): https://github.com/BethanyL/DeepKoopman 37 | 38 | — KRONIC (Eurika Kaiser): https://github.com/eurika-kaiser/KRONIC 39 | 40 | Chapter 10 41 | 42 | — SINDY-MPC (Eurika Kaiser): https://github.com/eurika-kaiser/SINDY-MPC 43 | 44 | — MLC (Thomas Duriez, Bernd Noack): http://berndnoack.com/MachineLearningControl.php 45 | 46 | 47 | Other codes that we found useful, but have not included in our repository because of copyright: 48 | 49 | cvx: http://cvxr.com/cvx/download/ 50 | 51 | mp3readwrite: https://labrosa.ee.columbia.edu/matlab/mp3read.html 52 | 53 | -------------------------------------------------------------------------------- /UTILS/CC2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/UTILS/CC2.mat -------------------------------------------------------------------------------- /UTILS/SVT.m: -------------------------------------------------------------------------------- 1 | function out = SVT(X,tau) 2 | [U,S,V] = svd(X,'econ'); 3 | out = U*shrink(S,tau)*V'; 4 | end -------------------------------------------------------------------------------- /UTILS/colors2.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/UTILS/colors2.mat -------------------------------------------------------------------------------- /UTILS/coolcolors.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/UTILS/coolcolors.mat -------------------------------------------------------------------------------- /UTILS/coolcolorsBW.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/UTILS/coolcolorsBW.mat -------------------------------------------------------------------------------- /UTILS/cosamp.m: -------------------------------------------------------------------------------- 1 | function Sest = cosamp(Phi,u,K,tol,maxiterations) 2 | 3 | % Cosamp algorithm 4 | % Input 5 | % K : sparsity of Sest 6 | % Phi : measurement matrix 7 | % u: measured vector 8 | % tol : tolerance for approximation between successive solutions. 9 | % Output 10 | % Sest: Solution found by the algorithm 11 | % 12 | % Algorithm as described in "CoSaMP: Iterative signal recovery from 13 | % incomplete and inaccurate samples" by Deanna Needell and Joel Tropp. 14 | % 15 | 16 | 17 | % This implementation was written by David Mary, 18 | % but modified 20110707 by Bob L. Sturm to make it much clearer, 19 | % and corrected multiple times again and again. 20 | % To begin with, see: http://media.aau.dk/null_space_pursuits/2011/07/ ... 21 | % algorithm-power-hour-compressive-sampling-matching-pursuit-cosamp.html 22 | % 23 | % This script/program is released under the Commons Creative Licence 24 | % with Attribution Non-commercial Share Alike (by-nc-sa) 25 | % http://creativecommons.org/licenses/by-nc-sa/3.0/ 26 | % Short Disclaimer: this script is for educational purpose only. 27 | % Longer Disclaimer see http://igorcarron.googlepages.com/disclaimer 28 | 29 | % Initialization 30 | Sest = zeros(size(Phi,2),1); 31 | v = u; 32 | t = 1; 33 | numericalprecision = 1e-12; 34 | T = []; 35 | while (t <= maxiterations) && (norm(v)/norm(u) > tol) 36 | y = abs(Phi'*v); 37 | [vals,z] = sort(y,'descend'); 38 | Omega = find(y >= vals(2*K) & y > numericalprecision); 39 | T = union(Omega,T); 40 | b = pinv(Phi(:,T))*u; 41 | [vals,z] = sort(abs(b),'descend'); 42 | Kgoodindices = (abs(b) >= vals(K) & abs(b) > numericalprecision); 43 | T = T(Kgoodindices); 44 | Sest = zeros(size(Phi,2),1); 45 | b = b(Kgoodindices); 46 | Sest(T) = b; 47 | v = u - Phi(:,T)*b; 48 | t = t+1; 49 | end 50 | -------------------------------------------------------------------------------- /UTILS/istft/Resynthesis.m: -------------------------------------------------------------------------------- 1 | clear, clc, close all 2 | 3 | % music program (stochastic non-stationary signal) 4 | [x, fs] = wavread('track.wav'); 5 | x = x(:, 1); 6 | xmax = max(abs(x)); 7 | x = x/xmax; 8 | 9 | % signal parameters 10 | xlen = length(x); 11 | t = (0:xlen-1)/fs; 12 | 13 | % define analysis and synthesis parameters 14 | wlen = 1024; 15 | h = wlen/4; 16 | nfft = wlen; 17 | 18 | % perform time-frequency analysis and resynthesis of the original signal 19 | [stft, f, t_stft] = stft(x, wlen, h, nfft, fs); 20 | [x_istft, t_istft] = istft(stft, h, nfft, fs); 21 | 22 | % plot the original signal 23 | figure(1) 24 | plot(t, x, 'b') 25 | grid on 26 | xlim([0 max(t)]) 27 | ylim([-1.1 1.1]) 28 | set(gca, 'FontName', 'Times New Roman', 'FontSize', 14) 29 | xlabel('Time, s') 30 | ylabel('Normalized amplitude') 31 | title('Original and reconstructed signal') 32 | 33 | % plot the resynthesized signal 34 | hold on 35 | plot(t_istft, x_istft, '-.r') 36 | legend('Original signal', 'Reconstructed signal') -------------------------------------------------------------------------------- /UTILS/istft/WindowChoice.m: -------------------------------------------------------------------------------- 1 | clear, clc, close all 2 | 3 | wlen = 64; % window length (recomended to be power of 2), choose it! 4 | h = wlen/4; % hop size (recomended to be power of 2), choose it! 5 | k = 5*wlen; % overlap-add span 6 | win = hamming(wlen, 'periodic'); % window, choose it! 7 | s = zeros(k, 1); 8 | 9 | for k = 0:h:k-wlen 10 | indx = k+1:k+wlen; % current window location 11 | s(indx) = s(indx) + win.^2; % window overlap-add 12 | winp(indx) = win; % for plot only 13 | plot(winp, 'ok') % plot just this window 14 | hold on 15 | end 16 | 17 | W0 = sum(win.^2); % find W0 18 | s = h*s/W0; % scale the window overlap-add 19 | stem(s, 'r'); % plot window overlap-add -------------------------------------------------------------------------------- /UTILS/istft/istft.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % Inverse Short-Time Fourier Transform % 3 | % with MATLAB Implementation % 4 | % % 5 | % Author: M.Sc. Eng. Hristo Zhivomirov 12/26/13 % 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | 8 | function [x, t] = istft(stft, h, nfft, fs) 9 | 10 | % function: [x, t] = istft(stft, h, nfft, fs) 11 | % stft - STFT matrix (only unique points, time across columns, freq across rows) 12 | % h - hop size 13 | % nfft - number of FFT points 14 | % fs - sampling frequency, Hz 15 | % x - signal in the time domain 16 | % t - time vector, s 17 | 18 | % estimate the length of the signal 19 | coln = size(stft, 2); 20 | xlen = nfft + (coln-1)*h; 21 | x = zeros(1, xlen); 22 | 23 | % form a periodic hamming window 24 | win = hamming(nfft, 'periodic'); 25 | 26 | % perform IFFT and weighted-OLA 27 | if rem(nfft, 2) % odd nfft excludes Nyquist point 28 | for b = 0:h:(h*(coln-1)) 29 | % extract FFT points 30 | X = stft(:, 1 + b/h); 31 | X = [X; conj(X(end:-1:2))]; 32 | 33 | % IFFT 34 | xprim = real(ifft(X)); 35 | 36 | % weighted-OLA 37 | x((b+1):(b+nfft)) = x((b+1):(b+nfft)) + (xprim.*win)'; 38 | end 39 | else % even nfft includes Nyquist point 40 | for b = 0:h:(h*(coln-1)) 41 | % extract FFT points 42 | X = stft(:, 1+b/h); 43 | X = [X; conj(X(end-1:-1:2))]; 44 | 45 | % IFFT 46 | xprim = real(ifft(X)); 47 | 48 | % weighted-OLA 49 | x((b+1):(b+nfft)) = x((b+1):(b+nfft)) + (xprim.*win)'; 50 | end 51 | end 52 | 53 | W0 = sum(win.^2); % find W0 54 | x = x.*h/W0; % scale the weighted-OLA 55 | 56 | % calculate the time vector 57 | actxlen = length(x); % find actual length of the signal 58 | t = (0:actxlen-1)/fs; % generate time vector 59 | 60 | end -------------------------------------------------------------------------------- /UTILS/istft/license.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Hristo Zhivomirov 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the distribution 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | POSSIBILITY OF SUCH DAMAGE. 25 | -------------------------------------------------------------------------------- /UTILS/istft/stft.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % Short-Time Fourier Transform % 3 | % with MATLAB Implementation % 4 | % % 5 | % Author: M.Sc. Eng. Hristo Zhivomirov 12/21/13 % 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | 8 | function [stft, f, t] = stft(x, wlen, h, nfft, fs) 9 | 10 | % function: [stft, f, t] = stft(x, wlen, h, nfft, fs) 11 | % x - signal in the time domain 12 | % wlen - length of the hamming window 13 | % h - hop size 14 | % nfft - number of FFT points 15 | % fs - sampling frequency, Hz 16 | % f - frequency vector, Hz 17 | % t - time vector, s 18 | % stft - STFT matrix (only unique points, time across columns, freq across rows) 19 | 20 | % represent x as column-vector if it is not 21 | if size(x, 2) > 1 22 | x = x'; 23 | end 24 | 25 | % length of the signal 26 | xlen = length(x); 27 | 28 | % form a periodic hamming window 29 | win = hamming(wlen, 'periodic'); 30 | 31 | % form the stft matrix 32 | rown = ceil((1+nfft)/2); % calculate the total number of rows 33 | coln = 1+fix((xlen-wlen)/h); % calculate the total number of columns 34 | stft = zeros(rown, coln); % form the stft matrix 35 | 36 | % initialize the indexes 37 | indx = 0; 38 | col = 1; 39 | 40 | % perform STFT 41 | while indx + wlen <= xlen 42 | % windowing 43 | xw = x(indx+1:indx+wlen).*win; 44 | 45 | % FFT 46 | X = fft(xw, nfft); 47 | 48 | % update the stft matrix 49 | stft(:, col) = X(1:rown); 50 | 51 | % update the indexes 52 | indx = indx + h; 53 | col = col + 1; 54 | end 55 | 56 | % calculate the time and frequency vectors 57 | t = (wlen/2:h:wlen/2+(coln-1)*h)/fs; 58 | f = (0:rown-1)*fs/nfft; 59 | 60 | end -------------------------------------------------------------------------------- /UTILS/istft/track.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamicslab/databook_matlab/d390d39d18489a4804ee87a143ae8db8a1f3010b/UTILS/istft/track.wav -------------------------------------------------------------------------------- /UTILS/pad.m: -------------------------------------------------------------------------------- 1 | function X = padflip(X); 2 | 3 | [nx,ny] = size(X); 4 | X = flipud(X); 5 | X = [X zeros(nx,1); 6 | zeros(1,ny+1)]; -------------------------------------------------------------------------------- /UTILS/padflip.m: -------------------------------------------------------------------------------- 1 | function X = padflip(X); 2 | 3 | [nx,ny] = size(X); 4 | X = flipud(X); 5 | X = [X zeros(nx,1); 6 | zeros(1,ny+1)]; -------------------------------------------------------------------------------- /UTILS/shrink.m: -------------------------------------------------------------------------------- 1 | function out = shrink(X,tau) 2 | out = sign(X).*max(abs(X)-tau,0); 3 | end --------------------------------------------------------------------------------