# Can some one help to draw this graph?

I am pretty new to Latex graphics I would like to aks whether the Latex could draw this picture?

The function I used is :

$f(x)=\left\{ \begin{array}{ll} (x-5)^3-3(x-5)^2-18(x-5)+40 & 1\leq x\leq 10, \\ 60-6x &10\leq x\leq 15. \end{array} \right.$


I looked around and it is too complicated for the beginner like me. If it is possible, could some one give some help ?

Thank you so much for your help.

• It definitely can. If you want to draw the graph using an equation, see the pgfplots package. Otherwise, plain Tikz will do. – Alenanno Nov 30 '15 at 15:41
• Do you have a function or the data as a CSV file? If so, I'd hazard a guess and say yes. – Richard Nov 30 '15 at 15:42
• Dear Alenna and Richard: Thank you for your comments, unfortunately I don't have the data. I would like to illustrate an example by picture. It does not have to be exactly like the picture ! – Duy Nguyen Nov 30 '15 at 15:44
• Then you have to give equations for the curves, (the first part looks like a cubic curve), or ask for Bézier curves, given a series of points. You also can easily do such things with pstricks, and more specifically with \pst-plot. – Bernard Nov 30 '15 at 15:50
• I have added the function into the question ! Thank you for your time ! – Duy Nguyen Nov 30 '15 at 18:57

If all you need is to plot the function, then pgfplots is quite easy. You just add \usepackage{pgfplots} to the preamble, and use

\begin{tikzpicture}
\begin{axis}
\end{axis}
\end{tikzpicture}


You can customize this and add annotations directly in the plot, and in general, if you want to plot functions or data, pgfplots is quite nice. For the below code however, I decided to use just plain TikZ. The \foreach is the only thing I think would be a bit more fiddly in an axis environment from pgfplots. Do ask if any of the code is cryptic.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections,arrows.meta}
\begin{document}
% The default unit vectors are 1cm long, without modifying them the plot would be almost 1m high
\begin{tikzpicture}[y=0.07cm,x=0.7cm]
% The syntax of the plot expression is (<x>,<y>).
% Here \x is the variable. We need curly braces around the y-expression in the first case
% to avoid confusing the parser, which looks for a closing paren.
% Note also that multiplication must be given explicitly, so 6*x, not 6x.
\draw [domain=1:10,samples=50] plot (\x,{(\x-5)^3-3*(\x-5)^2-18*(\x-5)+40});
\draw [domain=10:15] plot (\x,60-6*\x);

% name path is defined by the intersections library, it allows one to use
% this name at a later stage for calculating intersections between lines.
\draw [dashed,name path=zeroline] (0,0) -- (15,0) node[inner sep=0pt,minimum size=4pt,circle,fill,label=right:$e_{h,a}$]{};

% The x- and y-value for the maximum was calculated by WolframAlpha
\draw [dashed] (0,57.04) -- (15,57.04);
% In this situation, "latex" is the name of an arrow tip, so "latex-latex" means
% add an arrow tip at both ends of the line
\draw [dashed,latex-latex] (3.354,0) node[below] {$\rho$}-- (3.354,57.04);

% This draws the two middle vertical lines. I used a loop because that avoids repeating the function with two different x-values.
\foreach [count=\c] \x in {9,6}
\draw [dashed,latex-latex,name path=vertical \c] (\x,{(\x-5)^3-3*(\x-5)^2-18*(\x-5)+40}) -- node[right]{$g_{\c}(\bar{x})_t-y_{\c}$} (\x,57.04);

% Draw a tickmark and add the ticklabel below
\draw (6,2pt) -- (6,-2pt) node [below] {$\rho_{g_2, y_2}$};

% This draw the rightmost vertical line. The arrow tips are specified with the syntax
% from the arrows.meta library. Bar[] is the perpendicular line, Latex[] the arrow
% Braces around the arrow tip specification is needed to hide the brackets from the parser.
% pos=0.7 means that the node is placed 70% of the distance along the path,
% instead of the default 50% (i.e. midway).
\draw [dashed,{Bar[]Latex[]}-{Latex[]Bar[]}] (15,-30) -- node[pos=0.7,fill=white]{$h(\bar{x})_t-a$} (15,57.04);

