I have had a numerical problem in my simulations for weeks and I have finally narrowed it down to being a problem with the Eig function in MATLAB. Without going into too many details here is a small script that sets up two matrices K1 and K, which if you print them are completely identical. But for some reason the Eig function does not return the correct eigenvector for K and I have no idea why. In this matrix I have used the grid distance between adjacent points as dx = x(i+1)-x(i) on a uniform grid of length 1 but that is the only difference to the one set up in K1, where I simply used dx=1/N. Can anyone see what on earth is going on? Also the problem seems to happen only for sufficiently large N, i.e. small grid distances.. I have no idea why, but I am so frustrated for this since it is essential for my master's thesis.
clear all
clc
N=1000;
dx=1/N; %grid distance is 1/N
x=dx*(1:N); %make grid
A=zeros(N,N);
for i=1:N-1
A(i,i+1)=1;
end
K1=-1/dx^2*(A+A'-2*eye(N));
for i=2:N-1
K(i,i)=-2/(x(i+1)-x(i-1))*(1/(x(i+1)-x(i))+1/(x(i)-x(i-1)));
K(i,i-1)=2/(x(i+1)-x(i-1))*(1/(x(i)-x(i-1)));
K(i,i+1)=2/(x(i+1)-x(i-1))*(1/(x(i+1)-x(i)));
end
K(N,N-1)=K(N-1,N-2);
K(1,1)=K(2,2);
K(1,2)=K(2,3);
K(2,1)=K(2,3);
K(N,N)=K(N-1,N-1);
K=-K;
[h,y]=eig(K); %eigenvectors (h) and eigenvalues (y) of first K
[z,v]=eig(K1); %eigenvectors (z) and eigenvalues of (v) of K1
plot(x,z(:,1)) %plot first eigenvector of K1
plot(x,h(:,1))

KandK1. Of course, generally, they will have different eigenvectos/values - Ander Biguri