4
$\begingroup$

I need some help to complete this code in order to make the wanted plot. In the following, the function that I define as $m(x) = 1$ should be replaced (if possible) to a function that plots a vertical line (equation: $y = 1$) in the range $[-1, 2]$. Then I would like to fill the region under the curve $y = x$ but limited at the right by $y = 1$.

Here is the code I wrote so far, I am stuck on the vertical line + filling part.

Clear[f, g, h, m, plot, reg];
f[x_] = Sqrt[x];
g[x_] = -Sqrt[x];
h[x_] = x;
m[x_] = 1;
plot = Plot[{f[x], g[x], h[x], m[x]}, {x, -1.5, 2}, 
PlotStyle -> {{Darker@Cyan, Dashed}, {Darker@Cyan, Dashed}, 
Darker@Green, Darker@Red}, AspectRatio -> Automatic];
reg = RegionPlot[{h[x] <= y <= m[x]}, {x, -2, 3}, {y, -2, 2}, 
PlotStyle -> LightRed, BoundaryStyle -> None];
Show[reg, plot, PlotRange -> {-2, 3}, 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}}]}}]

And here is the output:

enter image description here

ADD

Here is the image I would like to replicate (it's from a book).

enter image description here

$\endgroup$
3
  • $\begingroup$ Also, just noticed I switched the inequalities: I meant the region between $y = x$ and $y = 1$ (from the point $(1, 1)$ downwards). $\endgroup$
    – Enrico M.
    Mar 24 at 13:11
  • 1
    $\begingroup$ A hand drawn picture would be more explanatory. $\endgroup$
    – Syed
    Mar 24 at 13:30
  • $\begingroup$ @Syed I added the pic I would like to replicate (it's from a book). The arrow + the gradient is optional $\endgroup$
    – Enrico M.
    Mar 24 at 13:34

3 Answers 3

7
$\begingroup$

My attempt:

Clear["Global`*"];

p1 = Plot[{Sqrt[x], -Sqrt[x], x}, {x, -1, 2}
  , AspectRatio -> Automatic
  , PlotStyle -> {
    {Dashing[{0.05, 0.03}], Black, Thick}
    , {Dashing[{0.05, 0.03}], Black, Thick}
    }
  , Ticks -> {None, None}
  , AxesLabel -> {Style["x", Bold, Italic, 20]
    , Style["y", Bold, Italic, 20]
    }
  , AxesStyle -> Directive @@ {Black, Arrowheads[0.07]}
  , Epilog -> {
    {Dashing[{0.05, 0.03}], Black, Thick
     , InfiniteLine[{{1, -1}, {1, 1}}]}
    , AbsolutePointSize[12], Point@{1, 1}
    , Text[Style["1", Bold, 20], {1.1, -0.15}]
    , {Black, Thick
     , Arrow[AnglePath[{{1, 1}, 116.565 Degree}, {{0.75, 0 Degree}}]]
     , Text[Style["∇ f", 20, Bold], {0.5, 1.8}]
     }
    }
  ];

reg = ImplicitRegion[{y <= x && x <= 1 && y < 1}, {x, y}];

p2 = RegionPlot[reg, PlotStyle -> Opacity[0.4, Gray], 
   BoundaryStyle -> {Thick, Black}
   , PlotRange -> {{-2, 2}, {-2, 2}}
   ];

Show[
 p1, p2
 ]

enter image description here

$\endgroup$
3
  • $\begingroup$ Damn... Call it "attempt"... It's wonderful! o.o $\endgroup$
    – Enrico M.
    Mar 24 at 15:02
  • $\begingroup$ Thanks. For better looking labels you will have to use MateX. $\endgroup$
    – Syed
    Mar 24 at 15:05
  • $\begingroup$ Oh, don't worry about that. My next "challenge" will be to learn to do it in LaTeX/Tikz. For the moment I'm ways more than satisfied, thank you!! $\endgroup$
    – Enrico M.
    Mar 24 at 15:06
5
$\begingroup$
$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

Using Filling

f[x_] = Sqrt[x];
h[x_] = x;
Plot[{f[x], -f[x], h[x],
  ConditionalExpression[h[x], -1 < x < 1]},
 {x, -1.5, 2},
 AspectRatio -> Automatic,
 PlotStyle -> {
   {Darker@Cyan, Dashed},
   {Darker@Cyan, Dashed},
   {Darker@Green, Dashed},
   Darker@Green},
 Filling -> {4 -> -1},
 FillingStyle -> {None, LightRed},
 PlotRange -> {-1.5, 1.5},
 AxesStyle -> Arrowheads[{0.05}],
 GridLines -> {{{-2, {Thick, AbsoluteDashing[{3, 3}]}}}, None},
 Epilog -> {
   {AbsoluteThickness[1.75], Line[{{1, 1}, {1, -1}}]},
   AbsoluteDashing[3, 3],
   Line[{{1, 2}, {1, -1}}],
   Line[{{1, 1}, {0, 1}}]}]

enter image description here

$\endgroup$
1
  • $\begingroup$ Superb. YOu guys are wondrous. $\endgroup$
    – Enrico M.
    Mar 24 at 16:46
4
$\begingroup$
  • Draw a Triangle.
Clear["Global`*"];
f[x_] = Sqrt[x];
g[x_] = -Sqrt[x];
p = {1, f[1]};
q = {1, g[1]};
k = {g[1], g[1]};
plot = Plot[{f[x], g[x]}, {x, -1.5, 2}, 
  PlotStyle -> Directive@{Darker@Cyan, Dashed}, 
  AspectRatio -> Automatic, 
  Epilog -> {AbsoluteThickness[2], Line[{p, q}], Line[{p, k}], Dashed,
     HalfLine[{1, 1}, {0, 1}], HalfLine[{1, 1}, {1, 1}], Gray, 
    Opacity[.5], Triangle[{p, q, k}]}]

enter image description here

$\endgroup$
0

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.