Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a function defined as:

enter image description here

How can I graph that using functions instead of coordinates? I'm currently working with pgfplots but I'm open to something else if it's better.

share|improve this question
1  
Welcome to TeX.SE. –  Peter Grill Jul 12 '12 at 5:07
    
Thank you, Peter! –  Dash Jul 12 '12 at 5:54

2 Answers 2

up vote 27 down vote accepted

Your function

enter image description here

has 3 discontinuity points which are at t=0, t=1, and t=2. The following graph is correct from mathematics point of view.

enter image description here

\documentclass[border=0bp]{standalone}

\usepackage{pst-plot}

\begin{document}

\psset{unit=1.5cm}
\begin{pspicture}[showgrid=false](-2.75,-0.75)(4,2)
    \psframe*[linecolor=yellow,opacity=0.5](-2.75,-0.75)(4,2)
    \psaxes[linecolor=lightgray]{->}(0,0)(-2.5,-0.5)(3.5,1.5)[$t$,0][$F(t)$,90]
    \psset{algebraic,linewidth=1.5pt,linecolor=red}
    \psplot[arrows=-o]{-2.5}{-1}{0}
    \psset{arrows=*-o}
    \psplot{-1}{0}{(x+1)/4}
    \psplot{0}{1}{1/2}
    \psplot{1}{2}{(x+7)/12}
    \psplot[arrows=*-]{2}{3.5}{1}
\end{pspicture}

\end{document}

Edit 3

It is easy to join the discontinuity points with vertical lines even though it must be avoided because the graph will no longer tell us about a function.

enter image description here

\documentclass[border=0bp]{standalone}

\usepackage{pst-plot}

\begin{document}

\psset{unit=1.5cm}
\begin{pspicture}[showgrid=false](-2.75,-0.75)(4,2)
    \psframe*[linecolor=yellow,opacity=0.5](-2.75,-0.75)(4,2)
    \psaxes[linecolor=lightgray]{->}(0,0)(-2.5,-0.5)(3.5,1.5)[$t$,0][$F(t)$,90]
    \psset{algebraic,linewidth=1.5pt,linecolor=red}
    \pscustom
    {
        \psplot{-2.5}{-1}{0}
        \psplot{-1}{0}{(x+1)/4}
        \psplot{0}{1}{1/2}
        \psplot{1}{2}{(x+7)/12}
        \psplot{2}{3.5}{1}
    }
\end{pspicture}

\end{document}

Edit 3.1

I forgot to tell you that you need to compile it (to get a tight PDF image) with either xelatex or a sequence of latex followed by dvips followed by ps2pdf.

Import the PDF image from within your input file by using \includegraphics and compile the main input file with pdflatex. I guessed your scenario like this.

Edit 3.14

I added some macros to the preamble for adjusting the size of canvas and borders. Hopefully it is useful for you.

enter image description here

\documentclass[border=0bp]{standalone}

\usepackage{pst-plot}

% to adjust the unit
\psset
{
    xunit=1cm,
    yunit=3cm,
}


% to adjust the axes
\def\L{-2.5}
\def\R{3.5}
\def\B{-0.2}
\def\T{1.2}


% to adjust the borders
\def\dL{2pt}
\def\dR{12pt}
\def\dB{2pt}
\def\dT{18pt}

\begin{document}

\begin{pspicture}[showgrid=false]
        (\dimexpr\L\psxunit-\dL\relax,\dimexpr\B\psyunit-\dB\relax)
        (\dimexpr\R\psxunit+\dR\relax,\dimexpr\T\psyunit+\dT\relax)

    %comment the following \psframe* if you DON'T need a colored background.

    \psframe*[linecolor=blue,opacity=0.1]
        (\dimexpr\L\psxunit-\dL\relax,\dimexpr\B\psyunit-\dB\relax)
        (\dimexpr\R\psxunit+\dR\relax,\dimexpr\T\psyunit+\dT\relax)

        \psaxes[linecolor=lightgray]{->}(0,0)(\L,\B)(\R,\T)[$t$,0][$F(t)$,90]
        \psset{algebraic,linewidth=1.5pt,linecolor=red}
        \psplot[arrows=-o]{\L}{-1}{0}
        \psset{arrows=*-o}
        \psplot{-1}{0}{(x+1)/4}
        \psplot{0}{1}{1/2}
        \psplot{1}{2}{(x+7)/12}
        \psplot[arrows=*-]{2}{\R}{1}
\end{pspicture}

\end{document}

Edit 3.141

I just realized there is a bad feature at each hollow dot when using the default value of plotpoints (which is 20). Fortunately, your functions have low frequencies, so I can reduce plotpoints to 2 without side effects to hide the bad feature.

enter image description here

\documentclass[border=0bp]{standalone}

\usepackage{pst-plot}

% to adjust the unit
\psset
{
    xunit=1cm,
    yunit=3cm,
}


% to adjust the axes
\def\L{-2.5}
\def\R{3.5}
\def\B{-0.2}
\def\T{1.2}


% to adjust the borders
\def\dL{2pt}
\def\dR{12pt}
\def\dB{2pt}
\def\dT{18pt}

\begin{document}

