# Is there any way to use Manipulate for eigenvectors calculation and their square plot

I have a matrix $H_{n\times n}$ . Now My job is to diagonalize it, we have $V1_{n\times n}$(set of eigenvectors of $H$). But my $H_{n\times n}$ is in terms of some parameters or variables(e.g. $t1$ and $t2$).I will fix one $t2 = 1$, will change $t1$. For example $t1$ goes from $0 \rightarrow10$. But I don't want to write all the values for $t1$ everytime. As we have Manipulate in Mathematica which does the job.

I have $H_{n\times n}(t1,t2)$ and then I am getting eigenvectors set (row wise) $V1_{n\times n}(t1=0\rightarrow10,t2=1)$, any value between $0$ and $10$ for $t1$. Then Matrix Plot of the $V=V1_{n\times n}(t1=t_{0},t2=1)*V1_{n\times n}(t1=t_{0},t2=1)$.

V1 = Eigenvectors[H /. {t1 -> 0.1, t2 -> 1}];
V = V1*V1;
MatrixPlot[V]


Is there a way so that I can use Manipulate for this Matrix plot for different set of $t1$ values. And I need not have to write this command for each values of $t1$.

Note: For a particular t1. For $t1 -> 10.0$, $t2 -> 1$. This is the Plot of V(n=20).

• Anybody has any answer to this weird problem in Mathematica.
– L.K.
Commented Sep 27, 2016 at 14:17

You say your H is dependent on two parameters, t1 and t2, and that you can fix one of them, say, t2. Hence H will be a function of t1, which I will denote with t in what follows (for illustrative purposes).

Having said that, let's define H[t] to be an explicit function of t. I'll take some random matrix for illustration:

n = 3;
H[t_] := RandomInteger[{0, 9}, {n, n}] + RandomChoice[{0, 1}, {n, n}] t;
MatrixForm@H[t]


and enclose Eigenvectors and MatrixPlot in a Module:

mp[t_] := Module[{V1, V},
V1 = Chop@Eigenvectors[H[t]];
V = V1*V1;
MatrixPlot[V, PlotLegends -> Automatic]
]


which one can Manipulate straightforward:

Manipulate[mp[t], {t, 0, 10, 1.}]


One can of course make the matrix H be a function of two or more variables, e.g. H[t1,t2,...] (with an according change of mp into mp[t1,t2,...]), and add additional controls to Manipulate, which will just have more sliders, for each of the variables:

Ndim = 10;

H1[t1_] := {{0, t1}, {t1, 0}};
H2[t2_] := N@{{0, 0}, {t2, 0}};

H[t1_, t2_] :=
ArrayFlatten@
Table[H1[t1] KroneckerDelta[m, p1] +
H2[t2] KroneckerDelta[m + 1, p1] +
Transpose@H2[t2] KroneckerDelta[m - 1, p1], {m, 1, Ndim}, {p1, 1,
Ndim}];

mp[t1_, t2_] := Module[{V1, V}, V1 = Chop@Eigenvectors[H[t1, t2]];
V = V1*V1;
MatrixPlot[V, PlotLegends -> Automatic]]

Manipulate[mp[t1, t2], {t1, 0, 10, 1.}, {t2, 0, 10, 1.}]

• Thank you for the code. I ran it and got this. See this Code Output
– L.K.
Commented Sep 30, 2016 at 11:20
• Go with: H1[t_] := {{0, t}, {t, 0}};, H[t_] := ArrayFlatten... and remember to change H1 into H1[t] in def of H[t]. Everything is a function but you didn't take that into account. Commented Sep 30, 2016 at 11:28
• V=V1*V1 not V1.V1.
– L.K.
Commented Sep 30, 2016 at 11:28
• I ran the code, but this came: Set::write: Tag List in {{0,t},{t,0}}[t_] is Protected. and Set::write: Tag List in {{0,t,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{t,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,1,0,t,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,t,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,1,0,t,0,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,t,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,1,0,t,0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,t,0,1,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,1,0,t,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,t,0,1,0,0,0,0,0,0,0,0,0}{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,t,0}}[t_] is Protected.
– L.K.
Commented Sep 30, 2016 at 11:31
• Please do not remove N@ from the code, this causes the code to malfunction. Commented Oct 4, 2016 at 11:07

corey979's answer is good, and I have used the code to create the matrix from his answer. You can also stay closer to your initial version (using replace rather than a function call):

n = 5;
h = RandomInteger[{0, 9}, {n, n}] + RandomChoice[{0, 1}, {n, n}] t;
Manipulate[v = Eigenvectors[h /. t -> tval]; MatrixPlot[v*v], {tval, 0, 10}]


For the exact form of H you define in the comment below, the code would be:

Ndim = 10; H1[t_] := {{0, t}, {t, 0}}; H2 = N@{{0, 0}, {1, 0}};
H[t_] := ArrayFlatten@
Table[H1[t] KroneckerDelta[m, p1] + H2 KroneckerDelta[m + 1, p1] +
Transpose[H2] KroneckerDelta[m - 1, p1], {m, 1, Ndim}, {p1, 1, Ndim}];
Manipulate[v = Eigenvectors[H[t] /. t -> tval]; MatrixPlot[v*v], {tval, 0, 10}]

• I want to plot v*v as a function of t.
– L.K.
Commented Sep 30, 2016 at 11:08
• After running this I got Output
– L.K.
Commented Sep 30, 2016 at 11:25
• L.K. The code in your output file is not the same as I proposed above. Remove the MatrixForm from H. Commented Sep 30, 2016 at 12:48
• THis is the output I got New Output
– L.K.
Commented Sep 30, 2016 at 12:53
• I removed the : from H definition, then also same output
– L.K.
Commented Sep 30, 2016 at 12:55