4

I am trying to graph a function using PGFPlots

f(x)=\exp(-\frac{x^2}{2})(x^2-1)

my code is

\begin{tikzpicture}
\begin{axis}[xmin = -8, xmax = 8,
ymin = -2, ymax = 3,xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]
\addplot[smooth,
thick,
blue,] exp(-x^2/2)(x^2-1);
\end{axis}
\end{tikzpicture}

what I get is

enter image description here

instead of

enter image description here,

please help me...

0

1 Answer 1

8

What I get is an error, actually. When plotting a function of x you should do

\addplot {<function>};

or

\addplot [<option>] {<function>};

if you're adding any options to the plot. You have done just \addplot [...] <function>; without the {}.

In addition, note that multiplication has to be specified explicitly with *, so you need

\addplot[smooth, thick,blue,] {exp(-x^2/2)*(x^2-1)};

Looking at the screenshot from Overleaf, I'd suspect you just forgot the *. With the above \addplot, I get

enter image description here

By default the domain plotted is -5:5, that's why the graph stops where it does. Add domain=-8:8 to either the axis options or the \addplot options if you want to plot the function to the axis limits you have specified.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}   
\begin{tikzpicture}
\begin{axis}[xmin = -8, xmax = 8,
ymin = -2, ymax = 3,xtick distance = 2.5,
ytick distance = 0.5,
grid = both,
minor tick num = 1,
major grid style = {lightgray},
minor grid style = {lightgray!25},
width = \textwidth,
height = 0.5\textwidth,
]
\addplot[smooth,
thick,
blue,] {exp(-x^2/2)*(x^2-1)};
\end{axis}
\end{tikzpicture}
\end{document}
2
  • 1
    thanks it helps@Torbjørn T.
    – ROBIN RAJ
    Jul 12, 2020 at 7:36
  • Even if x^2 now works for negative x, it is still bad practice (use x*x). Jul 12, 2020 at 19:58

You must log in to answer this question.

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