How to draw two intersecting graphs?

I want to make the following figure.

My efforts are:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\def\xmin{0}
\def\xmax{10}
\def\ymin{0}
\def\ymax{7}
\draw[style=help lines, ystep=1, xstep=1] ;

\draw (-.25,-.25) node[auto] {0};
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {$x$};
\draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {$f(x)$};

\def\intersectX1{2}
\def\intersectY1{4/5+3}
\def\intersectX2{7}
\def\intersectY1{9/5+3}
\def\intersectX{4.76}
\def\intersectY{4.26}
\def\QPX{4}
\def\QPY{5}
\draw[color=red,smooth] plot [domain=0:8] (\x,{((\x-4)^2)/5+3)});

\draw[dashed]  (2,0) node[below] {$x_1$} -- (2,3.8) node[up,left] {$f(x_1)$};
\draw[dashed]  (7,0) node[below] {$x_1$} -- (7,4.8) node[up,right] {$f(x_2)$};

\draw[dashed,orange]  (4,0) node[below] {$\alpha\,x_1+(1-\alpha)x_2$} -- (4,3) node[below] {$f(\alpha\,x_1+(1-\alpha)x_2)$};
\draw[color=black,dashedd] (4,3) -- (4,4.2) node[up] {$\alpha f(x_1) + (1-\alpha)f(x_2)$};
\draw[color=blue] (2,3.8) -- (7,4.8);
\end{tikzpicture}
\end{document}

-
Can you a) insert a picture of your current output and b) specify verbally what it is that you’re struggling with so that other users with the same problem can find this question and benefit from it as well? –  doncherry Feb 16 '13 at 17:03
Also, please check the code you upload: This example doesn't compile, because of a typo (dashedd) and a missing style (up). –  Jake Feb 16 '13 at 17:18
Why did you use the pronoun "We and our" rather than "I and my"? –  please stop stalking Feb 16 '13 at 19:24
You asked for a solution using pgfplots, but your attempt doesn't use it. Did you just want a tikz solution? –  Matthew Leingang Feb 17 '13 at 4:01

Just for fun with PSTricks. I made it before someone edited the question title.

\documentclass[pstricks,border={2pt 2pt 14pt 14pt}]{standalone}
\usepackage{pst-eucl}
\usepackage{pst-plot}

\def\f(#1){#1 2 sub 2 exp 2 mul 9 div 1 add}

\begin{document}
\begin{pspicture}(-3,-1)(6,4)
\psaxes[labels=none,ticks=none]{->}(0,0)(-3,-1)(6,4)[$x$,0][$y$,90]
\psplot{0.5}{5.5}{\f(x)}
\psplot[linecolor=magenta]{0.5}{5.5}{\g(x)}
\rput(!5.5 {\f(5.5) 0.25 add}/Y ED Y){$f(x)$}
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\f(x)}{\g(x)}{0.5}{A}
\pstInterFF{\f(x)}{\g(x)}{5.5}{B}
\pstGeonode
(!2 {\g(2)}/Y ED Y){C}
(!2 {\f(2)}/Y ED Y){D}
(0,0|A){Ay}
(A|0,0){Ax}
(0,0|B){By}
(B|0,0){Bx}
(0,0|C){Cy}
(C|0,0){Cx}
(0,0|D){Dy}
(D|0,0){Dx}
\psset{linestyle=dashed,dash=2pt 1pt,linecolor=gray,linewidth=0.5\pslinewidth}
\psline(Ax)(A)(Ay)
\psline(Bx)(B)(By)
\psline(Cx)(C)(Cy)
\psline(Dx)(D)(Dy)
\uput[-90](Ax){\tiny$x_1$}
\uput[-90](Bx){\tiny$x_2$}
\uput[-90](Cx){\tiny$tx_1+(1-t)x_2$}
\uput[180](Cy){\tiny$tf(x_1)+(1-t)f(x_2)$}
\uput[180](Dy){\tiny$f(tx_1+(1-t)x_2)$}
\end{pspicture}
\end{document}


Remarks

