1

Code:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{changepage}
\usepackage[margin=1in]{geometry} 
\usepackage{float}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.markings}
\tikzset{arrow marks/.style={postaction=decorate,decoration={markings,
            mark=between positions #1 and 1 step #1 with {\arrow{>}}}},
    arrow marks/.default=10pt}
\begin{document}
    \begin{figure}[H]
        \begin{adjustwidth}{-0.7in}{-0.7in}
            \centering
            \begin{tikzpicture}
            [declare function={a=0.5;lambda=5;}]

            \begin{axis}[
            %xtick distance = {1},
            %ytick distance = {1},
            xmin=-12,xmax=12,
            ymin=-8,ymax=8,
            height = 7in,width=1.2\textwidth,
            axis lines=center,
            axis line style=->, xlabel = {$x_1$}, ylabel={$x_2$},
            %axis equal,
            legend cell align = {left},
            every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
            every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,}, 
            title= {Bang-off-bang Control Trajectories},         title style={xshift=0, yshift=2em},
            domain=-15:15,samples=300,legend pos=outer north east]
            %Final Switch Curve x_2 < 0
            \addplot[->,>=latex,arrow marks=1cm,color = blue, thick, domain = -8:0,tips=proper]({-ln(1-a*x)/a^2 - x/a}, {x}) node[below left, pos = 0.3, font = \small] {\(u^* = 1\)};


            %Final Switch Curve x_2 > 0
            \addplot[->,>=latex,arrow marks=1cm,color = red, thick, domain = 8:0,tips=proper]({ln(1+a*x)/a^2 - x/a}, {x}) node[above right, pos = 0.3, font = \small] {\(u^* = -1\)};

            %Off Curve x_2 > 0
            \addplot[dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(lambda*a*x)/(lambda + 2*a*x))/a^2 - x/a}, {x}) node[below left, pos = 0.25, font = \small] {\(u^* = 0\)};

            %Off curve x_2 < 0
            \addplot[dotted, color = black, thick, domain = -8:0,tips=proper]({-ln(1-(lambda*a*x)/(lambda - 2*a*x))/a^2 - x/a}, {x}) node[above right, pos = 0.25, font = \small] {\(u^* = 0\)};


            \addplot[name path =FinSwCurveX2Neg,draw=none,domain = -8:0]({-ln(1-a*x)/a^2 - x/a}, {x});
            \addplot[name path =FinSwCurveX2Pos,draw=none, domain = 8:0]({ln(1+a*x)/a^2 - x/a}, {x});
            \addplot[name path=ZeroSwCurveX2Pos,dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(lambda*a*x)/(lambda + 2*a*x))/a^2 - x/a}, {x});
            \addplot[name path = ZeroSwCurveX2Neg,dotted, color = black, thick, domain = -8:0,tips=proper]({-ln(1-(lambda*a*x)/(lambda - 2*a*x))/a^2 - x/a}, {x});
            \addplot[color = orange,fill opacity=0.2]fill between[of= FinSwCurveX2Neg and ZeroSwCurveX2Neg];
            \addplot[color = orange,fill opacity=0.2]fill between[of= FinSwCurveX2Pos and ZeroSwCurveX2Pos];

            \addplot[name path = xAxisNeg, draw = none, domain = -12:0]{0};
            \addplot[name path = xAxisPos, draw = none, domain = 0:12]{0};
            \addplot[name path = yAxisNeg1, draw = none] coordinates{(0, -8) (0, 0)};
            \addplot[name path = yAxisNeg2, draw = none] coordinates{(-12, -8) (-12, 0)};
            \addplot[name path = yAxisPos1, draw = none] coordinates{(0, 0) (0, 8)};
            \addplot[name path = yAxisPos2, draw = none] coordinates{(12, 0) (12, 8)};
            \addplot[name path= yMax,thick, draw = none, domain=ln(1+(lambda*a*8)/(lambda + 2*a*8))/a^2 - 8/a:0] {8};
            \addplot[name path= yMin,thick, draw = none, domain= 0:-ln(1-(lambda*a*8)/(lambda - 2*a*8))/a^2 - 8/a] {-8};
            %yMax and yMin are used so that the second quadrant above s1 and the fourth quadrant below s2 are respectively properly colored
            \addplot[color = blue,fill opacity=0.2]fill between[of= ZeroSwCurveX2Pos and xAxisNeg];
            \addplot[color = blue,fill opacity=0.2]fill between[of= yAxisNeg1 and yAxisNeg2]; %colors the third quadrant
            \addplot[color = blue, fill opacity = 0.2] fill between[
            of = yMin and FinSwCurveX2Neg]; %For some reason, the blue color doesn't clip off at the y-axis, but rather goes past it
            \addplot[color = red,fill opacity=0.2]fill between[of= ZeroSwCurveX2Neg and xAxisPos]; %colors entire third quadrant blue
            \addplot[color = red,fill opacity=0.2]fill between[of= yAxisPos1 and yAxisPos2]; %colors entire first quadrant red
            \addplot[color = red,fill opacity=0.2]fill between[of= yMax and FinSwCurveX2Pos];
            \end{axis}
            \end{tikzpicture}
        \end{adjustwidth} 
    \end{figure}