\begin{pspicture}[showgrid=false]
    (\dimexpr\L\psxunit-\dL\relax,\dimexpr\B\psyunit-\dB\relax)
    (\dimexpr\R\psxunit+\dR\relax,\dimexpr\T\psyunit+\dT\relax)

    %comment the following \psframe* if you DON'T need a colored background.
    %\psframe*[linecolor=blue,opacity=0.1]
    %(\dimexpr\L\psxunit-\dL\relax,\dimexpr\B\psyunit-\dB\relax)
    %(\dimexpr\R\psxunit+\dR\relax,\dimexpr\T\psyunit+\dT\relax)

    \psaxes[linecolor=lightgray]{->}(0,0)(\L,\B)(\R,\T)[$t$,0][$F(t)$,90]
    \psset{algebraic,linewidth=1.5pt,linecolor=red,plotpoints=2}
    \psplot[arrows=-o]{\L}{-1}{0}
    \psset{arrows=*-o}
    \psplot{-1}{0}{(x+1)/4}
    \psplot{0}{1}{1/2}
    \psplot{1}{2}{(x+7)/12}
    \psplot[arrows=*-]{2}{\R}{1}
\end{pspicture}

\end{document}

If your functions have high frequency then decreasing plotpoints makes the plots no longer smooth. To avoid this side effect, we have to increase plotpoints and override the hollow dots with solid circles.

share|improve this answer
1  
Also, thanks for the response. The first graph looks perfect! I definitely want it to be disconnected. –  Dash Jul 12 '12 at 5:49
    
Looks perfect. I am actually just going with the version from before Edit ~Pi :) It looked fine to me, but I'll probably use the macros on future graphs. –  Dash Jul 12 '12 at 20:15
    
You created a lot of very good PSTricks examples. Would you like the idea of a PSTricks gallery in the very same shape as the TikZ gallery? I could setup a server with the same structure (based on PostgreSQL and Django) but exclusively for PSTricks. Would be a nice showcase and useful for new users. I could care for gallery maintainance and feeding in content, which is sent to me by email or taken from here. –  Stefan Kottwitz Apr 13 at 9:51
    
@StefanKottwitz: A nice idea. I will participate in such a idea. Thank you very much. –  Oh my ghost Apr 13 at 10:49

If you are using pgfplots you can use pgfmathdeclarefunction to specify the function and then use it the same way you would just a built in function. You can then plot is all at once if you want the end points connected, or plot each section separately:

enter image description here

References:

Code:

\documentclass[border=3pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\newcommand*{\PhM}{\phantom{-}}%

\newcommand{\pLabel}{%
$F(t) = \begin{cases}
  0                     & \PhM t < -1 \\[0.8ex]
  \frac{1}{4}t+\frac{1}{4}  & -1\le t <0 \\[0.8ex]
  \frac{1}{2}           & \PhM 0\le t <1\\[0.8ex]
  \frac{1}{12}t+\frac{7}{12} & \PhM 1\le t <2\\[0.8ex]
  1                     & \PhM t\ge 2
\end{cases}
$
}

\pgfmathdeclarefunction{PieceA}{1}{(0)}%
\pgfmathdeclarefunction{PieceB}{1}{(#1/4 + 1/4)}%
\pgfmathdeclarefunction{PieceC}{1}{(0.5)}%
\pgfmathdeclarefunction{PieceD}{1}{(#1/12 + 7/12)}%
\pgfmathdeclarefunction{PieceE}{1}{(1)}%


\pgfmathdeclarefunction{MyFunction}{1}{%
  \pgfmathparse{%
    (and(   1,    #1<-1)*(0)            +%
    (and(#1>=-1,  #1< 0)*(#1/4 + 1/4)   +%
    (and(#1>= 0,  #1< 1)*(0.5)          +%
    (and(#1>= 1,  #1< 2)*(#1/12 + 7/12) +%
    (and(#1>= 2,    1  )*(1)%
    }%
}


\begin{document}
\begin{tikzpicture}
  \begin{axis}
    \addplot[domain=-5:5, blue, samples=100, ultra thick] {MyFunction(x)};
    \node [right] at (axis cs: -5.5,0.7) {\tiny\pLabel};
  \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}
    \foreach \xStart/\xEnd  in {-5/-1, -1/0, 0/1, 1/2, 2/5} {
        \addplot[domain=\xStart:\xEnd, blue, samples=10, ultra thick] {MyFunction(x)};
    }
    \node [right] at (axis cs: -5.5,0.7) {\tiny\pLabel};% Labe graph

    % Show discontinuty points
    \draw [draw=blue, fill=white, thick] (axis cs: 0, 0.250) circle (2.0pt);
    \draw [draw=blue, fill=blue,  thick] (axis cs: 0, 0.500) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: 1, 0.500) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: 1, 0.666) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: 2, 0.750) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: 2, 1.000) circle (2.0pt);
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
2  
Be careful, the function has 3 discontinuities, at t=0, t=1, and t=2. –  Oh my ghost Jul 12 '12 at 4:51
3  
In mathematics, we don't draw vertical lines at discontinuity points because those lines will make the "function" is no longer a function. –  Oh my ghost Jul 12 '12 at 5:21
1  
@Dash: Please ensure you are using the latest released version of pgfplots. I am using the latest TeXLive2011. –  Peter Grill Jul 12 '12 at 5:34
2  
A hollow dot or solid dot is also important to clarify exactly whether or not the segment end point is undefined at the discontinuity point in question. –  Oh my ghost Jul 12 '12 at 6:09
1  
+1 I like this. For the hollow/solid dots you can use \pgfplotsset{soldot/.style={color=blue,only marks,mark=*}} \pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}} in the preamble, and then \addplot[holdot] coordinates{(0,0.25)(1,0.5)(2,0.75)}; \addplot[soldot] coordinates{(0,0.5)(1,0.666)(2,1)}; in the picture –  cmhughes Jul 12 '12 at 15:14

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.