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.

I am wondering why the following does not plot the way it's written and exactly how does the Do[] function operate? I think I could be using it incorrectly.

In[1]:= u[x_, t_] := (1/2) (Exp[-(x + t)^2] + Exp[-(x - t)^2])
        Do[
           Plot[u[x, k], {x, -5, 5}, 
           AxesLabel -> {x, u}, 
           PlotRange -> {0, 1}],
           {k, 0, 3}
          ]

EDIT

In[2]:= Clear["Global`*"]
        a=1;
        b=1;
        \[Phi][\[Xi]_,\[Nu]_,t_]:=\!\(
        \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(10\)]\(
        \*UnderoverscriptBox[\(\[Sum]\), \(j = 1\), \(10\)]\((
        \*FractionBox[\(5\ \((\(-1\) + 
        \*SuperscriptBox[\((\(-1\))\), \(j\)])\)\ \((\(-1\) + 
        \*SuperscriptBox[\((\(-1\))\), \(i\)])\)\), \(j\ i\ 
        \*SuperscriptBox[\(\[Pi]\), \(2\)]\)] Cos[
        \*FractionBox[\(1\), 
        SuperscriptBox[\(\[Pi]\), \(4\)]] 
        \*SqrtBox[\(
        \*SuperscriptBox[\(i\), \(4\)] 
        \*SuperscriptBox[\(\[Pi]\), \(4\)] + 
        \*SuperscriptBox[\(j\), \(4\)] 
        \*SuperscriptBox[\(\[Pi]\), \(4\)]\)] t])\) Sin[i\ \[Pi]\ \[Xi]] Sin[j\ \[Pi]\                   
        \[Nu]]\)\);
        Do[Print@
           Plot3D[\[Phi][\[Xi],\[Nu],m],{\[Xi],0,a},{\[Nu],0,b},
           Mesh:>Automatic,
           PlotLabel:>"Surface Plot of Solution at t = "<>ToString[m],
           ColorFunction:>{"BlueGreenYellow"}],
           {m,0,10,2}
          ]
share|improve this question
    
Strongly related: mathematica.stackexchange.com/q/5682/121 –  Mr.Wizard Jun 22 '12 at 19:09

3 Answers 3

up vote 10 down vote accepted

The reason is that since version 6 of Mathematica, the Plot returns its output just like any other function. It does not print is as a side effect. Do is meant to be used with operations that have side effects and simply discards the output, so you never see the plot.

You can either use Table, which collects the values in a list:

Mathematica graphics

Note how you get a list of graphics objects. Graphics are expressions like any other, they're simply shown using a special formatting. You can always see their internal structure by using InputForm on them or pressing Ctrl-Shift-I on the containing cell.

Or you can use Print explicitly:

Do[Print@Plot[u[x, k], {x, -5, 5}, AxesLabel -> {x, u}, 
   PlotRange -> {0, 1}], {k, 0, 3}]

Note: If as @JM noted, you are trying to animate the plot, use Animate:

Animate[Plot[u[x, k], {x, -5, 5}, AxesLabel -> {x, u}, 
  PlotRange -> {0, 1}], {k, 0, 3, 1}]
share|improve this answer
    
I have a strong feeling OP's code was taken from an old Mathematica book, and the intention was to animate the plot; you might thus want to mention Animate[]. –  J. M. Jun 22 '12 at 8:27
    
@J.M.: No this was not the intention, I was trying to find a more convenient way of making multiple plots when necessary by the the computer do the work for me in a more programmatic fashion rather than copy/paste several Plot commands or use Manipulate over a certain range (parameter). But that does sounds interesting now that you bring that idea up. Thanks :) –  night owl Jun 22 '12 at 9:56
    
@nightowl You can use Row/Column/Grid to arrange the plots in a nicer layout –  Szabolcs Jun 22 '12 at 9:57
    
@night owl: I see; as I said, it's exactly how animations were done in old Mathematica, so that's what I thought you were doing. Then indeed, Table[] is what you want (though that too was used for producing animations as well for subsequent exporting). –  J. M. Jun 22 '12 at 10:07
    
@Szabolcs: Thanks! I will look into how to arrange them nicely using the suggestions you've given. –  night owl Jun 23 '12 at 2:20

Another way is to use DisplayFunction, something before the time of graphic input/output of version 6. The following line prints the same way as Szabolcs's 2nd example.