\end{document}

Output:

enter image description here

What I am trying to do is that area shaded underneath the solid blue curve on the fourth quadrant. However, that shaded area overflows to the third quadrant. I think that the area is shaded from top to bottom. What could be the problem, and how to fix it? Also, is there a way to use the fillbetween command between three paths?

2

You do not need to restrict yourself to the fill between possibility. An arguably more powerful option is to fill between intersection segments. I added a path for the negative y axis and fill

\path [name path=BC,%draw=cyan,thick,->,
            fill = blue, fill opacity = 0.2,
            intersection segments={of=FinSwCurveX2Neg and negative y axis,
            sequence={A0 -- B1}, },] -- cycle;

which yields

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{changepage}
\usepackage[margin=1in]{geometry} 
\usepackage{float}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{decorations.markings}
\tikzset{arrow marks/.style={postaction=decorate,decoration={markings,
            mark=between positions #1 and 1 step #1 with {\arrow{>}}}},
    arrow marks/.default=10pt}
\begin{document}
    \begin{figure}[H]
        \begin{adjustwidth}{-0.7in}{-0.7in}
            \centering
            \begin{tikzpicture}
            [declare function={a=0.5;lambda=5;}]

            \begin{axis}[
            %xtick distance = {1},
            %ytick distance = {1},
            xmin=-12,xmax=12,
            ymin=-8,ymax=8,
            height = 7in,width=1.2\textwidth,
            axis lines=center,
            axis line style=->, xlabel = {$x_1$}, ylabel={$x_2$},
            %axis equal,
            legend cell align = {left},
            every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
            every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,}, 
            title= {Bang-off-bang Control Trajectories},         title style={xshift=0, yshift=2em},
            domain=-15:15,samples=300,legend pos=outer north east]
            %Final Switch Curve x_2 < 0
            \addplot[->,>=latex,arrow marks=1cm,color = blue, thick, domain =
                -8:0,tips=proper,name path=blue]({-ln(1-a*x)/a^2 - x/a}, {x}) node[below left, pos = 0.3, font
                = \small] {\(u^* = 1\)};


            %Final Switch Curve x_2 > 0
            \addplot[->,>=latex,arrow marks=1cm,color = red, thick, domain = 8:0,tips=proper]({ln(1+a*x)/a^2 - x/a}, {x}) node[above right, pos = 0.3, font = \small] {\(u^* = -1\)};

            %Off Curve x_2 > 0
            \addplot[dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(lambda*a*x)/(lambda + 2*a*x))/a^2 - x/a}, {x}) node[below left, pos = 0.25, font = \small] {\(u^* = 0\)};

            %Off curve x_2 < 0
            \addplot[dotted, color = black, thick, domain = -8:0,tips=proper]({-ln(1-(lambda*a*x)/(lambda - 2*a*x))/a^2 - x/a}, {x}) node[above right, pos = 0.25, font = \small] {\(u^* = 0\)};


            \addplot[name path =FinSwCurveX2Neg,draw=none,domain =
            -8:0,draw=none]({-ln(1-a*x)/a^2 - x/a}, {x});
            \path[name path=negative y axis] (0,0) -- (0,-8);
            \addplot[name path =FinSwCurveX2Pos,draw=none, domain = 8:0]({ln(1+a*x)/a^2 - x/a}, {x});
            \addplot[name path=ZeroSwCurveX2Pos,dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(lambda*a*x)/(lambda + 2*a*x))/a^2 - x/a}, {x});
            \addplot[name path = ZeroSwCurveX2Neg,dotted, color = black, thick, domain = -8:0,tips=proper]({-ln(1-(lambda*a*x)/(lambda - 2*a*x))/a^2 - x/a}, {x});
            \addplot[color = orange,fill opacity=0.2]fill between[of= FinSwCurveX2Neg and ZeroSwCurveX2Neg];
            \addplot[color = orange,fill opacity=0.2]fill between[of= FinSwCurveX2Pos and ZeroSwCurveX2Pos];

            \addplot[name path = xAxisNeg, draw = none, domain = -12:0]{0};
            \addplot[name path = xAxisPos, draw = none, domain = 0:12]{0};
            \addplot[name path = yAxisNeg1, draw = none] coordinates{(0, -8) (0, 0)};
            \addplot[name path = yAxisNeg2, draw = none] coordinates{(-12, -8) (-12, 0)};
            \addplot[name path = yAxisPos1, draw = none] coordinates{(0, 0) (0, 8)};
            \addplot[name path = yAxisPos2, draw = none] coordinates{(12, 0) (12, 8)};
            \addplot[name path= yMax,thick, draw = none, domain=ln(1+(lambda*a*8)/(lambda + 2*a*8))/a^2 - 8/a:0] {8};
            \addplot[name path= yMin,thick, draw = none, domain= 0:-ln(1-(lambda*a*8)/(lambda - 2*a*8))/a^2 - 8/a] {-8};
            %yMax and yMin are used so that the second quadrant above s1 and the fourth quadrant below s2 are respectively properly colored
            \addplot[color = blue,fill opacity=0.2]fill between[of= ZeroSwCurveX2Pos and xAxisNeg];
            \addplot[color = blue,fill opacity=0.2]fill between[of= yAxisNeg1 and yAxisNeg2]; %colors the third quadrant
