# Unexpected result: Numerical and analytical results do not match! [closed]

I am calculating the eigenvalues and eigenvectors of a matrix. This is my code:

n = 6;

M = m IdentityMatrix[n - 1];

K = Table[0, {j, 1, n - 1}, {i, 1, n - 1}];

For[j = 1, j <= n - 1, j++,
For[i = 1, i <= n - 1,
i++, {If[j == i, K[[j, i]] = 2 k, Nothing],
If[i == j + 1, K[[j, i]] = -k, Nothing],
If[i == j - 1, K[[j, i]] = -k, Nothing]}]]

sol = Eigensystem[Inverse[M].K];

ϕϕ = Transpose@Reverse@sol[];

diagM = Transpose[ϕϕ].M.ϕϕ;

MatrixForm@Chop@diagM


This produces the following matrix $$diagM =\left( \begin{array}{ccccc} 12 m & 0 & 0 & 0 & 0 \\ 0 & 4 m & 0 & 0 & 0 \\ 0 & 0 & 3 m & 0 & 0 \\ 0 & 0 & 0 & 4 m & 0 \\ 0 & 0 & 0 & 0 & 12 m \\ \end{array} \right)$$

However applying numerical data

m = 247(*kg*);
L = 6.8(*m*);
II = 4.55*10^-5(*m^4*);
A = 0.0048(*m^2*);
Subscript[F, 0] = 268(*N*);
EE = 2.1*10^11(*Pa*);
n = 6;
k = 1.61 ((EE II)/(2 (L/n))^3) (L/n)^2;


And running the code above again produces a completely different matrix

$$diagM=\left( \begin{array}{ccccc} 247. & 0 & 0 & 0 & 0 \\ 0 & 247. & 0 & 0 & 0 \\ 0 & 0 & 247. & 0 & 0 \\ 0 & 0 & 0 & 247. & 0 \\ 0 & 0 & 0 & 0 & 247. \\ \end{array} \right)$$

Notice the different diagonal elements!

Please help me understand why and which is now correct? This obviously affects my final result.

## closed as off-topic by Daniel Lichtblau, m_goldberg, Bob Hanlon, MarcoB, Karsten 7.May 6 '16 at 22:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, m_goldberg, Bob Hanlon, MarcoB, Karsten 7.
If this question can be reworded to fit the rules in the help center, please edit the question.

The numerical definitions you give later modify the calculation of the symbolic definitions you use earlier. If you clear all your variables at the start of your calculation, the problem goes away and repeatable results are obtained. In particular, it is the numerical definition of k that seems to muddy the waters.

In order to do what you want, a far better approach would be to use replacement rules to inject numerical values into your symbolic result (i.e. diagM /. m -> 247 etc.), or to localize the numerical values using e.g. Block:

Block[
{
m = 247(*kg*),
L = 6.8(*m*),
II = 4.55*10^-5(*m^4*),
EE = 2.1*10^11(*Pa*),
k = 1.61 ((EE II)/(2 (L/n))^3) (L/n)^2
},
MatrixForm@Chop@(diagM)
]


Notice that the code above does not contain definitions for n and A, which do not influence diagM.

The results are then: which are of course completely compatible with each other.