5
$\begingroup$

I have a function, which plots the given function array f. This is a part of the function:

Plot[-f, {x,0,2}, Filling->{1->{2}}, PlotStyle->Green]

When I specify {x^2, 2x} as the function f, I get the errors "2 must be an integer between 1 and 1. " and "{2} is not a valid Filling specification.". I get this error because I have a minus sign in front of the argument f. If I plot {-x^2,-2x} then everything is fine. So how to multiply everything in f by -1 without getting these errors?

$\endgroup$
7
$\begingroup$

You need to evaluate the first argument of Plot before sampling the specific datapoints, as the multiplication (the minus sign before the list of functions) does not get thread over the list by default, as Plot has attribute HoldAll. This means that your example Plot is practically called with only one function (which is a List) and therefore the Filling specification does not make sense. Discussed in more detail here.

Plot[Evaluate[-{x^2, 2 x}], {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green]

This effectively equals the following (note that the - signs are in front of the individual functions):

Plot[{-x^2, -2 x}, {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green]

Both produce the following correct plot:

Mathematica graphics

$\endgroup$
4
$\begingroup$

As István Zachar describes this is an evaluation problem. I recommend however using a different form:

Plot[-f, {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green, Evaluated -> True]

This is superior to Evaluate[-f] in that the Plot variable (x) is correctly localized.

For example:

f := {x^2, 2 x};

x = 7; (* accidental definition *)

Plot[-f, {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green, Evaluated -> True]

Mathematica graphics

The other method fails:

Plot[Evaluate[-f], {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green]

Mathematica graphics

$\endgroup$
  • 1
    $\begingroup$ +1 for the foolproof method. Am I right in assuming that this is an undocumented option (I never heard of before)? $\endgroup$ – István Zachar May 28 '12 at 9:28
  • 2
    $\begingroup$ @István as far as I know it is undocumented, but it has been around for a while. $\endgroup$ – Mr.Wizard May 28 '12 at 9:46
  • $\begingroup$ We really need a comprehensive list of undocumented symbols and string options. I'm not sure adding Evaluated to this huge and messy list would help anyone... $\endgroup$ – István Zachar May 28 '12 at 10:10

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.