# Defining Functions

I have the following matrix:

  M = {{En + 2 Cos[k a] t, t1 + t2 Cos [k a]},{t1 + t2 Cos [k a], En + 2 Cos[k a] t}};


I want the eigen value to be function of t,t1 and t2 so that they can be manipulated: So I do the following:

  eig=Eigenvalues[M];
eg[t1_, t_, t2_] := eig[[1]];


But in the information of eg, it shows:

Globaleg
eg[t1_,t_,t2_]:=eig[[1]]


But if I print out eig and copy the expression in

eg[t1_,t_,t2_]:=En - t1 + 2 t Cos[a k] - t2 Cos[a k];(*The expression of eig[[1]]*)


I get what I want. Could someone explain me where I am going wrong and how to get it correct without the copy pasting.

There are two options:

eg[t1_, t_, t2_] := Evaluate[eig[[1]]]


or

eg[t1_, t_, t2_] = eig[[1]]


The reason for this is eig needs to be evaluated before the function is called with the arguments. If you use SetDelayed, the eig[[1]] remains unevaluated until the function is called. Let me illustrate.

fun = {x + 1, y};
f[x_] := fun[[1]];
g[x_] = fun[[1]];
?f
?g


f[x_]:=fun[[1]]

g[x_]=1+x

f[2]
g[2]


1+x

3

Let's Trace

f[2] // Trace
g[2] // Trace


{f[2],fun[[1]],{fun,{1+x,y}},{1+x,y}[[1]],1+x}

{g[2],1+2,3}

So, as you see, f ignores the argument since pattern matching of the argument to the RHS is done first in a function call, but fun as a symbol doesn't match to x_. The RHS is then evaluated, but by the time x appears, the pattern matching was over. In the case of g, the RHS contains x, so the argument is matched and the whole expression is evaluated with the new value. Instead of Setyou can force evaluate the RHS using Evaluate`.