%{ Calculation of the eigenmodes of a vibrating string. Numerical method: finite-difference scheme, second-order central. clear all close all clc N = 50; [S,V_an,kappa_an]=eigenProblem(N,{'fd2','fd4'}); %} function [S,V_an,kappa_an]=eigenProblem(N,method) % Analytical eigenvalues/eigenvectors kappa_an = (1:N-1).*pi; V_an = sin(repmat((1:N-1),[N+1,1]).*repmat((0:N).',[1,N-1]).*(pi/N)); % Normalization V_an = V_an./(repmat(sqrt(sum(V_an.^2,1)),[N+1,1])); color = {'k','g'}; for k=1:length(method) % Numerical eigenvalues/eigenvectors switch method{k} case{'fd2'} A = fd2(N-1); case{'fd4'} A = fd4(N-1); end [S(k).V,S(k).D] = eigs(A,N-1,'smallestabs'); S(k).V = [zeros(1,N-1);S(k).V;zeros(1,N-1)]; S(k).kappa = N*sqrt(-diag(S(k).D)); % Reverse sign of numerical eigenvectors if necessary for j=1:N-1 ep = norm(S(k).V(:,j)-V_an(:,j)); em = norm(S(k).V(:,j)+V_an(:,j)); if (em