# Drawing Piecewise Graphs in LaTeX [duplicate]

This question already has an answer here:

I have been using LaTeX forever, but strangely just realized I've never had to plot a graph in it. The graph is seen here:

and the equations are here

I want to use

\usepackage{pgfplots}


However I am open to using other packages if you recommend any?

Thanks

## marked as duplicate by Bobyandbob, CarLaTeX, Mensch, Stefan Pinnow, Phelype OleinikMar 11 '18 at 21:30

• Welcome! The subject of piecewise graphs has been asked and answered many times on this site – cmhughes Mar 11 '18 at 20:45
• – Stefan Pinnow Mar 11 '18 at 20:46

as starting point:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=60]
\addplot[blue, domain=0:pi] {cos(deg x)-1};
%
\addplot[blue, domain=pi:2*pi] {3*cos(deg x)+1};
\end{axis}
\end{tikzpicture}
\end{document}


where deg make conversion of argument x from radians to degrees (trigonometric functions are defined for degrees).

• or you can write x r it will understand radians context with that, or change the trig format – percusse Mar 11 '18 at 20:56
• @percusse, i simply forgot on this option :-(. thank you very much to remind me :-). but now i make short text, and receive error. i should repeat my test more carefully. – Zarko Mar 11 '18 at 20:59

Same output as Zarko's nice answer, but really piecewise functions with ifthenelse.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[samples=60]
\addplot[blue, domain=0:2*pi] {ifthenelse(x<pi,cos(deg x)-1,3*cos(deg x)+1)};
\addplot[green,domain=0:2*pi] {ifthenelse(x<pi,-sin(deg x),-3*sin(deg x))};
\end{axis}
\end{tikzpicture}
\end{document}