I have a function defined as:

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.
I have a function defined as:

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.
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:

\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}
t=0, t=1, and t=2.
– kiss my armpit
Jul 12 '12 at 04:51
pgfplots. I am using the latest TeXLive2011.
– Peter Grill
Jul 12 '12 at 05:34
\foreach loop that invokes a separate \addplot for each segment:
\foreach \min/\max/\function in {-10 / -1 / 0, -1 / 0 / {1/4*x+1/4}, 0 / 1 / 0.5, 1 / 2 / {1/12*x+7/12}, 2 / 10 / 1}{ \addplot [blue, ultra thick] [domain=\min:\max]{\function};}
\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 function

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

\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}
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.

\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}
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.
I added some macros to the preamble for adjusting the size of canvas and borders. Hopefully it is useful for you.

\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}
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.

\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.