3

I have the code for a piecewise linear function f defined over the closed interval [-4, 12]. I scaled the image by 2/5.

On the same graph, I want to plot another piecewise function g representing $\int_{2}^{x} f(t) \, dt$ for any number $-4 \leq x \leq 12$. It involves plotting parabolas on six different "pieces" of the domain of f. I have the commands for these in the code, preceded with a %. I get an error message by implementing any of these commands.

I know that f and g can easily be plotted using pgfplots. I would like to be able to plot them using just TikZ, though.

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}

%A piecewise linear function is drawn over the interval [-4, 12]. (The figure is magnified by 2/5.)
\draw ({-4*(2/5)},{-4*(2/5)}) -- (0,{4*(2/5)}) -- ({2*(2/5)},0) -- ({4*(2/5)},{4*(2/5)}) -- ({8*(2/5)},{-4*(2/5)})
-- ({10*(2/5)},0) -- ({12*(2/5)},{-4*(2/5)});

%Two points on the graph are drawn.
\coordinate (left_endpoint) at ({-4*(2/5)},{-4*(2/5)});
\draw[fill] (left_endpoint) circle [radius=1.5pt];
\coordinate (right_endpoint) at ({12*(2/5)},{-4*(2/5)});
\draw[fill] (right_endpoint) circle [radius=1.5pt];


%The graph for the area function "starting at 2" is drawn. It is defined on 6 "pieces" of the
%domain of f.
%\draw[color=blue] plot [domain=-4:0] ({(2/5)*(\x)}, {(2/5)*((\x)^2 + 4*x - 4)});
%\draw[color=blue] plot [domain=0:2] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 4*x - 4)});
%\draw[color=blue] plot [domain=2:4] ({(2/5)*(\x)}, {(2/5)*((\x)^2 - 4*x + 4)});
%\draw[color=blue] plot [domain=4:8] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 12*x - 28)});
%\draw[color=blue] plot [domain=8:10] ({(2/5)*(\x)}, {(2/5)*((\x)^2 - 20*x + 100)});
%\draw[color=blue] plot [domain=10:12] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 20*x - 100)});


%Tick marks are drawn along the y-axis.
\draw ($(0,{-4*(2/5)}) +(-2pt,0)$) -- ($(0,{-4*(2/5)}) +(2pt,0)$);
\path node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,{-4*(2/5)}) +(-2pt,0) +(-0.15,0)$){$-4$};
\draw ($(0,{4*(2/5)}) +(-2pt,0)$) -- ($(0,{4*(2/5)}) +(2pt,0)$);
\path node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,{4*(2/5)}) +(-2pt,0) +(-0.15,0)$){4};


%The axes are drawn.
\draw[latex-latex] ($(0,{-4*(2/5)})  +(0pt,-12.5pt)$) -- ($(0,{4*(2/5)})  +(0pt,12.5pt)$) node[above right]{$y$};
\draw[latex-latex] ($({-4*(2/5)},0) +(-12.5pt,0pt)$) -- ($({12*(2/5)},0) +(12.5pt,0pt)$) node[below right]{$x$};


\end{tikzpicture}
 \end{document}
5
  • 1
    the example produces the error ! Package tikz Error: You need to say \usetikzlibrary{calc} for coordinate calculation. (and has no \end{document} May 1, 2017 at 17:10
  • @David Carlisle Thanks for correcting the code. May 1, 2017 at 17:27
  • I got distracted with a phone call. May 1, 2017 at 17:27
  • If it were continuous to the first derivative you could call it a quadratic spline. May 1, 2017 at 19:44
  • 1
    @John Kormylo The first derivative of g is a continuous function. Over the interval [-4, 0], f(x) = 2x + 4 and g(x) = x^{2} + 4x - 4; over the interval [0, 2], f(x) = -2x + 4 and g(x) = -x^{2} + 4x - 4; over the interval [2, 4], f(x) = 2x - 4 and g(x) = x^{2} - 4x + 4. May 2, 2017 at 14:03

1 Answer 1

4

Not sure if this is what you were expecting but fixing the x to \x

enter image description here

\documentclass{amsart}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}

%A piecewise linear function is drawn over the interval [-4, 12]. (The figure is magnified by 2/5.)
\draw ({-4*(2/5)},{-4*(2/5)}) -- (0,{4*(2/5)}) -- ({2*(2/5)},0) -- ({4*(2/5)},{4*(2/5)}) -- ({8*(2/5)},{-4*(2/5)})
-- ({10*(2/5)},0) -- ({12*(2/5)},{-4*(2/5)});

%Two points on the graph are drawn.
\coordinate (left_endpoint) at ({-4*(2/5)},{-4*(2/5)});
\draw[fill] (left_endpoint) circle [radius=1.5pt];
\coordinate (right_endpoint) at ({12*(2/5)},{-4*(2/5)});
\draw[fill] (right_endpoint) circle [radius=1.5pt];


%The graph for the area function "starting at 2" is drawn. It is defined on 6 "pieces" of the
%domain of f.
\draw[color=blue] plot [domain=-4:0] ({(2/5)*(\x)}, {(2/5)*((\x)^2 + 4*\x - 4)});
\draw[color=blue] plot [domain=0:2] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 4*\x - 4)});
\draw[color=blue] plot [domain=2:4] ({(2/5)*(\x)}, {(2/5)*((\x)^2 - 4*\x + 4)});
\draw[color=blue] plot [domain=4:8] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 12*\x - 28)});
\draw[color=blue] plot [domain=8:10] ({(2/5)*(\x)}, {(2/5)*((\x)^2 - 20*\x + 100)});
\draw[color=blue] plot [domain=10:12] ({(2/5)*(\x)}, {(2/5)*(-(\x)^2 + 20*\x - 100)});


%Tick marks are drawn along the y-axis.
\draw ($(0,{-4*(2/5)}) +(-2pt,0)$) -- ($(0,{-4*(2/5)}) +(2pt,0)$);
\path node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,{-4*(2/5)}) +(-2pt,0) +(-0.15,0)$){$-4$};
\draw ($(0,{4*(2/5)}) +(-2pt,0)$) -- ($(0,{4*(2/5)}) +(2pt,0)$);
\path node[anchor=east, inner sep=0, font=\footnotesize] at ($(0,{4*(2/5)}) +(-2pt,0) +(-0.15,0)$){4};


%The axes are drawn.
\draw[latex-latex] ($(0,{-4*(2/5)})  +(0pt,-12.5pt)$) -- ($(0,{4*(2/5)})  +(0pt,12.5pt)$) node[above right]{$y$};
\draw[latex-latex] ($({-4*(2/5)},0) +(-12.5pt,0pt)$) -- ($({12*(2/5)},0) +(12.5pt,0pt)$) node[below right]{$x$};


\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .