# Drawing function in tikz

I need to make a graphs as you can see in the picture, but I try many ways and I cannot get what I want. Any of you could help me?

-
Please let us know the function. – kiss my armpit Jul 31 '14 at 12:17
Also, show what you have tried previously! Welcome to TeX.SX! – darthbith Jul 31 '14 at 12:42

This solution assumes the plot is symmetric, (but, non-symmetric is the dashed plot.) Since no actual function is given, this solution provides a possible idea to draw. A solution using pgfpplots of function (x, 4-1/x) and use symmetric property with respect to y.

Code

\documentclass{article}
\usepackage[margin=1cm,paper size={15cm,10cm}]{geometry}
\thispagestyle{empty}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0.01:4, samples=100,grid=major,
restrict y to domain=-4:4,xlabel=$x$,ylabel=$y(x)$, legend pos=north west]
\addplot[dashed] (-x,4-3/x); % for non-symmetric branch
\end{axis}
\end{tikzpicture}
\end{document}

-
Thank you very, very useful! – naw Aug 1 '14 at 13:08
Glad it help and thank you too. – Jesse Aug 1 '14 at 13:11

An easy-to-customize template with PSTricks.

\documentclass[pstricks,border=0pt,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot}
\usepackage[nomessages]{fp}

\FPeval\XMin{0-3}
\FPeval\XMax{3}
\FPeval\YMin{0-3}
\FPeval\YMax{4.5}

\FPeval\XOL{0-1/3} % of DeltaX
\FPeval\XOR{1/3} % of DeltaX
\FPeval\YOB{0-1/3} % of DeltaY
\FPeval\YOT{1/3} % of DeltaY

%\FPset\TrigLabelBase{3}
\FPeval\DeltaX{1}
\FPeval\DeltaY{.5}

\FPeval\AxisL{XMin+DeltaX*XOL}
\FPeval\AxisR{XMax+DeltaX*XOR}
\FPeval\AxisB{YMin+DeltaY*YOB}
\FPeval\AxisT{YMax+DeltaY*YOT}

\newlength\Width\Width=10cm
\newlength\Height\Height=8cm

\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt

\psset
{
llx=\llx,
lly=\lly,
urx=\urx,
ury=\ury,
%xtrigLabels=true,
%ytrigLabels=true,
%trigLabelBase=\TrigLabelBase,
labelFontSize=\scriptstyle,
xAxisLabel=$x$,
yAxisLabel=$y$,
algebraic,
plotpoints=100,
}

\def\f{4-1/abs(x)}

\begin{document}
\pslegend[rt]{%
\color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=4-1/\lvert x\rvert$ \\
\color{Red}\rule{12pt}{1pt} & \color{Red} $y=4$
}
\begin{psgraph}
[
dx=\DeltaX,
dy=\DeltaY,
Dx=\DeltaX,
Dy=\DeltaY,
linecolor=gray,
tickcolor=gray,
ticksize=-3pt 3pt,
axespos=top,
]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
\psaxes
[
dx=\DeltaX,
dy=\DeltaY,
labels=none,
subticks=5,
tickwidth=.4pt,
subtickwidth=.2pt,
tickcolor=Red!30,
subtickcolor=ForestGreen!30,
xticksize=\YMin\space \YMax,
yticksize=\XMin\space \XMax,
subticksize=1,
](0,0)(\XMin,\YMin)(\XMax,\YMax)
\psclip{\psframe[linestyle=none,linewidth=0](\XMin,\YMin)(\XMax,\YMax)}
\psplot[linecolor=NavyBlue]{\XMin}{-0.1}{\f}
\psplot[linecolor=NavyBlue]{0.1}{\XMax}{\f}
\psplot[linecolor=Red]{\XMin}{\XMax}{4}
\endpsclip
\end{psgraph}
\end{document}


-
Thank you very much! it's very useful! – naw Aug 1 '14 at 13:08