% In hindsight, using the intersections library for this is overkill, as the point
% obviously is placed at (9,0).
% But it's a convenient feature to know about, so I'll leave it.
% I see I also used a different method for creating the black dot and text, though I could
% have used an empty node with a label here as well. Ah well, just shows
% that there are more than one option.
\fill [name intersections={of=zeroline and vertical 1}] (intersection-1) circle[radius=2pt] node [above right] {$\rho_{g_1,y_1}$};

\end{tikzpicture}
\end{document}


Metapost is also a good tool for this type of chart, where you have a mathematical function that you want to annotate. Here's a version of your chart, with some comments.

prologues := 3;
outputtemplate := "%j%c.eps";
beginfig(1);

% define the function
vardef f(expr x) =
if x < 10 : (x-5)**3 - 3((x-5)**2) - 18(x-5) + 40
else:       60-6x
fi
enddef;

% define the function path and an x-axis
path ff, xx; numeric u, s;
u = 7mm;
s = 1/8;
ff = ((1,f(1)) for x=1+s step s until 15: -- (x,f(x)) endfor) xscaled u yscaled 0.1u;
xx = origin -- right scaled 16u;

% find four points along the function path
(x1,y1) = point directiontime right of ff of ff; % the first point at which we are travelling "right"
(x2,y2) = point 5/s of ff;  % two points roughly in the middle
(x3,y3) = point 8/s of ff;
(x4,y4) = point infinity of ff; % the point at the end

% draw the reference lines, and arrows
drawoptions(withcolor .5 white);
draw xx;
draw xx shifted (0,y1);

drawoptions(dashed evenly);
drawdblarrow (x1,0)  -- (x1,y1);
drawdblarrow (x2,y2) -- (x2,y1);
drawdblarrow (x3,y3) -- (x3,y1);
drawdblarrow (x4,y4) -- (x4,y1);

drawoptions();
label.bot(btex $\rho$ etex, (x1,0));
label.bot(btex $\rho_{g_2,y_2}$ etex, (x2,0)); draw (up--down) shifted (x2,0);
dotlabel.urt(btex $\rho_{g_1,y_1}$ etex, (x3,0));
dotlabel.urt(btex $e_{h,a}$ etex, (x4,0));
label.rt(btex $g_1(\bar{x}_t)-y_1$ etex, (x3,(y1+y3)/2));
label.rt(btex $g_2(\bar{x}_t)-y_2$ etex, (x2,(y1+y2)/2));
picture t; % this time erase the background of the label first
t = thelabel(btex $h(\bar{x}_t)-a$ etex, (x4,y1/2+2.5));
unfill bbox t; draw t;

% draw the function itself
drawoptions(withpen pencircle scaled 1 withcolor .67 red);
draw ff;

% add 5% margin all round
z0 = center currentpicture;
setbounds currentpicture to bbox currentpicture shifted -z0 scaled 1.05 shifted z0;
endfig;
end.


## Notes

• MP has a nice "implied multiplication" feature that lets you write 6x in formulae instead of 6*x, but this has the side effect that the precedence of multiplication and exponentiation is not what you might expect. So 3(x-5)**2 comes out as (3(x-5))**2, so I have had to write 3((x-5)**2) in the function definition.

• Defining a basic unit, u=7mm, and then defining all other lengths in terms of that lets you adjust the scale of the overall drawing easily.

• The variable s is used for the step size of the loop that plots the curve. A value of 1/4 is ok for some plots, 1/16 is probably overkill. Using negative powers of 2 means that the sums come out neatly in MP's idiosyncratic arithmetic.

• The concept of "time" along a path is explained in the manual (follow the link above). directiontime tells us at what time (along the path) we are travelling in a given direction.

• infinity is actually defined to be just less than 4096 but it's big enough for most purposes.