0

I want to plot the following function:

enter image description here

I can easily use Pgfplots to plot each section, but I could not unite the two functions without using a vertical function (which I think is an ugly solution). Is there a way to correctly plot conditional functions?

0

1 Answer 1

2

Description

You can put any pgfmath expression (see pgf manual, chapter: "Mathematical Expressions") as parameter to \addplot: In your case:

ifthenelse( <condition>, <then>, <else>)

or the short form:

<condition> ? <then> : <else>

Note: You could also mark the discontinuity, instead of drawing a vertical line (see How can I add open and closed circles on either side of a jump discontinuity?).

Result

enter image description here

Code

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[xmin=0]
        %\addplot[red, samples=300] { ifthenelse(x > 4, 0, .1*(.5*x-4)^2) };
        \addplot[blue, samples=300] { x > 4 ? 0 : .1*(.5*x-4)^2 };
    \end{axis}
\end{tikzpicture}

\end{document}
1
  • Perfect answer! Thank you very much! never thought to check the manual will do that next time! Feb 8, 2023 at 17:46

You must log in to answer this question.

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