10

I want to graph two quadratic equations using pgfplots. The first one work fine, however when I add the second equation, I get the following error message:

Package PGF Math Error: Unknown operator `x' or `x+' (in 'x^2 - 3x + 4').

It seems PGFplots will not let me multiply numbers at all. Functions such as 2x or even x * 2 don't seem to work. Here is what I am working with:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel = {$x$},
    ylabel = {$ y = f(x)$}
]
    \addplot[no markers, red]{x^2};
    \addplot[no markers, blue]{x^2 - 3x + 4};
\end{axis}
\end{tikzpicture}

The red plot works fine, but the blue one causes a compilation error.

14

pgfplots doesn't assume multiplication between scalars and variables, so your expression should be x^2 - 3*x + 4:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = center,
    xlabel = {$x$},
    ylabel = {$y = f(x)$}
]
    \addplot[no markers, red]{x^2};
    \addplot[no markers, blue]{x^2-3*x+4};
\end{axis}
\end{tikzpicture}
\end{document}
|improve this answer|||||
  • For some reason, this does not work on ShareLaTeX, but it works when compiled locally -- something to keep in mind if you use ShareLaTeX. – Joe the Person Sep 27 '14 at 16:42
  • Figured it out, I removed \tracingall and that caused it to work. Thanks! – Joe the Person Sep 27 '14 at 17:05
3

Try:

 \addplot[no markers, blue]{x^2-3*x+4};

That should work.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.