23

I would like to plot these four equations :

$$y=\dfrac{3}{2x+1}, y=3x-2, x=2   y=0$$

and i don't know how to draw vertical line of equation x=2 and shade a region like the picture below

2 4

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pst-plot}
\usepackage{pstricks}

\begin{document}

\begin{table}[h]
\centering
    \begin{tikzpicture}
        \begin{axis}[
             extra y ticks       = 2,
      extra y tick labels = z,
      extra y tick style  = { grid = major },
             axis lines=middle,
             grid=major,
              xmin=-1,
                xmax=3,
        ymin=-1,
        ymax=3,
        xlabel=$x$,
        ylabel=$y$,
                xtick={-1,-0.5,0,...,3},
        ytick={-1,-0.5,0,...,3},
        legend style={at={(axis cs:-.5,0,1)}, anchor=north west,width=90cm, draw=none,opacity=.9, inner sep=2pt,fill=gray!10}]
            %\addlegendentry{$f(x)=2^{x}$}
            %\addlegendentry{$f(x)={\left(\dfrac{1}{2}\right)}^x$}
            smooth,
                enlargelimits=false,
        axis line style={latex-latex},
 \addplot[thick, samples=100, domain=0:6,blue] {3/(2*x+1)}; %node[right] {$f(x)=2^{x}$};
\addplot[thick, samples=50, smooth,domain=0:6,olive] {3*x-2}; % node[right]{$f(x)={\dfrac{1}{2}}^x$};
    \end{axis}
\end{tikzpicture}
\end{table}

\end{document}

which produces :

3

Educ
  • 4,362

2 Answers2

27

I reduced your code a bit, but this is what you need:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[h]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        xmin=-1, xmax=3,
        ymin=-1, ymax=3,
        axis lines=middle,
      ]  
      \addplot[samples=100, domain=0:3, name path=A] {3/(2*x+1)}; 
      \addplot[samples=50, domain=0:3,name path=B] {3*x-2}; 
      \path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
       \addplot[gray, pattern=north west lines] fill between[of=B and xaxis, soft clip={domain=0.66666:1}];
      \addplot[gray, pattern=north west lines] fill between[of=A and xaxis, soft clip={domain=1:2}];
      \addplot +[mark=none] coordinates {(2, -1) (2, 3)};
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

The result is:

result

MaxNoe
  • 6,136
14

Here is a over kill solution for you.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\pgfplotsset{compat=1.12}

\begin{document}

\begin{table}[h]
\centering
    \begin{tikzpicture}
        \begin{axis}[
             extra y ticks       = 2,
        extra y tick labels = z,
       extra y tick style  = { grid = major },
        axis lines=middle,
        grid=major,
        xmin=-1,
        xmax=3,
        ymin=-1,
        ymax=3,
        xlabel=$x$,
        ylabel=$y$,
        xtick={-1,-0.5,0,...,3},
        ytick={-1,-0.5,0,...,3},
        legend style={at={(axis cs:-.5,0,1)}, anchor=north west,width=90cm, draw=none,opacity=.9, inner sep=2pt,fill=gray!10}]
            %\addlegendentry{$f(x)=2^{x}$}
            %\addlegendentry{$f(x)={\left(\dfrac{1}{2}\right)}^x$}
            smooth,
                enlargelimits=false,
        axis line style={latex-latex},
 \addplot[thick, samples=100, domain=0:6,blue,name path=one] {3/(2*x+1)}; %node[right] {$f(x)=2^{x}$};
\addplot[thick, samples=50, smooth,domain=0:6,olive,name path=two] {3*x-2}; % node[right]{$f(x)={\dfrac{1}{2}}^x$};
\addplot[thick, samples=50, smooth,domain=0:6,magenta, name path=three] coordinates {(2,-1)(2,3)};
\path[name path=axis] (0,0) -- (3,0);
\path[name path=lower,
%draw=red,ultra thick,
intersection segments={of=one and two,sequence=R1 -- L2}
];
\addplot
fill between[
of=axis and lower,
soft clip={
domain=0:2
},
split,
every segment no 0/.style={fill=none},
every segment no 1/.style={pattern=grid,pattern color=orange},
];
    \end{axis}
\end{tikzpicture}
\end{table}

\end{document}

enter image description here

  • Please verify ur code it's not working for me by the way i'm using ttexniccenter under windows – Educ Apr 26 '15 at 18:04
  • @Educ Not working is not enough information as this code compiles for me. Do you have an up to date tex distribution? What is the error you get? –  Apr 26 '15 at 23:26
  • before i make update and tell you what i my version, there is 56 errors for example latex Error: \begin{tikzpicture} on input line 12ended by \end{document} – Educ Apr 27 '15 at 02:50
  • @Educ: You did something extra then. I don't get any error. –  Apr 27 '15 at 02:52
  • i got another error " you can't use '\end' in internal vertical mode" – Educ Apr 27 '15 at 02:57
  • when i use sharelatex i got no error – Educ Apr 27 '15 at 03:07