TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
f(x)={-x for -l<x≤0
          & 0 otherwise}

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

share|improve this question
2  
If you have tried anything, please add the code. – Torbjørn T. Jan 3 at 14:15
1  
2  
Gentlemen, let us save this questions. :) – Paulo Cereda Jan 3 at 14:40
2  
@Rahilgupta How about tex.stackexchange.com/a/261534/36296, this plots a function with 3 cases. – samcarter Jan 3 at 14:49
2  
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,
    ]
    \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

share|improve this answer

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}
share|improve this answer
1  
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:

Quack

share|improve this answer
    
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}

enter image description here

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.