• \usepackage{pst-eucl} for (among others) \pstGeonode and \pstInterFF.
• \usepackage{pst-plot} for (among others) \psaxes and \psplot.
• \def\f(#1){#1 2 sub 2 exp 2 mul 9 div 1 add} is the quadratic function in RPN (Reverse Polish Notation). It equals to \f(#1)=2/9 * (#1-2)^2 + 1 in infix notation.
• \def\g(#1){#1 2.5 div 1 add} is the line function in RPN. It equals to \g(#1)=#1/2.5 + 1 in infix notation.
• \begin{pspicture}(-3,-1)(6,4)<drawing macros>\end{pspicture} defines the size of drawing area (canvas).
• \psaxes[labels=none,ticks=none]{->}(0,0)(-3,-1)(6,4)[$x$,0][$y$,90] draws Cartesian coordinate axes with x and y labels but without numerical labels and their tick marks.

-> represents there is one arrow head for each axis. The arrow head is at the right end for the horizontal axis and at the top end for the vertical axis. If we use <-, we obtain the opposite position; the arrow head is at the left end for the horizontal axis and at the bottom for the vertical axis. Using <-> makes all ends have arrow heads. There are many options to change the shape of the arrow head, please consult the PSTricks documentation for the details.

• \psplot{0.5}{5.5}{\f(x)} plots \f(x) from 0.5 to 5.5.

• \psplot[linecolor=magenta]{0.5}{5.5}{\g(x)} plots \g(x) from 0.5 to 5.5 and change the color line to magenta.
• \rput(!5.5 {\f(5.5) 0.25 add}/Y ED Y){$f(x)$} puts $f(x)$ label at the point (5.5,f(5.5)+0.25).

(!5.5 {\f(5.5) 0.25 add}/Y ED Y) is a point in RPN. It equals to (5.5,f(5.5)+0.25). The RPN version seems to be cryptic but you will get used to after doing a bit of exercises. <operand> /Y ED Y is the short notation for <operand> /Y exch def Y (of course, there is no significant number of keystrokes we can save).

• \psset{PointName=none,PointSymbol=none} set options that affect the following macros.

• \pstInterFF{\f(x)}{\g(x)}{0.5}{A} searches for the nearest intersection point between \f(x) and \g(x) around x=0.5. The intersection point will be named as A.
• \pstInterFF{\f(x)}{\g(x)}{5.5}{B} searches for the nearest intersection point between \f(x) and \g(x) around x=5.5. The intersection point will be named as B.

• The following

\pstGeonode
(!2 {\g(2)}/Y ED Y){C}
(!2 {\f(2)}/Y ED Y){D}
(0,0|A){Ay}
(A|0,0){Ax}
(0,0|B){By}
(B|0,0){Bx}
(0,0|C){Cy}
(C|0,0){Cx}
(0,0|D){Dy}
(D|0,0){Dx}


is used to define

• point C at (2,g(2))

• point D at (2,f(2))

• point Py at (0,<ordinate of P>) where point P should be read as any point.

• point Px at (<abscissa of P>,0) where point P should be read as any point.

The theory you need to know is (<coordinate 1>|<coordinate 2>) represents a new coordinate that equals to (<abscissa of coordinate 1>, <ordinate of coordinate 2>). Please consult the PSTricks documentation for the details.

• \psline(Px)(P)(Py) draws a series of lines starting from point Px and stopping at point Py.

• \uput[<angle>](<point>){<contents>} puts <contents> in the direction of <angle> from the point (<point>).

If you have any question, please make a comment. And make sure you compile the code with either latex->dvips->ps2pdf sequence or a single xelatex.

Update 1

There are some minor changes I made.

• Scaling math font globally and using the default text font size (rather than \tiny) for all labels.

\usepackage{lmodern}
\DeclareMathSizes{10}{8}{5}{4}

• Removing 2 unnecessary horizontal dashed lines.

• Aligning labels on the horizontal axis by using \rput instead of \uput.

\documentclass[pstricks,border={2pt 2pt 2pt 4pt}]{standalone}
\usepackage{pst-eucl}
\usepackage{pst-plot}

\usepackage{lmodern}
\DeclareMathSizes{10}{8}{5}{4}

\def\f(#1){#1 2 sub 2 exp 2 mul 9 div 1 add}

\begin{document}
\begin{pspicture}(-3,-0.5)(6,4)
\psaxes[labels=none,ticks=none,arrowinset=0]{->}(0,0)(-3,-0.5)(6,4)%[$x$,0][$y$,90]
\psplot{0.5}{5.5}{\f(x)}
\psplot[linecolor=magenta]{0.5}{5.5}{\g(x)}
\rput(!5.5 {\f(5.5) 0.2 add}/Y ED Y){$f(x)$}
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\f(x)}{\g(x)}{0.5}{A}
\pstInterFF{\f(x)}{\g(x)}{5.5}{B}
\pstGeonode
(!2.2 {\g(2.2)}/Y ED Y){C}
(!2.2 {\f(2.2)}/Y ED Y){D}
%(0,0|A){Ay}
(A|0,0){Ax}
%(0,0|B){By}
(B|0,0){Bx}
(0,0|C){Cy}
(C|0,0){Cx}
(0,0|D){Dy}
(D|0,0){Dx}
(0,-0.25){XLabel}
\psset{linestyle=dashed,dash=2pt 1pt,linecolor=gray,linewidth=0.5\pslinewidth}
\psline(Ax)(A)%(Ay)
\psline(Bx)(B)%(By)
\psline(Cx)(C)(Cy)
\psline(Dx)(D)(Dy)
\rput(Ax|XLabel){$x_1$}
\rput(Bx|XLabel){$x_2$}
\rput(Cx|XLabel){$tx_1+(1-t)x_2$}
\uput[180](Cy){$tf(x_1)+(1-t)f(x_2)$}
\uput[180](Dy){$f(tx_1+(1-t)x_2)$}
\end{pspicture}
\end{document}


Latest update

Infix notation used for the sake of your convenience.

\documentclass[pstricks,border={2pt 2pt 2pt 4pt}]{standalone}
\usepackage{pst-eucl}
\usepackage{pst-plot}

\usepackage{lmodern}
\DeclareMathSizes{10}{8}{5}{4}

\def\f(#1){(2*(#1-2)^2/9+1)}
\def\g(#1){((#1)/2.5+1)}

\pstVerb{/I2P {exec AlgParser cvx exec} def}

\begin{document}
\begin{pspicture}(-3,-0.5)(6,4)
\psaxes[labels=none,ticks=none,arrowinset=0]{->}(0,0)(-3,-0.5)(6,4)%[$x$,0][$y$,90]
\psplot{0.5}{5.5}{{\f(x)} I2P}
\psplot[linecolor=magenta]{0.5}{5.5}{{\g(x)} I2P}
\rput(*5.5 {(\f(x)+0.2)}){$f(x)$}
\psset{PointName=none,PointSymbol=none}
\pstInterFF{{\f(x)} I2P}{{\g(x)} I2P}{0.5}{A}
\pstInterFF{{\f(x)} I2P}{{\g(x)} I2P}{5.5}{B}
\pstGeonode
(*2.2 {\g(x)}){C}
(*2.2 {\f(x)}){D}
%(0,0|A){Ay}
(A|0,0){Ax}
%(0,0|B){By}
(B|0,0){Bx}
(0,0|C){Cy}
(C|0,0){Cx}
(0,0|D){Dy}
(D|0,0){Dx}
(0,-0.25){XLabel}
\psset{linestyle=dashed,dash=2pt 1pt,linecolor=gray,linewidth=0.5\pslinewidth}
\psline(Ax)(A)%(Ay)
\psline(Bx)(B)%(By)
\psline(Cx)(C)(Cy)
\psline(Dx)(D)(Dy)
\rput(Ax|XLabel){$x_1$}
\rput(Bx|XLabel){$x_2$}
\rput(Cx|XLabel){$tx_1+(1-t)x_2$}
\uput[180](Cy){$tf(x_1)+(1-t)f(x_2)$}
\uput[180](Dy){$f(tx_1+(1-t)x_2)$}
\end{pspicture}
\end{document}

-

Instead of computing the coordinates of the intersections you could use tikz's intersections library:

Notes:

• I do not know what the up style is, so I added

\tikzset{up/.style={above}}


to work around that issue.

• Similarly I assumed that the dashedd was intended to be dashed. So I defined

\tikzset{dashedd/.style={dashed}}


If it was a typo, I would recommend correcting the spelling of dashedd. If you have dashedd defined elsewhere simply remove the above mentioned \tikzset line.

Code:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{pgfplots}

\tikzset{up/.style={above}}
\tikzset{dashedd/.style={dashed}}

\begin{document}
\begin{tikzpicture}
\def\xmin{0}
\def\xmax{10}
\def\ymin{0}
\def\ymax{7}
\draw[style=help lines, ystep=1, xstep=1] ;

\draw (-.25,-.25) node[auto] {0};
\draw[->] (\xmin,\ymin) -- (\xmax,\ymin) node[right] {$x$};
\draw[->] (\xmin,\ymin) -- (\xmin,\ymax) node[above] {$f(x)$};

\def\intersectX1{2}
\def\intersectY1{4/5+3}
\def\intersectX2{7}
\def\intersectY1{9/5+3}
\def\intersectX{4.76}
\def\intersectY{4.26}
\def\QPX{4}
\def\QPY{5}
\draw[color=red,smooth, ultra thick, name path=My Function] plot [domain=0:8] (\x,{((\x-4)^2)/5+3)});

\draw[dashed, name path=x1]
(2,0) node[below] {$x_1$}
-- (2,3.8) node[up,left, xshift=-0.2cm, yshift=0.1cm] {$f(x_1)$};

\draw[dashed]
(7,0) node[below] {$x_2$}% <--- corrected
-- (7,4.8) node[up,right, yshift=-0.15cm] {$f(x_2)$};

\draw[dashed,orange, name path= Orange Line]
(4,0) node[below] {$\alpha\,x_1+(1-\alpha)x_2$}
-- (4,3) node[below] {$f(\alpha\,x_1+(1-\alpha)x_2)$};

\draw[color=black,dashedd, name path=Dashed Vertical Line] (4,3)
-- (4,4.2) node[up] {$\alpha f(x_1) + (1-\alpha)f(x_2)$};
\draw[color=magenta, thick, shorten <= -1.0cm, shorten >= -1.0cm, name path=Diagonal Line] (2,3.8) -- (7,4.8);

%% Determine intersection of function and the dashed lines
\draw [dashed, blue, name intersections={of=My Function and Orange Line}]
(intersection-1) -| (0,0) node [left] {$f(tx_1 + (1-t)x_2)$};
\draw [dashed, blue]
(4,4.2) -- (0,4.2) node [left] {$tf(x_1) + (1-t) f(x_2)$};
\end{tikzpicture}
\end{document}

-

Needs latest pst-eucl from CTAN or http://texnik.dante.de/tex/generic/pst-eucl/

\documentclass[pstricks,border={2pt 2pt 2pt 4pt}]{standalone}
\usepackage{pst-eucl}
\usepackage{pst-plot}

\def\f{2*(x-2)^2/9+1}
\def\g{x/2.5+1}

\begin{document}\footnotesize
\begin{pspicture}[algebraic](-3,-0.5)(6,4)
\psaxes[labels=none,ticks=none]{->}(0,0)(-3,-0.5)(6,4)%[$x$,0][$y$,90]
\psplot{0.5}{5.5}{\f}
\psplot[linecolor=magenta]{0.5}{5.5}{\g}
\rput(*5.5 {\f+0.2}){$f(x)$}
\psset{PointName=none,PointSymbol=none}
\pstInterFF{\f}{\g}{0.5}{A}
\pstInterFF{\f}{\g}{5.5}{B}
\psset{linestyle=dashed,dash=2pt 1pt,linecolor=gray,linewidth=0.5\pslinewidth,dotstyle=o}
\pnodes(*2.2 {\g}){C}(*2.2 {\f}){D}
\psCoordinates(A) \psCoordinates(B) \psCoordinates(C) \psCoordinates(D)
\uput[-90](A | 0,0){$x_1\mathstrut$}
\uput[-90](B | 0,0){$x_2\mathstrut$}
\uput[-90](2.2,0){$tx_1+(1-t)x_2\mathstrut$}
\uput[180](0,0 | C){$tf(x_1)+(1-t)f(x_2)$}
\uput[180](0,0 | D){$f(tx_1+(1-t)x_2)$}
\end{pspicture}
\end{document}


-
Is it possible to remove the dot in \psCoordinates? I actually use dotscale=0.01 that seems too funny. –  please stop stalking Jul 26 '14 at 17:39