5

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?

rm -rf
  • 88,781
  • 21
  • 293
  • 472
Cobold
  • 153
  • 4

2 Answers2

7

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

István Zachar
  • 47,032
  • 20
  • 143
  • 291
4

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371