function [A,h] = mesheig(n) % % Returns the matrix in the eigenvalue problem to solve % solve y" + w^2*y = 0, using n nodes over [0,1], % with boundary value conditions y(0) = 0, y(1) = 0. % % On entry : % n number of internal nodes in [0,1]. % % On return : % A tridiagonal matrix of the linear system, % defined by a mesh of finite differences; % h space between the nodes x(k) = x(0) + k*h. % % Example: % [A,h] = mesheig(4); % [v,lambda] = eig(A); % solve the eigenvalue problem % m = max(v(:,1)); % to scale 1st eigenvector % x = 0:h:1 % prepare plot: range for x % y = [0; v(:,1)/m; 0] % first eigenvector % plot(x,y) % h = 1/(n+1); A = zeros(n,n); for k = 1:n-1 A(k+1,k) = -1; A(k,k+1) = -1; end; for k = 1:n A(k,k) = 2; end;