Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Wizard Oct 2 '13 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. –  dustin Oct 2 '13 at 22:59
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
Can each graph be in its own output window? –  dustin Oct 3 '13 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). –  bill s Oct 3 '13 at 0:17
    
@dustin Perhaps Table[CreatePalette@Plot[...]] or CreateDocument is what you want. –  Michael E2 Oct 3 '13 at 0:57
add comment

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

share|improve this answer
    
Can each graph be in its own output window? –  dustin Oct 3 '13 at 0:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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