1
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Wizard
    Commented Oct 2, 2013 at 22:57
  • $\begingroup$ @Wizard wont that overlay them on the same plot? If so, I am looking to have them on different plots not on one. $\endgroup$
    – dustin
    Commented Oct 2, 2013 at 22:59

2 Answers 2

6
$\begingroup$

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}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Can each graph be in its own output window? $\endgroup$
    – dustin
    Commented Oct 3, 2013 at 0:11
  • $\begingroup$ 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). $\endgroup$
    – bill s
    Commented Oct 3, 2013 at 0:17
  • $\begingroup$ @dustin Perhaps Table[CreatePalette@Plot[...]] or CreateDocument is what you want. $\endgroup$
    – Michael E2
    Commented Oct 3, 2013 at 0:57
5
$\begingroup$

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]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Can each graph be in its own output window? $\endgroup$
    – dustin
    Commented Oct 3, 2013 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.