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 would like to show graphics repeatedly with changes in parameters. Neither of the following show anything. Nor do they give an error message.

Do[Plot[y = Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]

For[n = 1, n < 6, n++, Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]

These merely show the plot for n=1.

Do[Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]

Do[(Pause[1]; Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]), {n, 1, 5}]

For[n = 1, n < 6, n++, Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]]

(I tried Pauses in the others, to no avail.)

How do I make this work?

share|improve this question
1  
In addition to the great answers below I would also recommend taking a look at this URL. All those free apps are made with Mathematica and you can download the source code and see how it works. demonstrations.wolfram.com/search.html?query=parabola –  Vitaliy Kaurov Feb 21 '12 at 2:03
add comment

4 Answers

up vote 6 down vote accepted

You probably want Animate, if your intent is to show the plot varying for different n. Here's an example:

Animate[Plot[Sqrt[(1/n) x], {x, 0, 1}, PlotRange -> {All, {0, 1}}], {n, 1, 5}]

enter image description here

share|improve this answer
add comment

Try one of the following:

Table[ Plot[ Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]

or

 Plot[ Evaluate@Table[ Tooltip@Sqrt[(1/n) x], {n, 1, 5}], {x, 0, 1}]

The first method yields five different plots (as you seemed to expect; you don't need to sety = Sqrt[(1/n) x] in Plot since you don't make any use of it), while the latter visalizes differences between functions in one plot, where Tooltip makes show formulas of given functions.

Edit

One could follow this way further to make a visualization a bit more friendly like in R.M.'s answer :

Animate[
 Show[{Plot[Sqrt[x/k], {x, 0, 1}, PlotStyle -> Thick], 
       Plot[Evaluate@Table[Sqrt[x/n], {n, 1, 5}],
            {x, 0, 1}, PlotStyle -> Thick]}, 
       PlotRange -> {0, 1}], {k, 1, 5}]

enter image description here

share|improve this answer
add comment

What you use depends on what you are looking for.

But, first things first, neither Do nor For will work, as you found out, as both explicitly only return a value (a plot is considered a value) if Return is used. (See below for ways to get For and Do to work.) Unfortunately, Return breaks out of the For and Do immediately, so only the n==1 iteration is evaluated. So, it pays to use a function that is built for constructing lists rather than Do or For.

The simplest method of plotting a sequence of plots is as Artes said: use Table. Between the two forms he proposes, I prefer the second form as it plots the functions on the same graph, but the first form is more akin to what you were attempting. As an alternative, you could Map Plot over the values of n

Plot[Sqrt[(1/#) x], {x, 0, 1}]& /@ Range[1,5]

or,

Plot[ Evaluate[ Sqrt[(1/#) x]& /@ Range[1,5] ], {x, 0, 1}]

Additionally, you should look at Manipulate which allows you to manipulate the plot directly.

Manipulate[Plot[Sqrt[x/n], {x, 0, 1}, PlotRange -> {0, 1}], {n, 1, 5}]

gives

Note the use of PlotRange which fixes the y-axis to a specific range. If it is not present, the plot looks identical, but the y-axis changes in scale. Lastly, there's animating the whole thing, as R.M suggested.


As an aside, the above code is what one would usually use if they want to create a new List of objects. But, expressions can be constructed where both Do and For will in effect create a new List. The secret is to use Append or AppendTo on a List that already exists, or possibly using Reap and Sow.

Using your code for Do as an example,

 lst = {};
 Do[AppendTo[lst, Plot[Sqrt[(1/n) x] , {x, 0, 1}]], {n, 1, 5}]

or, more verbosely

 lst = {};
 Do[lst = Append[lst, Plot[Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]

and, most "simply"

 Reap[Do[Sow[Plot[Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]][[2]]
share|improve this answer
2  
Good answer explaining why OP's code doesn't work. The rest of us focused on the final objective and neglected the root of the OP's confusion :) –  rm -rf Feb 20 '12 at 19:31
add comment

Here is the really quick way: In your first Do loop, you can force Plot to create output by adding Print in front of it:

Do[Print@Plot[y = Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]

Now you decide you want to animate the result just to get an overview of the plots, but don't want to redo the loop. To do that, select all the output graphics by clicking the cell bracket containing them, then press Comand-y (or Control-y depending on the platform). This is also available under the menu Graphics > Rendering > Animate selected graphics.

This is what you do to just show the variations in a plot, as you asked. It's not the best choice if you want to permanently create a movie, of course.

share|improve this answer
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.