# How to draw ReLU function

I want to draw functions (like ReLU) that have different function values for different domains.

ReLU is defined as;

f(x)=0 for x<0 f(x)=x for x>=0

I've adapted hugovdberg's answer to: How can I produce such a graph of my piecewise function?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-3:5,
]
\end{axis}
\end{tikzpicture}
\end{document}


• Thanks, another question on your answer. How can I make the values of function shown from zero? Oct 18 '16 at 8:59
• @guest345 What does it mean? Oct 18 '16 at 10:06

Here is another option, taking advantage of the definition of > and < as boolean operators in pgfplots. It has the advantage to define one single function (hence no legend issues, style is easier to define, etc.).

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-3:5,
]
\end{axis}
\end{tikzpicture}
\end{document}

• Very nice indeed! Oct 18 '16 at 16:59

As JM114516 already stated in his answer, the solution from Ignasi is sub-optimal, because drawing two lines for one line has several disadvantages.

Here I present a solution that is a bit more general, i.e. it isn't restricted that one side of the piecewise function is zero, by using the ifthenelse operator. And to show the "generality" of this solution I have used x^2 for the then part instead of just 0 which gives the solution guest345 was asking for.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-3:5,
]