3
$\begingroup$

I am having trouble filling a particular region in a plot.

I am trying to use Mathematica to plot the same image I linked below, and this is ok (except for all the numbers/letters labelling but this is not important).

What I have trouble with, is how to fill the region below the $x$-axis but above the green curve.

Again, I would also know how instead fill the entire region above the green curve, but under the parabola. I tried many combinations of the filling code but no results.

Thank you!

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Please provide your code (copy and paste-able InputForm) for a minimal working example. $\endgroup$
    – Bob Hanlon
    Dec 19, 2022 at 20:00

3 Answers 3

7
$\begingroup$

One way is using RegionPlot to add such region.

Clear[f, g, plot, reg];
f[x_] = 4 + 1/16 (-9 - 4 Sqrt[5]) (-19 + 8 Sqrt[5] + x)^2;
g[x_] = Log[5, x + 2] - 1;
plot = Plot[{f[x], g[x]}, {x, -2.5, 4.5}, 
   PlotStyle -> {Darker@Cyan, Darker@Green}, AspectRatio -> Automatic];
reg = RegionPlot[{g[x] <= y <= f[x] && y <= 0}, {x, -2, 3}, {y, -2, 
    0}, PlotStyle -> LightRed, BoundaryStyle -> None];
Show[reg, plot, PlotRange -> {-4, 4}, AxesStyle -> Arrowheads[{0.05}],
  Axes -> True, Frame -> False, 
 GridLines -> {{{-2, {Thick, AbsoluteDashing[{3, 3}]}}}, None}, 
 Epilog -> {{AbsoluteDashing[3, 3], Line[{{-1, -1}, {-1, 0}}], 
    Line[{{-1, -1}, {0, -1}}]}}]

enter image description here

$\endgroup$
1
  • $\begingroup$ This is so aesthetically pleasing. Thank you! $\endgroup$
    – Heidegger
    Dec 20, 2022 at 8:55
5
$\begingroup$
Clear["Global`*"]

f1[x_] = -1 (2 x - 3) (x + 1);
f2[x_] = Log[(3 x + 6)/10];

plt = Plot[{f1[x], f2[x]}, {x, -3, 4},
   PlotRange -> {-5, 5}];

The intersections of the curves are

arg = Solve[{f1[x] == f2[x], -3/2 < x < 2}, x];

rng = LessEqual @@ Insert[x /. arg, x, 2];

f3[x_] = ConditionalExpression[f2[x], rng];

f4[x_] = ConditionalExpression[Min[f1[x], 0], rng];

Plot[{f1[x], f2[x], f3[x], f4[x]}, {x, -3, 4},
 Filling -> 3 -> {4},
 FillingStyle -> Lighter[ColorData[97][2], 0.7],
 PlotRange -> {-5, 5},
 PlotStyle -> {ColorData[97][1], ColorData[97][2],
   Opacity[0], Opacity[0]},
 PlotLegends -> Placed[
   {f1[x], f2[x], None, None},
   {0.8, 0.7}],
 AxesLabel -> (Style[#, 14] & /@ {x, f})]

enter image description here

$\endgroup$
3
$\begingroup$

As you did not specify any functions, therefore I choose something similar:

f1[x_] = -1 (2 x - 3) (x + 1);
f2[x_] = Log[(3 x + 6)/10];

To fill from f1 to the axis:

Plot[{f1[x], f2[x]}, {x, -3, 4}, PlotRange -> {-5, 5}, 
 Filling -> {2 -> 0}]

enter image description here

And to fill from f2 to f1:

Plot[{f1[x], f2[x], f3[x]}, {x, -3, 4}, PlotRange -> {-5, 5}, 
 Filling -> {2 -> {1}}]

enter image description here

Addendum

A fill from the axis down to f2 can be done with an auxillary function:

f3[x_] = If[Re@f2[x] < 0, 0, f2[x]]
Plot[{f3[x], f1[x], f2[x]}, {x, -3, 4}, PlotRange -> {-5, 5}, 
 Filling -> {1 -> {3}}]

![enter image description here

For filling only inside f1, chnage the auxillary function and f2 because of the pole:

f2[x_] = Log[(3 x + 6.1)/10]; 
f3[x_] = If[f1[x] <= Re@f2[x], f2[x], If[Re@f2[x] < 0, 0, f2[x]]]
Plot[{f3[x], f1[x], f2[x]}, {x, -3, 4}, PlotRange -> {-5, 5}, 
 Filling -> {1 -> {3}}]

enter image description here

$\endgroup$
5
  • $\begingroup$ This is good! In the first one, how to fill only the region below $x$-axis but above the orange curve? I mean the little region contained from $-1$ ish to $1.5$? And in the second one, only the region betweel the orange curve and the parabola, between $-1$ ish and $1.5$? $\endgroup$
    – Heidegger
    Dec 19, 2022 at 20:32
  • $\begingroup$ I added this case. $\endgroup$ Dec 19, 2022 at 21:01
  • $\begingroup$ Thank you for the work! But I actually meant only the small region inside the parabola, and above the green curve. Sorry for all this trouble! $\endgroup$
    – Heidegger
    Dec 19, 2022 at 21:15
  • 1
    $\begingroup$ I added this, but I think you should start doing something yourself. Otherwise you shall never learn Mathematica $\endgroup$ Dec 20, 2022 at 8:46
  • $\begingroup$ I tried a lot but I failed in what I wanted. And again, neither the last case you showed is what I wanted. I thank you really much for all the efforts, I learnt two new things from your answer though. I will keep learning, for sure! $\endgroup$
    – Heidegger
    Dec 20, 2022 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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