# Expression giving multiple plots of a function, where each plot varies according to the value for a certain parameter

How can I plot multiple plots for M = 10, 20, 50?

f[x_] = Piecewise[{{1, 0 <= x <= 1}, {-1, -1 <= x <= 0}}];

M = 50;

c = Parallelize[
Table[(2*n + 1)/2 Integrate[f[x]*LegendreP[n, x], {x, -1, 1}], {n,
1, M}]];


I would like to plot for each M at once.

Plot[{Sum[c[[n]]*LegendreP[n, x], {n, 1, M}], f[x]}, {x, -1, 1},
PlotStyle -> {{Red}, {Thick, Blue}}, GridLines -> Automatic]


How can I do this?

• Well, I may not get exactly what you want to do, but if I do the only thing you need to do is create Table for multiple M and Join this Table with f[x] and use this new list as your first argument for Plot. Commented Oct 2, 2013 at 22:57
• @Wizard wont that overlay them on the same plot? If so, I am looking to have them on different plots not on one. Commented Oct 2, 2013 at 22:59

Here's one way that straigthforwardly makes a table of plots:

Table[Plot[{Sum[c[[n]]*LegendreP[n, x], {n, 1, M}], f[x]}, {x, -1, 1},
PlotStyle -> {{Red}, {Thick, Blue}}, GridLines -> Automatic], {M, {10, 20, 50}}]


• Can each graph be in its own output window? Commented Oct 3, 2013 at 0:11
• Not sure what you mean by "window" but you can arrange them at your convenience. For instance, add //TableForm and the three are vertically oriented. Or use Grid or GraphicsRow or GraphicsColumn. Each of these has many display options (for spacing and other things). Commented Oct 3, 2013 at 0:17
• @dustin Perhaps Table[CreatePalette@Plot[...]] or CreateDocument is what you want. Commented Oct 3, 2013 at 0:57

Many ways to do this. This is another variation of Bill's answer using Do, Reap, Sow , Partition and Grid

f[x_] := Piecewise[{{1, 0 <= x <= 1}, {-1, -1 <= x <= 0}}];
m = 50;
c = Parallelize[Table[(2*n + 1)/2 Integrate[f[x]*LegendreP[n, x],{x, -1, 1}],{n,1,m}]];

r = Last@Reap@Do[Sow@Plot[{Sum[c[[n]]*LegendreP[n, x], {n, 1, m}],
f[x]}, {x, -1, 1}, PlotStyle -> {{Red}, {Thick, Blue}}, GridLines -> Automatic,
AxesLabel -> {x, Row[{"n=", m}]},ImagePadding -> 30], {m, 1, 50, 2}];

Grid[Partition[r[[1, All]], 5], Frame -> All, Spacings ->{1, 1},FrameStyle->LightGray]


• Can each graph be in its own output window? Commented Oct 3, 2013 at 0:10