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

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

Thruston
  • 42,268

7 Answers7

14

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,
    ]
    \addplot[blue,domain=-2:-1] {0};
    \addplot[blue,domain=-1:0] {-x};
    \addplot[blue,domain=0:2] {0};
    \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}

enter image description here

Henri Menke
  • 109,596
9

Probably you like to draw single saw tooth ...

enter image description here

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}
Zarko
  • 296,517
7

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:

Quack

Paulo Cereda
  • 44,220
  • 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 04 '16 at 04: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 04 '16 at 08: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 '16 at 16:16
5

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}

enter image description here

Bernard
  • 271,350
2

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:

enter image description here

Franck Pastor
  • 18,756
2

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);

enter image description here

Henri Menke
  • 109,596
2

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") 
curve(0*x, from=-2, to=-1, add=TRUE)
curve(-x, from=-1, to=0, add=TRUE)
curve(0*x, from=0, to=2, add=TRUE)
@
\end{document}

enter image description here

  • 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ï V. Kostyrka Jan 24 '16 at 16:13