Problem with the function eig(), is not the same [vec, val1] = eig (A) and val2 = eig (A), the eigenvalues do not match.
Show older comments
I have a problem, I have a matrix A to which you calculate your eigenvalues in two ways, the first in the following way [Evec,Eval1]=eig(A) and the second in the following way Eval2=eig(A). What I expected was that diag(Eval1) was equal to Eval2, but that does not happen for this matrix.
My matriz A is of size 100x100 symmetric. This matrix is generated with following code:
SamplePeack1=wblrnd(1,1.5,1,100);
SamplePeack2=wblrnd(3,6,1,100);
SamplePeak=[MuestraP1,MuestraP2]; %This is a sample with two peacks
Distrib2Peaks=fitdist(SamplePeak', 'kernel');
Sample2Peak100= random(Distrib2Peaks,1, 100);
fdenVerd2Picos=@(x)pdf(Distrib2Picos,x);
[fEstKernel,xicoor,bw] = ksdensity(Sample2Peak100); %Claculating bandwidth of Kernel estimation
A=exp(-((repmat(Sample2Peak100,100,1)-repmat(Sample2Peak100',1,100)).^2)/(4*bw^2))/(2*bw*sqrt(pi));
Then calculate the eigenvalues
[Evec,Eval1]=eig(A);
Eval2=eig(A);
But in this case we have
>> min(diag(eigenv))
ans =
-6.5310e-15
>>min(eigenv2)
ans =
-3.5015e-15
They should not be different.
2 Comments
Rik
on 26 May 2018
e-15 is quite close to eps, so this might be a difference in rounding. Maybe the methods of calculation are subtly different as well.
Greg Heath
on 26 May 2018
Why not put this in the ANSWER BOX?
Answers (1)
Rik
on 26 May 2018
1 vote
Short answer: e-15 is essentially 0, so those two values are equal.
Long answer: Matlab uses binary (duh.), which can't be decimal (again: duh.). The way this is often solved, is by using a system called 'floating point numbers' (often abbreviated to 'floats'). This puts a limit on the decimal precision, which depends on how many binary digits you are prepared to use to represent each number. Matlab has two options: single and double. The eps function shows the minimum value Matlab can distinguish (which is different between single and double). In general, rounding errors will start occurring when small_number/bigger_number<=eps (probably earlier).
So every time you see numbers spanning 15 orders of magnitude, be aware of machine precision. The same holds for comparing a result to 0. Using functions like ismembertol can save you from ugly code in such situations.
Categories
Find more on Numeric Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!