TikZ が関数を間違って描画します。 - コードを改善するにはどうすればよいですか?

TikZ が関数を間違って描画します。 - コードを改善するにはどうすればよいですか?

このTikZコードを入手しましたが、これはgeogebraとは異なる結果をもたらすと言われました(https://artofproblemsolving.com/community/c68h3228155_need_help_with_tikz):

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[thick, domain=-1.5:2] plot(\x, {2.8*\x^4+4.77*\x^3-5.47*\x^2-7.61*\x});
\end{tikzpicture}

また、漸近線は TikZ として別の結果を示します。上記のリンクの投稿 #2 を参照してください。

TikZ で改善できる点は何でしょうか?

答え1

-問題は、 を展開するときの の展開/優先順位にあります\x@Skillmon の解決策または括弧を追加します:

\documentclass[border=3.14,tikz]{standalone}

\begin{document}

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[thick, red, domain=-1.5:2] plot(\x, {2.8*(\x)^4+4.77*(\x)^3-5.47*(\x)^2-7.61*(\x)});
\draw[thick, dotted, domain=-1.5:2] plot(\x, {2.8*\x*\x*\x*\x+4.77*\x*\x*\x-5.47*\x*\x-7.61*\x});
\end{tikzpicture}

\end{document}

ここに画像の説明を入力してください

次のようにすると、符号の優先順位の問題がさらに顕著になります。

\documentclass[border=3.14,tikz]{standalone}

\begin{document}

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[thick, red, domain=-1.5:2] plot(\x, {\x^2});
\draw[thick, dotted, domain=-1.5:2] plot(\x, {(\x)^2});
\end{tikzpicture}

\end{document}

ここに画像の説明を入力してください

たとえば、\x^2が展開され、値が負、たとえば -2 の場合、 が得られ-2^2、これは と解釈されるようです-4

答え2

ティZ には指数関数に関する問題があり、\x*\x*\x*\xの代わりにを使用します\x^4

\documentclass[border=3.14,tikz]{standalone}

\begin{document}

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[thick, domain=-1.5:2] plot(\x, {2.8*\x*\x*\x*\x+4.77*\x*\x*\x-5.47*\x*\x-7.61*\x});
\end{tikzpicture}

\end{document}

ここに画像の説明を入力してください

これをさらに改善するには、 を減らしdomain(x=2 で評価された関数は 45.86 になります)、 を増やしsamples、 を使用することもできますsmooth


\documentclass[border=3.14,tikz]{standalone}

\begin{document}

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[thin, red, domain=-1.5:1.5, samples=101, smooth] plot(\x, {2.8*\x*\x*\x*\x+4.77*\x*\x*\x-5.47*\x*\x-7.61*\x});
\end{tikzpicture}

\end{document}

関数にまだ数値的な問題がある場合 (この関数の場合はそうではありませんが、発生する可能性があります)、パッケージを使用することをお勧めしますpgfmath-xfp。プロットの速度は少し遅くなりますが、パッケージで定義された関数を使用すると、解像度が大幅に向上します。

\documentclass[border=3.14,tikz]{standalone}

\usepackage{pgfmath-xfp}

\begin{document}

\begin{tikzpicture}[scale=.5]
\draw[->] (-3,0)--(5,0) node[below]{$x$};
\draw[->] (0,-7)--(0,3) node[left]{$y$};
\draw[red, domain=-1.5:1.5, samples=101, smooth] plot(\x, {2.8*\x*\x*\x*\x+4.77*\x*\x*\x-5.47*\x*\x-7.61*\x});
\pgfmxfpdeclarefunction{myfunction}{1}
  {2.8*(#1)^4+4.77*(#1)^3-5.47*(#1)^2-7.61*(#1)}
\draw[very thin, blue, domain=-1.5:1.5, samples=101,smooth] plot(\x, {myfunction(\x)});
\end{tikzpicture}

\end{document}

ここに画像の説明を入力してください

関連情報