5
$\begingroup$

I cannot understand how to fill the triangle, and only it, made by the given lines and the x-axis. Here is the code I have tried:

Clear[f, g, plot, reg];
f[x_] = -x + 6;
g[x_] = 2 x - 4;
plot = Plot[{f[x], g[x]}, {x, 0, 23}, 
PlotStyle -> {Darker@Cyan, Darker@Green}, AspectRatio -> Automatic];    
reg = RegionPlot[{f[x] <= g[x] && y <= 8/3}, {x, 2, 6}, {y, 0, 8/3}, 
PlotStyle -> LightGray, BoundaryStyle -> None];
Show[reg, plot, PlotRange -> {-1, 5}, AxesStyle -> Arrowheads[{0.05}],
Axes -> True, Frame -> False]

whose output is as follows:

enter image description here

Any help? Thank you.

I was messing around with the functions f and g, but I cannot find a way to fill only the triangle.

$\endgroup$
1
  • $\begingroup$ reg = RegionPlot[{y <= g[x] && y <= f[x]}, {x, 2, 6}, {y, 0, 8/3}, PlotStyle -> LightGray, BoundaryStyle -> None] $\endgroup$
    – cvgmt
    Feb 12 at 23:53

2 Answers 2

7
$\begingroup$
f[x_] = -x + 6;
g[x_] = 2 x - 4;
h[x_] := Min[g[x], f[x]];
Plot[{f[x], g[x], h[x]}, {x, 0, 23},
 AspectRatio -> Automatic,
 PlotStyle -> {Darker@Cyan, Darker@Green, None},
 Filling -> {3 -> {Axis, {None, Blue}}}]
$\endgroup$
8
$\begingroup$
Clear[f, g, reg];

f[x_] := -x + 6;
g[x_] := 2 x - 4;


reg = ImplicitRegion[y <= g[x] && y <= f[x] && y > 0, {x, y}];

p1 = Plot[{f[x], g[x]}, {x, 0, 8}
   , PlotRange -> {-1, 4}
   , AxesOrigin -> {0, 0}
   , PlotStyle -> {Darker@Cyan, Darker@Green}
   , PlotRangePadding -> {{0, 2}, {0, 0}}
   ];

p2 = RegionPlot[reg
   , PlotStyle -> HatchFilling[]
   , BoundaryStyle -> None];

gr = Graphics[{HatchFilling[], Rectangle[]}
   , ImageSize -> Small
   ];

legend = LineLegend[{Darker@Cyan, Darker@Green, White}
   , {f[x], g[x], TraditionalForm@reg[[1]]}
   , LegendMarkers -> {None, None, gr}
   , LegendMarkerSize -> 20];

Legended[
 Show[p1, p2]
 , Placed[legend, {0.7, 0.8}]
 ]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Oh wow, that is awesome!!! $\endgroup$
    – Enrico M.
    Feb 13 at 16:03

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.