4

Suppose I have an equation plotted as shown:

Plot[(HarmonicNumber[K] - HarmonicNumber[K - r]) (HarmonicNumber[K] - 
 HarmonicNumber[-1 + r]) /. K -> 10, {r, 0, 11}, Filling -> 0.4]

image

But what I really want is to have the two (triangular?) filling FLIPPED inside at $x \approx2$ and $x \approx9$. The direction of filling of the upper (semi-circular) part is not important. Preferably it can be Top/outside.

Can anybody help with this flipping?

Thanks

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Afloz
  • 261
  • 2
  • 8

4 Answers4

6

Another way, copying once more from @J.M.'s answer here: How can I fill under a function in a plot just to right of a specified vertical line?

Using @b.gatessucks definition of f:

f[r_, k_] = (HarmonicNumber[k] - HarmonicNumber[k - r]) (HarmonicNumber[k] - 
      HarmonicNumber[-1 + r])

we can do:

With[{ff = f[r, 10]}, 
   Plot[{ConditionalExpression[ff, ff < 0.4], 
         ConditionalExpression[ff, ff >= 0.4]}, {r, 0, 11}, 
   Filling -> {1 -> Axis, 2 -> Top}, PlotStyle -> ColorData[1, 1], 
   FillingStyle -> LightRed]]

based on the ideas linked above (using ConditionalExpression), getting the same result as in @b.gatessucks' answer.

The advantage of this approach is that you can easily modify the fillings (and other options) for the individual parts (but if you use a complicated function, I suspect it could turn slow (due to the added conditions)

Pinguin Dirk
  • 6,519
  • 1
  • 26
  • 36
5

One way :

f[r_, k_] = (HarmonicNumber[k] - HarmonicNumber[k - r]) (HarmonicNumber[k] - 
 HarmonicNumber[-1 + r])

sol1 = r /. FindRoot[f[r, 10] == 0.4, {r, 2}];
sol2 = r /. FindRoot[f[r, 10] == 0.4, {r, 9}];
m = FindMaximum[f[r, 10], {r, sol1, sol2}][[1]];

Show[Plot[f[r, 10], {r, 0, 11}], 
     Plot[f[r, 10], {r, 0, sol1}, Filling -> Bottom], 
     Plot[f[r, 10], {r, sol1, sol2}, Filling -> {1 -> m}], 
     Plot[f[r, 10], {r, sol2, 11}, Filling -> Bottom]]

enter image description here

b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
4

You could draw an invisible second function (a block) and have the filling occur between the two:

f[r_, k_] = (HarmonicNumber[k]-HarmonicNumber[k-r])(HarmonicNumber[k]-HarmonicNumber[-1 + r]);

max = NMaxValue[{f[r, 10], 0 < r < 11}, r]
min = NMinValue[{f[r, 10], 0 <= r <= 11}, r]

Plot[
  {
    f[r, 10], 
    Rescale[ Boole[f[r, 10] > 0.4], {0, 1}, {min, max}]
  }, 
  {r, 0, 11}, 
  Filling -> {1 -> {2}}, 
  PlotStyle -> {Automatic, None}
]

enter image description here

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
2

For version 7:

Warning: for simplicity r is not localized in either method; Formal Symbols advised in practice.

hf[K_] := With[{H = HarmonicNumber}, (H[K] - H[K - r]) (H[K] - H[r - 1])]

Plot[
 {If[hf[10] < 0.4, hf[10]], If[hf[10] >= 0.4, hf[10]]},
 {r, 0, 11}, 
 Filling -> {1 -> Bottom},
 PlotStyle -> Black
]

enter image description here

Or:

Plot[
 {If[hf[10] < 0.4, hf[10]], If[hf[10] >= 0.4, hf[10]]},
 {r, 0, 11}, 
 Filling -> {1 -> Bottom, 2 -> Top},
 FillingStyle -> LightGray,
 PlotStyle -> Black
]

enter image description here

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