2
$\begingroup$

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?

$\endgroup$
3
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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