# ListPlot in a Loop [closed]

I have the following program which generates the plot from a data list(i.e. Data1). However I want to print some plots for some values of the parameter "a". How would I proceed to print the scatter plots in the For loop (side by side as a table preferrably). The Code is as follows.

Data1 = {};
del = 0.1;
delx = 1/del;
n = 10;
a = 2;(* I would like to use various values of this variable in a loop throughout the code*)
(*************From Here************)
Vx = SparseArray[{}, {n, n}];

For[k = -2*Pi/a, k <= 2*Pi/a, k = k + 0.01,
ConsMat = ((k^2)/2)*IdentityMatrix[n];
dfdx = (SparseArray[{Band[{1, 2}] -> -I*k*delx,
Band[{2, 1}] -> I*k*delx, {1, n} ->
I*k*delx, {n, 1} -> -I*k*delx}, {n, n}]);
d2fdx2 = -1*
SparseArray[{Band[{1, 1}] -> -(delx^2), Band[{1, 2}] -> delx^2,
Band[{2, 1}] -> delx^2, {1, n} -> delx^2, {n, 1} -> delx^2}, {n,
n}];

p = 1;
For[x = 1, x <= 5, x = x + 5/n,
Vx[[p, p]] = (Cos[2 Pi*x/a] - 1);
p++;
];
p = 1;

Hamil = (dfdx + d2fdx2 + ConsMat) + Vx;
EigenVal = Eigenvalues[Hamil];

For[j = 1, j <= Length[EigenVal], j++,
Coord = {k, EigenVal[[j]]};
AppendTo[Data1, Coord];
];

];
ListPlot[Data1]
(**************To Here********************)

• It will help you if you don't use For loops in Mathematica. You can use Print to display multiple plots in a loop, e.g. Do[Print@Plot[Sin[k x], {x, 0, 10}], {k, 1, 5}]. Or you can use Table to collect them into a list. Table[Plot[Sin[k x], {x, 0, 10}], {k, 1, 5}]. Use Column or Row to format the list. Jan 17, 2017 at 10:43
• Next time please try to show a minimal example. Could you not have illustrated the same problem with much less code? See sscce.org for guidance. Jan 17, 2017 at 10:44
• Thank you for your information.I would definitely keep this in mind. Jan 17, 2017 at 13:29

Create a function using Module which returns the plot.

listP[a_] :=
Module[{Data1, del, delx, n, k, Vx, dfdx, d2fdx2, ConsMat, p, x,
Hamil, EigenVal, j, Coord},
Data1 = {}; del = 0.1; delx = 1/del; n = 10;
Vx = SparseArray[{}, {n, n}];
For[k = -2*Pi/a, k <= 2*Pi/a, k = k + 0.01,
ConsMat = ((k^2)/2)*IdentityMatrix[n];
dfdx = (SparseArray[{Band[{1, 2}] -> -I*k*delx,
Band[{2, 1}] -> I*k*delx, {1, n} ->
I*k*delx, {n, 1} -> -I*k*delx}, {n, n}]);
d2fdx2 = -1*
SparseArray[{Band[{1, 1}] -> -(delx^2), Band[{1, 2}] -> delx^2,
Band[{2, 1}] -> delx^2, {1, n} -> delx^2, {n, 1} ->
delx^2}, {n, n}];
p = 1;
For[x = 1, x <= 5, x = x + 5/n, Vx[[p, p]] = (Cos[2 Pi*x/a] - 1);
p++;];
p = 1;
Hamil = (dfdx + d2fdx2 + ConsMat) + Vx;
EigenVal = Eigenvalues[Hamil];
For[j = 1, j <= Length[EigenVal], j++, Coord = {k, EigenVal[[j]]};
AppendTo[Data1, Coord];];];
ListPlot[Data1, Frame -> True]
];


Then create a table of plots for different values of a

Table[listP[a], {a, 1, 3}]