3

I want to draw this figure:enter image description here

I write a code as:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
    ‎\draw [<->] (0,2) node (yaxis) [above] {$y$}‎ ‎|‎- ‎(3,0) node (xaxis) [right] {$x$};‎
     ‎\node[‎‎scale=0.4]  at (0‎‎‎‎.3,-0.‎08‎‎‎‎‎) {‎$‎a$‎};‎‎
    \node[‎‎scale=0.4]  at (1‎‎‎‎.3,-0.‎1‎‎‎‎‎) {‎$\frac{‎a+b}{2}$‎};‎‎
    \node[‎‎scale=0.4]  at (2‎‎‎‎.3,-0.‎08‎‎‎‎‎) {‎$‎b$‎};‎‎
    \path[purple,rotate=360,draw,fill=gray!50] (2.3,1.2) parabola (0.3,0.5);
    \draw [black] (0.3,0.5) to [out=20,in=-160] (1.3,1.03) to [out=20,in=160] (2.3,1.2);
   ‎\node[‎‎scale=0.3]  at (0‎‎‎‎.65,0.‎82‎‎‎‎‎) {\rotatebox{30}{‎$‎y=p_2(x)‎$‎}};‎‎
     \draw[] (1.3,0) to (1.3,1.03);
      \draw[] (0.3,0) to (0.3,0.5);
      \draw[] (2.3,0) to (2.3,1.2);
     ‎\node[‎‎scale=0.3]  at (2,1.32) {\rotatebox{4}{‎$‎y=f(x)‎$‎}};‎‎
\end{tikzpicture}
\end{document}

the output is: enter image description here How I can correct it? Also, $x$ and $y$ are too big.

  • 7
    How does this question differ from your other question How can draw this figures single or beside other? – Dai Bowen Nov 26 '16 at 14:49
  • 2
    What makes you think that you will get answers here that you wouldn't also get for your other question? – gernot says Reinstate Monica Nov 26 '16 at 14:59
  • 2
    @Nima that's because you're question is entirely a "do it for me" question posting a picture and asking somebody to recreate that for you in TikZ from scratch. Speaking personally if you post a picture like this then unless I find it very interesting I'm not going to put the effort in to create the entire diagram for you. If you post imperfect code that's a different story - then I can see you've tried (and thus that I might actually be able to teach something by providing an answer) and it's less effort for me to be helpful to correct things. – Dai Bowen Nov 26 '16 at 15:00
  • 1
    Take a look at this answer which covers similar issues, take a look at Min­i­mal in­tro­duc­tion to TikZ, perhaps this TUGboat article, and search the pgf documentation and add an attempt of your own and you're much more likely to get attempts to assist you. – Dai Bowen Nov 26 '16 at 15:03
  • 1
    Thank you for showing us what you've gotten so far. But it looks like you have the right figure pretty well figured out. What is your question? Are you trying to do the left figure now? What is your attempt at that? Or are you trying to adjust something that's not quite right about the figure you do have? What is it that's wrong? – Teepeemm Nov 26 '16 at 20:25
3

You can use the intersections to fix the crossing and also fillbetween library for the gray area. Though, since you are using pgfplots, it would have been quite easier if you had closed form formulas for the curves such that we wouldn't have to find the mid point manually.

\documentclass[tikz]{standalone}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=center, ytick=\empty, 
ymax=1.3, ymin=0, xmax=2.6,xmin=0,
xtick={0.3,1.3,2.3}, xticklabels={$a$,$\frac{a+b}{2}$,$b$},
axis line style={-}, xlabel=$x$, ylabel=$y$]
\draw[name path=A, purple] (2.3,1.2) parabola (0.3,0.5);
\node[] at (1.6,1.2){$y=f(x)$};
\path[name path=B] (\pgfkeysvalueof{/pgfplots/xmin},0) 
                   --(\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot[gray!50] fill between[of=A and B,soft clip={domain=0.3:2.3}];
\draw[name path=C] (1.3,0) -- (1.3,1.025);
\path[name intersections={of=A and C}] coordinate (midpoint) at (intersection-1);
\draw (0.3,0.5) ..controls ++(0.1,0.3) and ++(-0.2,-0.05) .. (midpoint)
                node[pos=0.5,above left]{$y=p_2(x)$} 
                ..controls ++(0.2,0.05) and ++(-0.1,-0.05) .. (2.3,1.2);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.