在一些查询中,我看到有人问如何绘制线性不等式(即如何根据不等式用颜色填充区域)。抱歉,我的代码太长了:
\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}
\def\a{0.5}
\def\lambda{5}
\def\xTwoInitPosToZeroOne{1.5}
\def\xOneInitPosToZeroOne{ln(1 +(\lambda*\a* \xTwoInitPosToZeroOne)/(\lambda + 2*\a*\xTwoInitPosToZeroOne))/(\a^2) - \xTwoInitPosToZeroOne/\a}
\def\zeroControlPosToNegOne{ln((2*\a* \xTwoInitPosToZeroOne + \lambda)/\lambda)/\a}
\def\xTwoInitPosToZeroTwo{2}
\def\xOneInitPosToZeroTwo{ln(1 +(\lambda*\a* \xTwoInitPosToZeroTwo)/(\lambda + 2*\a*\xTwoInitPosToZeroTwo))/(\a^2) - \xTwoInitPosToZeroTwo/\a}
\def\zeroControlPosToNegTwo{ln((2*\a* \xTwoInitPosToZeroTwo + \lambda)/\lambda)/\a}
\def\xTwoInitPosToZeroThree{3}
\def\xOneInitPosToZeroThree{ln(1 +(\lambda*\a* \xTwoInitPosToZeroThree)/(\lambda + 2*\a*\xTwoInitPosToZeroThree))/(\a^2) - \xTwoInitPosToZeroThree/\a}
\def\zeroControlPosToNegThree{ln((2*\a* \xTwoInitPosToZeroThree + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroOne{-1.5}
\def\xOneInitNegToZeroOne{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroOne)/(\lambda - 2*\a*\xTwoInitNegToZeroOne))/(\a^2) - \xTwoInitNegToZeroOne/\a}
\def\zeroControlNegToPosOne{ln((-2*\a* \xTwoInitNegToZeroOne + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroTwo{-2}
\def\xOneInitNegToZeroTwo{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroTwo)/(\lambda - 2*\a*\xTwoInitNegToZeroTwo))/(\a^2) - \xTwoInitNegToZeroTwo/\a}
\def\zeroControlNegToPosTwo{ln((-2*\a* \xTwoInitNegToZeroTwo + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroThree{-3}
\def\xOneInitNegToZeroThree{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroThree)/(\lambda - 2*\a*\xTwoInitNegToZeroThree))/(\a^2) - \xTwoInitNegToZeroThree/\a}
\def\zeroControlNegToPosThree{ln((-2*\a* \xTwoInitNegToZeroThree + \lambda)/\lambda)/\a}
\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]
\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\)};
\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\)};
\addplot[dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(\lambda*\a*x)/(\lambda + 2*\a*x))/\a^2 - x/\a}, {x});
\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.3, font = \small] {\(u^* = 0\)};
%%Starting from here, I am not sure if this is necessary or not
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroOne/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {1/\a + (\xTwoInitPosToZeroOne - 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=red, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegOne] ({(\xTwoInitPosToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {\xTwoInitPosToZeroOne*exp(-\a*x)});%1 Pos
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroTwo/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {1/\a + (\xTwoInitPosToZeroTwo - 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=red, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegTwo] ({(\xTwoInitPosToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {\xTwoInitPosToZeroTwo*exp(-\a*x)});%2 Pos
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroThree/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroThree}, {1/\a + (\xTwoInitPosToZeroThree - 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=red, arrow marks=1cm, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegThree] ({(\xTwoInitPosToZeroThree/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroThree}, {\xTwoInitPosToZeroThree*exp(-\a*x)});%3 Pos
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroOne/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {-1/\a + (\xTwoInitNegToZeroOne + 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=blue, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosOne] ({(\xTwoInitNegToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {\xTwoInitNegToZeroOne*exp(-\a*x)});%1 Neg
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroTwo/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {-1/\a + (\xTwoInitNegToZeroTwo + 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=blue, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosTwo] ({(\xTwoInitNegToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {\xTwoInitNegToZeroTwo*exp(-\a*x)});%2 Neg
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroThree/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroThree}, {-1/\a + (\xTwoInitNegToZeroThree + 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=blue, arrow marks = 1cm, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosThree] ({(\xTwoInitNegToZeroThree/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroThree}, {\xTwoInitNegToZeroThree*exp(-\a*x)});%3 Neg
\end{axis}
\end{tikzpicture}
\end{adjustwidth}
\caption{Optimal control trajectories for Problem 4 for $a = 0.5$ and $\lambda = 5$. The solid plot is the final switching curve, the dotted plot is the switching curve to ``off" mode, while the dashed plots are the state trajectories that are not originally on the switching curve.}
\end{figure}
\end{document}
我之所以这样写是Starting from here, I am not sure if this is necessary or not
因为我不确定颜色填充是否会填满虚线曲线。这是我的输出(带有“高亮”):
我想要的是,在 LHS(即黑色虚线曲线顶部的左侧和蓝色实线曲线的左侧),我想将该区域涂成红色(或粉红色,具有一定的不透明度),虚线和实线(红色和蓝色)之间的区域涂成橙色(或任何颜色),RHS(即黑色虚线曲线底部的右侧和红色实线的右侧)涂成蓝色(具有一定的不透明度)。有没有办法做到这一点(不覆盖虚线)?
答案1
问题是,当使用时fillbetween
,路径必须命名,但如果这些路径包括装饰风格,它将不起作用,在这种情况下markings
,我不知道为什么,但它不起作用,因为我放置了没有绘图draw=none
和样式标记的副本,以便用这些填充区域生成具有一定不透明度的填充,opacity=0.3
还添加一些点以便填充整个图形(data)--++(15cm,0);
。
梅威瑟:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,patterns,backgrounds}
\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}
\def\a{0.5}
\def\lambda{5}
\def\xTwoInitPosToZeroOne{1.5}
\def\xOneInitPosToZeroOne{ln(1 +(\lambda*\a* \xTwoInitPosToZeroOne)/(\lambda + 2*\a*\xTwoInitPosToZeroOne))/(\a^2) - \xTwoInitPosToZeroOne/\a}
\def\zeroControlPosToNegOne{ln((2*\a* \xTwoInitPosToZeroOne + \lambda)/\lambda)/\a}
\def\xTwoInitPosToZeroTwo{2}
\def\xOneInitPosToZeroTwo{ln(1 +(\lambda*\a* \xTwoInitPosToZeroTwo)/(\lambda + 2*\a*\xTwoInitPosToZeroTwo))/(\a^2) - \xTwoInitPosToZeroTwo/\a}
\def\zeroControlPosToNegTwo{ln((2*\a* \xTwoInitPosToZeroTwo + \lambda)/\lambda)/\a}
\def\xTwoInitPosToZeroThree{3}
\def\xOneInitPosToZeroThree{ln(1 +(\lambda*\a* \xTwoInitPosToZeroThree)/(\lambda + 2*\a*\xTwoInitPosToZeroThree))/(\a^2) - \xTwoInitPosToZeroThree/\a}
\def\zeroControlPosToNegThree{ln((2*\a* \xTwoInitPosToZeroThree + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroOne{-1.5}
\def\xOneInitNegToZeroOne{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroOne)/(\lambda - 2*\a*\xTwoInitNegToZeroOne))/(\a^2) - \xTwoInitNegToZeroOne/\a}
\def\zeroControlNegToPosOne{ln((-2*\a* \xTwoInitNegToZeroOne + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroTwo{-2}
\def\xOneInitNegToZeroTwo{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroTwo)/(\lambda - 2*\a*\xTwoInitNegToZeroTwo))/(\a^2) - \xTwoInitNegToZeroTwo/\a}
\def\zeroControlNegToPosTwo{ln((-2*\a* \xTwoInitNegToZeroTwo + \lambda)/\lambda)/\a}
\def\xTwoInitNegToZeroThree{-3}
\def\xOneInitNegToZeroThree{-ln(1 -(\lambda*\a* \xTwoInitNegToZeroThree)/(\lambda - 2*\a*\xTwoInitNegToZeroThree))/(\a^2) - \xTwoInitNegToZeroThree/\a}
\def\zeroControlNegToPosThree{ln((-2*\a* \xTwoInitNegToZeroThree + \lambda)/\lambda)/\a}
\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
]
%IV Cuadrant blue plot as path A.
\addplot[name path=A,->,draw=none,domain = -8:0]({-ln(1-\a*x)/\a^2 - x/\a}, {x});
\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\)};
%IV Cuadrant dashed black plot as path B.
\addplot[name path=B,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.3, font = \small] {\(u^* = 0\)};
%Fill between A and B
\addplot[orange,fill opacity=0.2]fill between[of=A and B];
%II Cuadrant red plot as path C.
\addplot[name path=C,draw=none, domain = 8:0]({ln(1+\a*x)/\a^2 - x/\a}, {x});
\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\)};
%II Cuadrant dashed black plot as path D.
\addplot[name path=D,dotted, color = black, thick, domain = 8:0,tips=proper]({ln(1+(\lambda*\a*x)/(\lambda + 2*\a*x))/\a^2 - x/\a}, {x});
%Fill between path C and D
\addplot[orange,fill opacity=0.2]fill between[of=C and D];
%%Starting from here, I am not sure if this is necessary or not
%1st Red arrow marks dashed path E
\addplot[name path=E,draw=none,domain=0:-6]
({x/\a + (\xTwoInitPosToZeroOne/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {1/\a + (\xTwoInitPosToZeroOne - 1/\a)*exp(-\a*x)})-- ++(-15cm,0);
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroOne/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {1/\a + (\xTwoInitPosToZeroOne - 1/\a)*exp(-\a*x)});
%complement
\addplot[name path=E2,draw=none,domain=0:\zeroControlPosToNegOne] ({(\xTwoInitPosToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {\xTwoInitPosToZeroOne*exp(-\a*x)});%1 Pos
\addplot[->,>=latex, color=red, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegOne] ({(\xTwoInitPosToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroOne}, {\xTwoInitPosToZeroOne*exp(-\a*x)});%1 Pos
%2nd Red arrow marks dashed path F
\addplot[name path=F,draw=none,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroTwo/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {1/\a + (\xTwoInitPosToZeroTwo - 1/\a)*exp(-\a*x)});
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroTwo/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {1/\a + (\xTwoInitPosToZeroTwo - 1/\a)*exp(-\a*x)});
%Complement
\addplot[name path=F2,draw=none,domain=0:\zeroControlPosToNegTwo] ({(\xTwoInitPosToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {\xTwoInitPosToZeroTwo*exp(-\a*x)});%2 Pos
\addplot[->,>=latex, color=red, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegTwo] ({(\xTwoInitPosToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroTwo}, {\xTwoInitPosToZeroTwo*exp(-\a*x)});%2 Pos
%Fill between path E and F
\addplot[red,fill opacity=0.2]fill between[of=F and E];
\addplot[red,fill opacity=0.2]fill between[of=F2 and E2];
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=red, dashed,thick,domain=-6:0]
({x/\a + (\xTwoInitPosToZeroThree/\a - 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitPosToZeroThree}, {1/\a + (\xTwoInitPosToZeroThree - 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=red, arrow marks=1cm, tips = proper, dashed,thick,domain=0:\zeroControlPosToNegThree] ({(\xTwoInitPosToZeroThree/\a)*(1 - exp(-\a*x)) + \xOneInitPosToZeroThree}, {\xTwoInitPosToZeroThree*exp(-\a*x)});%3 Pos
%Blue path G
\addplot[name path=G,draw=none,domain=0:-6]
({-x/\a + (\xTwoInitNegToZeroOne/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {-1/\a + (\xTwoInitNegToZeroOne + 1/\a)*exp(-\a*x)}) -- ++(15cm,0);
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroOne/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {-1/\a + (\xTwoInitNegToZeroOne + 1/\a)*exp(-\a*x)});
%complement
\addplot[name path=G2,draw=none,domain=0:\zeroControlNegToPosOne] ({(\xTwoInitNegToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {\xTwoInitNegToZeroOne*exp(-\a*x)});%1 Neg
\addplot[->,>=latex, color=blue, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosOne] ({(\xTwoInitNegToZeroOne/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroOne}, {\xTwoInitNegToZeroOne*exp(-\a*x)});%1 Neg
%Blue path H
\addplot[name path=H,draw=none,domain=-6:0]({-x/\a + (\xTwoInitNegToZeroTwo/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {-1/\a + (\xTwoInitNegToZeroTwo + 1/\a)*exp(-\a*x)});
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroTwo/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {-1/\a + (\xTwoInitNegToZeroTwo + 1/\a)*exp(-\a*x)});
%complement
\addplot[name path=H2,draw=none,domain=0:\zeroControlNegToPosTwo] ({(\xTwoInitNegToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {\xTwoInitNegToZeroTwo*exp(-\a*x)});%2 Neg
\addplot[->,>=latex, color=blue, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosTwo] ({(\xTwoInitNegToZeroTwo/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroTwo}, {\xTwoInitNegToZeroTwo*exp(-\a*x)});%2 Neg
%Fill between path G and H
\addplot[blue,fill opacity=0.2]fill between[of=G and H];
\addplot[blue,fill opacity=0.2]fill between[of=G2 and H2];
\addplot[->,>=latex,arrow marks=1cm, tips = proper,
color=blue, dashed,thick,domain=-6:0]
({-x/\a + (\xTwoInitNegToZeroThree/\a + 1/(\a^2))*(1 - exp(-\a*x)) + \xOneInitNegToZeroThree}, {-1/\a + (\xTwoInitNegToZeroThree + 1/\a)*exp(-\a*x)});
\addplot[->,>=latex, color=blue, arrow marks = 1cm, tips = proper, dashed,thick,domain=0:\zeroControlNegToPosThree] ({(\xTwoInitNegToZeroThree/\a)*(1 - exp(-\a*x)) + \xOneInitNegToZeroThree}, {\xTwoInitNegToZeroThree*exp(-\a*x)});%3 Neg
\end{axis}
\end{tikzpicture}
\end{adjustwidth}
\caption{Optimal control trajectories for Problem 4 for $a = 0.5$ and $\lambda = 5$. The solid plot is the final switching curve, the dotted plot is the switching curve to ``off" mode, while the dashed plots are the state trajectories that are not originally on the switching curve.}
\end{figure}
\end{document}