%             \addplot[color = blue, fill opacity = 0.2] fill between[
%             of = yMin and FinSwCurveX2Neg]; %For some reason, the blue color doesn't clip off at the y-axis, but rather goes past it
            %
            \path [name path=BC,%draw=cyan,thick,->,
                fill = blue, fill opacity = 0.2,
                intersection segments={of=FinSwCurveX2Neg and negative y axis,
                sequence={A0 -- B1}, },] -- cycle;
            %
            \addplot[color = red,fill opacity=0.2]fill between[of= ZeroSwCurveX2Neg and xAxisPos]; %colors entire third quadrant blue
            \addplot[color = red,fill opacity=0.2]fill between[of= yAxisPos1 and yAxisPos2]; %colors entire first quadrant red
            \addplot[color = red,fill opacity=0.2]fill between[of= yMax and FinSwCurveX2Pos];
            \end{axis}
            \end{tikzpicture}
        \end{adjustwidth} 
    \end{figure}
\end{document}

enter image description here

Similar comments apply to your other shaded regions but there you seem to get what you want.

16
  • 1
    Cool! In fact, I found an ad hoc way which is to color the third quadrant white then recolor it with axis on top option. I feel that your method is more direct and concise. I was looking for a direct and concise solution. Thank you so much!
    – Superman
    May 1 '20 at 6:17
  • I looked more into the code, what does sequence={A0 -- B1} do? I have seen on this link (tex.stackexchange.com/questions/417970/…), that there are different versions like sequence={A1[reverse] -- B1}and sequence={A1 -- B1}. How to the numbers affect the result, and what does A and B mean? Finally, what does reverse do?
    – Superman
    May 1 '20 at 18:28
  • @Superman The intersecting paths get decomposed into intersection segments, which are labeled A0, A1 ... for the first path and B0, B1, ... for the second. [reverse] means that the corresponding segment is to be reversed, which is important for fills. So sequence={A1[reverse] -- B1} means "run through the second segment of the first path in the opposite direction and then through the second segment of the second path in the original direction of that path".
    – user194703
    May 1 '20 at 18:31
  • 1
    @Superman OK, good, I always confuse them. This raises the question why the examples in the manual use A and B on pages 443, 444, say.
    – user194703
    May 1 '20 at 19:01
  • 1
    @Superman Yes, you need to build up the paths in steps, see e.g. the second code in this answer.
    – user194703
    May 1 '20 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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