Do[Plot[u[x, k], {x, -5, 5}, AxesLabel -> {x, u}, PlotRange -> {0, 1},
   DisplayFunction -> Print], {k, 0, 3}]

DisplayFunction -> f wraps the actual output of any Graphics function (Plot here) into f. This way one can directly send the plot to any pipe, for example to a stream or file. If you are version 6+ though, the best solution is to use Table, just as Szabolcs has said.

share|improve this answer
    
Thanks for that alternative method. Could you read the last comment under Szabolcs post. I want to try and use GraphicsGrid with the Do command. Also, another concern came up. What is the most efficient way of using the iterator for the Do command. Because at most it take three arguments, e.g. Do[expr,{k,min,max,step-size}]. But what if you have a function such as $\phi[\xi_,\nu_,t]$, and you want to plot for value of time say: $t=0,0.3,0.7,0.10,0.14,0.16$. As you can notice, the time instances (steps) are not by the same amount, so how could you account for this when using –  night owl Jun 23 '12 at 8:10
    
the Do command. Or is there something you can add.. Or would you just have to input the times in manually for a case like this? –  night owl Jun 23 '12 at 8:11
2  
@nightowl the syntax you are looking for is Do[f[x], {x, {0, 0.3, 0.7, 0.10, 0.14, 0.16}}] -- this also works for Table. Also realize you could use Scan or Map, e.g. Scan[f, {0, 0.3, 0.7, 0.10, 0.14, 0.16}] –  Mr.Wizard Jun 23 '12 at 8:29
    
@Mr.Wizard: Oh my! Thank You. This helps alot! :). So could you ("usually") input list in place of an iterator if you have several values to input with staggering increments of time? Now only to get GraphicsGrid to work in junction with it and that will complete my handy toolbox for the moment. :) Do each graphic have a internal name associated with them or could one be assigned to them. Because I know that GraphicsGrid[{{g_11,g_12,...},...}] needs to have variables (functions) hence -> ($g_{11},g_{12}$) assigned for input to create the 2-D grid. –  night owl Jun 23 '12 at 8:57
    
@nightowl I'm not sure what you're trying to do but it's quite possible you don't realize you can pass a table of graphics objects directly to GraphicsGrid: GraphicsGrid[ Table[Plot[k Sin[x] + x, {x, 0, 3 Pi}], {k, 4}] ~Partition~ 2 ] (Here I generate four plots with Table, split it into a 2x2 table with Partition, and pass the result to GraphicsGrid.) –  Mr.Wizard Jun 23 '12 at 9:38

Since this is getting a bit lengthy for comments:

What is the most efficient way of using the iterator for the Do command. Because at most it take three arguments, e.g. Do[expr,{k,min,max,step-size}]. But what if you have a function such as ϕ[ξ,ν,t], and you want to plot for value of time say: t=0,0.3,0.7,0.10,0.14,0.16. As you can notice, the time instances (steps) are not by the same amount, so how could you account for this

The syntax you are looking for is:

Do[f[x], {x, {0, 0.3, 0.7, 0.10, 0.14, 0.16}}]

This also works for Table. In simple cases such as this example you could use Scan or Map:

Scan[f, {0, 0.3, 0.7, 0.10, 0.14, 0.16}]

Do each graphic have a internal name associated with them or could one be assigned to them. Because I know that GraphicsGrid[{{g_11,g_12,...},...}] needs to have variables (functions) hence -> (g11,g12) assigned for input to create the 2-D grid.

No, the elements of the table given to GraphicsGrid do not need to be assigned to symbols first. Here is an example using the function given in your question:

u[x_, t_] := (1/2) (Exp[-(x + t)^2] + Exp[-(x - t)^2])

plots =
 Table[
   Plot[u[x, k], {x, -5, 5}, AxesLabel -> {x, u}, PlotRange -> {0, 1}],
   {k, 0, 3}
 ];

Partition[plots, 2] // GraphicsGrid

Mathematica graphics

share|improve this answer
    
Okay, Thanks. I don't see what Scan does. It must be something internally but does not return anything to the screen. And Map just seems to do something weird like {Cos[x][0],Cos[x][0.3],...} with f being Cos[x], from your example shown. And I'm glad to see that it works for Table, but my question was how to get it to work with the example I provided in the question underneath EDIT using the Do command. –  night owl Jun 23 '12 at 21:45
    
@nightowl Scan is to Map as Do is to Table. Try: Scan[Print, {1, 2, 3, 4, 5}] –  Mr.Wizard Jun 23 '12 at 23:33

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.