動畫:如何連續移動拋物線?

動畫:如何連續移動拋物線?

我怎麼能繪製拋物線圖$f(x)=x^2$並製作它的垂直動畫。

\documentclass{standalone}

\usepackage{mathtools} %includes amsmath
\usepackage{mathabx}
\usepackage{tikz}
\usetikzlibrary{patterns,calc,arrows}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}[>=triangle 45, cap=round]

\draw[->] (-4.0,0) -- (4.0,0); 
\node[below] at (4.0, 0.0) {\textit{x}};
\draw[->] (0,-4.0) -- (0,4.0);
\node[left] at (0.0,4.0) {\textit{f(x)}};


\draw[blue, line width=1.75pt, domain=-2.00 : 2.00, samples=1500] plot[smooth](\x, {\x*\x });

\end{tikzpicture} 

\end{document}

我怎麼能慢慢地將這個拋物線向右移動3個單位並改變顏色並寫入$f(x-3)$

答案1

使用該animate包,您可以執行以下操作:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{animate}

\begin{document}
\begin{animateinline}[controls, palindrome]{15}
\multiframe{33}{rt=-4+0.25}{
    \begin{tikzpicture}
    \\clip (-5.5,-1.5) rectangle (5.5,9.5);
    
    \draw[->] (-5,0) -- (5,0) node[above] {$x$};
    \draw[->] (0,-1) -- (0,9) node[above] {$y$};
    \path (0,0) node[below left] {$0$};

    \begin{scope}[shift={(\rt,0)}]
        \draw (-4,8) parabola bend (0,0) (4,8);
    \end{scope}
    \pgfmathsetmacro{\y}{abs(\rt^2)/2}
    \node[fill=red, circle, inner sep=2pt] at (0,\y) {};
    
    \end{tikzpicture}
}
\end{animateinline}
\end{document}

在此輸入影像描述


應用於您的 MWE,您的程式碼可能如下所示(因為我不知道您要更改為哪種顏色,所以我只是選擇了紅色):

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{animate}

\begin{document}
\begin{animateinline}[controls, palindrome]{4}
\multiframe{4}{rt=0+1}{
    \begin{tikzpicture}[>=triangle 45, cap=round]
    
    \clip (-4.5,-4.5) rectangle (4.5,4.5);

    \draw[->] (-4,0) -- (4,0); 
    \node[below] at (4,0) {$x$};
    \draw[->] (0,-4) -- (0,4);
    
    \ifdim\rt pt=0pt\relax
        \node[left] at (0,4) {$f(x)$};
    \else
        \node[left] at (0,4) {$f(x - \pgfmathprintnumber[precision=3]{\rt})$};
    \fi

    \pgfmathtruncatemacro{\colorgrad}{\rt/(4-1)*100}
    \draw[red!\colorgrad!blue, line width=1.75pt, domain=-2.00 : 2.00, samples=101] plot[smooth] ({\x+\rt}, {\x*\x});
    
    \end{tikzpicture} 
}
\end{animateinline}
\end{document}

在此輸入影像描述

當然,您也可以添加更多步驟,但隨後您必須考慮編寫什麼作為標籤y軸。 (上面的程式碼有些“硬化”,現在也接受浮點數,因此您可以使用不同的設定。)

相關內容