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.

For example, I want to generate a list of random waves and plot them.

Then I wrote

f:=Random[Real]
h:=Floor[f*20]

And then

Manipulate[Plot[Table[f Sin[h x + h t], {n, 0, 5}], {x, -5, 5}, PlotRange -> {-2, 2}], {t, 0, 10}]

But it turned out a mess.

I tried to fix it with

a = Table[f Sin[ h x + h t], {n, 0, 5}]
Manipulate[Plot[a, {x, -5, 5}], {t, 0, 10}]

But no graph was generated.

Finally I tried

a = Table[f Sin[ h x + h t], {n, 0, 5}]
Manipulate[Plot[a /. t -> b, {x, -5, 5}], {b, 0, 10}]

This time the graph was correct. But there are still 2 problems:

  1. All functions are in the same color.
  2. It lags a lot when I tried to manipulate t.

These problems don't exist if you write by hand the list inside plot.

So what is the correct way to solve these problems and get the desired output?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

I used 3 rather than 6 functions for each plot and used a Frame rather than Axes to reduce the clutter.

f := RandomReal[];
h := Floor[f*20];

When the Table is inside of the Plot use Evaluate

Manipulate[
 Plot[
  Evaluate[
   Table[f Sin[h x + h t], {3}]],
  {x, -5, 5},
  PlotRange -> {-1.1, 1.1},
  Frame -> True,
  Axes -> False],
 {{t, 5}, 0, 10, 1, Appearance -> "Labeled"}]

enter image description here

Manipulate[
 a = Table[f Sin[h x + h t], {3}];
 Plot[a, {x, -5, 5},
  PlotRange -> {-1.1, 1.1},
  Frame -> True,
  Axes -> False],
 {{t, 5}, 0, 10, 1, Appearance -> "Labeled"}]

enter image description here

Similarly, the ReplaceAll needs to be in an Evaluate

a = Table[f Sin[h x + h t], {3}];
Manipulate[
 Plot[Evaluate[a /. t -> b], {x, -5, 5},
  PlotRange -> {-1.1, 1.1},
  Frame -> True,
  Axes -> False],
 {{b, 5}, 0, 10, Appearance -> "Labeled"}]

enter image description here

share|improve this answer

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.