# How to graph piece wise function. f(x)={-x for -l<x≤0 & 0 otherwise}

f(x)={-x for -l<x≤0
& 0 otherwise}


How to graph the above equation. pls help. also explain

-
If you have tried anything, please add the code. – Torbjørn T. Jan 3 at 14:15
Gentlemen, let us save this questions. :) – Paulo Cereda Jan 3 at 14:40
@Rahilgupta How about tex.stackexchange.com/a/261534/36296, this plots a function with 3 cases. – samcarter Jan 3 at 14:49
This should not be closed yet. Please wait at least 24 hours after asking the OP for improvements to the question before voting to close. Also, if you downvote, please don't forget to revert the vote after the question is improved. – Martin Schröder Jan 3 at 15:20

I suggest you use the pgfplots package. Since your function has three intervals I split the plot in these three

 0  for x ≤ -l
-x  for -l < x ≤ 0
0  for 0 < x


There is a discontinuity point at x = -l, so I drew a filled circle at f(-l) = 0 and an empty circle at f(-l) = l.

Also, don't forget to jksabn a alkew klakjlre.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$x$},ylabel={$f(x)$},
xtick={-1,0},xticklabels={$-l$,$0$},
ytick={0,1},yticklabels={$0$,$l$},
no marks,
]
\node[blue,draw,fill=blue ,circle,inner sep=1pt] at (axis cs:-1,0) {};
\node[blue,draw,fill=white,circle,inner sep=1pt] at (axis cs:-1,1) {};
\end{axis}
\end{tikzpicture}
\end{document}


-

Probably you like to draw single saw tooth ...

With help of TikZ it is easy:

\documentclass[border=3mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[ ->]  (0,-0.1) node[below] {0} -- (0,2) node[below right] {$f(x)$};
\draw[<->]  (-3,0) node[below] {$-\infty\gets x$} --
( 3,0) node[below] {$x \to +\infty$};
\draw   (-0.1,1) node[left] {1} -- + (0.2,0);
\draw   (-1,0.1) -- + (0,-0.2) node[below] {$-1$};
%
\draw[very thick,red]   (-2.5,0) -- (-1,0)
(-1.0,1) -- (0,0)
( 0.0,0) -- (2.5,0);
\draw[very thin,dashed,red] (-1,0) -- (-1,1);
\end{tikzpicture}
\end{document}

-
There is a handy synonym for \leftarrow which is \gets (just as \to for \rightarrow) – Henri Menke Jan 3 at 15:04
Thank for editing my answer, it seems that new year is to young that I will be full operational :( , – Zarko Jan 3 at 15:05
@HenriMenke, thank you for \gets, I will consider it. – Zarko Jan 3 at 15:06

For the sake of quackiness, let us add a gnuplot hybrid solution! :)

% arara: pdflatex: { shell: yes }
\documentclass{article}

\usepackage{gnuplottex}

\begin{document}

\begin{gnuplot}[terminal=pdf]
set key inside left top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
f(x) = -1 < x && x <= 0 ? -x : 0
plot f(x)
\end{gnuplot}

\end{document}


The output:

-
Something's weird with gnuplot's choice of points for this plot: the line never gets to (-1, 1) or (0, 0). – Mike Renfro Jan 4 at 4:30
@Mike: indeed, I will take a look later, I believe it's a matter of specifying the desired range. I don't even use gnuplot that much. :) – Paulo Cereda Jan 4 at 8:09
@PauloCereda Might also be related to the amount of sampled points, I'd guess. If yes, 'set sample 1000' should help. And why using the pdfterminal if there is the cairolatex or even tikz terminal? :p – John Mar 21 at 16:16

Here is a way with pstricks:

\documentclass[border=4pt, svgnames]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{pst-plot, pst-node}
\usepackage{auto-pst-pdf}

\begin{document}

\psset{unit=3cm, arrowinset=0.12, ticksize= -2pt 2pt, linejoin=1}
\begin{pspicture*}(-2.6,-1.1)(1.6,1.9)
\psaxes[linecolor=LightSteelBlue, tickcolor=LightSteelBlue]{->}(0,0)(-2.6,-1.1)(1.6,1.9)[$x$, -135] [$y$,-135]%
\uput[dl](0,0){0}
\pnode(-2.6,0){A}\pnode{} \pnode(0,0.9pt){O} \pnode(1.55,0.9pt){B}\pnode(-1,0){C}\pnode(-1,1){D}
\psline[linestyle=dashed, dash=4pt 4pt, linewidth=0.3pt](0,1)(D)(C)
\psset{linecolor=IndianRed}
{\psset{linewidth =1.2pt}
\ncline[offset=0.9pt, dotsize=3pt, arrows=-*]{A}{C}\psline(D)(O)(B)}
\psdot[dotstyle=o](D)
\end{pspicture*}

\end{document}


-

Another way with mfpic, a very efficient LaTeX interface to MetaPost:

\documentclass[border=2mm]{standalone}
\usepackage[metapost]{mfpic}
\setlength{\mfpicunit}{1cm}
\opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[3]{-2.5}{1.5}{-0.5}{1.5}
\doaxes{xy}
\dashed\lines{(-1, 1), (0, 1)}
\drawcolor{red}
\dashed\lines{(-1, 0), (-1, 1)}
\penwd{1bp}
\lines{(\xmin, 0), (-1, 0)}
\lines{(-1, 1), (0, 0), (\xmax-.04, 0)}
\pointcolor{red}
\point[4pt]{(-1, 0)}
\pointfillfalse
\point[4pt]{(-1, 1)}
\tlpointsep{3bp}
\tlabels{[tc](\xmax, 0){$x$} [cr](0, \ymax){$y$} [tr](0, 0){$O$}
[tc](-1, 0){$-1$} [br](0, 1){$1$}}
\end{mfpic}
\closegraphsfile
\end{document}


To be compiled with LaTeX, then MetaPost, then LaTeX again. Result:

-

With Asymptote.

settings.outformat="pdf";
import graph;

size(6cm,4cm,IgnoreAspect);

real l = 1;
real epsilon = 1e-16;

real f(real x) {
if (-l < x && x <= 0)
return -x;
else
return 0;
}

draw(graph(f, -2, -l-epsilon),blue);
draw(graph(f, -l+epsilon, 2),blue);
dot((-l,f(-l-epsilon)),blue);
dot((-l,f(-l+epsilon)),blue,UnFill);

ylimits(-0.2,1.2);

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,RightTicks);


-

A solution using R, embedded in the LaTeX source file, precompiled using knitr.

\documentclass[border=.25in]{standalone}
\begin{document}
<<echo=FALSE>>=
plot(NA,xlim=c(-2,2),ylim=c(-.2,1.2),xlab="x",ylab="y")

0*x is quite impressive. I would suggest using lines(c(-2,-1), rep(0,2)) etc., points with pch=16 to show inclusion/exclusion, and finally, set the ylim=c(-0.05, 1.05). And probably asp=1, too. – Andreï Kostyrka Jan 24 at 16:13