Message Boards Message Boards

0
|
3319 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Different outputs with same inputs

Posted 11 years ago

Why if I plot

Plot[Table[a x, {a, 5}], {x, -3, 3}]

all the lines have same colour but if I separate in this way

Table[a x, {a, 5}]; Plot[%, {x, -3, 3}]

the lines have different colour?

POSTED BY: g p
2 Replies
Posted 11 years ago

Thanks for your answer!!

Now the question is:

Why has the plot function the attribute HoldAll???

Thanks again :)

POSTED BY: g p

This is because the plot function has the attribute HoldAll. Because of this, the Plot function cannot tell that the head of the first argument is a list since the Table is not evaluated and, hence, has the Head Table. Generally when plotting something that will evaluate to a list of functions you should override the HoldAll for the first argument in this way:

Plot[Evaluate[Table[a x, {a, 5}]], {x, -3, 3}]

This serves two purposes. One is the one I just mentioned. The other one--and more important one--is that the Plot will then not need to evaluate the Table for every point on the graphs. It will evaluate the Table once before plotting and then plot the separate functions. This can often lead to a very significant performance advantage, especially if the evaluate of the first argument to yield the list of functions is itself time consuming.

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract