Eigenvalue problem constrained with a penalty method

I am trying to constrain an eigenvalue problem. I am aware of the method utilizing the nullspace of the constraint vectors but I was wondering if it would be to use a penalty method for the same purpose. Trying the following at MATLAB/Octave

r = rand(4,4);
r=r' + r;
p=[1, 0,  0 , -1]';
w=10000;
[V, D] = eig(r + w*p*p');
V


does not enforce $p={[1 , 0, 0,-1}]$ for all eigenvectors (components 1 and 4 of the system should have the same value). There seems to be always 1 eigenvector where the 1st and last vector component are the same in value and have opposite signs.

Is there any obvious reason why this is happening and I miss it? Sorry if this is obvious, my linear algebra is a bit rusty.

1 Answer

I found the error, I just need to constrain the implied identity matrix on the right-hand-side too:

r = rand(4, 4);
r = r' + r;
p=[1 0 0  -1]';
M = eye(4);
w = 1000;
[V,D ] = eig(r + w*p*p', M + w*p*p');
V