8

How can I plot the function $f(x) = \frac{732x - 228}{19} when x≤4 and \frac{120x^2+122x - 38}{19} when x>4. I used the following codes in latex

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$x$},
ylabel={$y$},
xmin=1, xmax=10,
ymin=5, ymax=700,
]
\addplot[domain=1:4,blue]{\frac{732x-228}{19}};
\addplot[domain=4:10,blue]{\frac{120x^2+122x - 38}{19}};
\end{axis}
\end{tikzpicture}
\end{document}
1
  • 1
    Welcome to TeX.SX ! Use the engine math language instead of LaTeX math display commands, and you should be fine. For example : \addplot[domain=1:4,blue]{(732*x-228)/19};
    – marsupilam
    Commented Jun 6, 2017 at 6:56

3 Answers 3

7

You need to enter the functions in the correct syntax. \frac is a command for typesetting fractions. For functions in pgfplots you write multiplication with * and division with /. Thus your first function becomes:

(732*x-228)/19

The parser used is that from pgf, so see the pgf manual for further details and which functions may be used. Note you do not need to load tikz as pgfplots does this for you.

Sample output

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$x$},
ylabel={$y$},
xmin=1, xmax=10,
ymin=5, ymax=700,
]
\addplot[domain=1:4,blue]{(732*x-228)/19};
\addplot[domain=4:10,blue]{(120*x^2+122*x - 38)/19};
\end{axis}
\end{tikzpicture}
\end{document}
5

Another solution is to use piecewise functions using logicals to define their range. The logical x<=1 will evaluate to one when the domain is in x less than or equal to one, or zero when the domain is greater than one. So you can multiply a function by a logical to make the value equal to the function value in its domain and zero outside the domain. Then you can superimpose the functions.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$x$},
ylabel={$y$},
xmin=1, xmax=10,
ymin=5, ymax=700,
]
\addplot[domain=1:10,blue]
{
(x<=4) * (732*x - 228)/19 +
(x>4)  * (120*x^2 + 122*x - 38)/19
};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Although this solution is late it has some added benefits. For example, you only use one plot to create it, which simplifies the legend entry if needed. Another benefit, or drawback, depending on your desired result is the functions will be connected by a vertical line on the discontinuous point.

5

enter image description here

\frac{...}{...} is syntax for writing equations, for math calculation you should use * for multiplying, / for dividing and ^ for exponent:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
xlabel={$x$},
ylabel={$y$},
xmin=1, xmax=10,
ymin=5, ymax=700,
]
\addplot[domain=1:4,blue]{(732*x - 228)/19}; % <---
\addplot[domain=4:10,blue]{(120*x^2 + 122*x - 38)/19}; % <---
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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