为什么图是错的?指数函数是正的,所以它一定不会低于 0 线
\documentclass[a4paper,12pt]{book}
\pdfpagewidth\paperwidth
\pdfpageheight\paperheight
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture} [ declare function={
func(\x)= (\x <= -1) * (0) +
and(\x > -1, \x < 1) * (exp(1/(\x*\x-1))) +
(\x >=1) * (0);}]
\begin{axis}[domain=-3:3]
\addplot [blue,smooth] {func(x)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
即使您定义的函数不平滑,该smooth
选项也会尝试绘制平滑的函数。请尝试增加样本数量,如下所示:x=-1
x=1
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture} [ declare function={
func(\x)= (\x <= -1) * (0) +
and(\x > -1, \x < 1) * (exp(1/(\x*\x-1))) +
(\x >=1) * (0);}]
\begin{axis}[domain=-3:3]
\addplot [blue, samples=500] {func(x)};
\end{axis}
\end{tikzpicture}
\end{document}