LoginSignup
1
0

manim で区分求積のアニメーション

Last updated at Posted at 2023-08-02

はじめに

速さと距離の関係をアニメーションにして示したかったので、manimで作ってみました。

コードはjupyter notebook(あるいは、google colab(後述))で動かしてください。notebook以外では1行目のマジックコマンド(%%で始まるやつ)が使えません。

コード

自分の環境がある人はコピペするだけで使えます。

%%manim -qm RiemanIntegral

class RiemanIntegral(Scene):
    def construct(self):
        axes = Axes(
            x_range=[-0.5, 5],
            x_length=8,
            y_range=[-0.5, 10],
            y_length=7
        ).add_coordinates()
        
        axes_labels = axes.get_axis_labels(
            MathTex("t").scale(1), MathTex("v").scale(1)
            )

        graph = axes.plot(
            lambda x: 0.4*(x-1) * (x-3) * (x-4) +6,
            x_range=[-0.5, 5],
            color=YELLOW
        ) 
        
        graph_label = axes.get_graph_label(
            graph=graph,
            label=MathTex("v(t)")   
        )        
        
        self.add(axes, graph, axes_labels, graph_label)


        #dividing setting
        dx_list = [4, 1, 0.5, 0.3, 0.1, 0.05, 0.01]
        rectangles = VGroup(
            *[
                axes.get_riemann_rectangles(
                    graph=graph, 
                    x_range=[0, 4],
                    dx=dx,
                    stroke_width=0.1,
                    stroke_color=WHITE, 
                )
                for dx in dx_list
            ]
        )

        


        first_area = rectangles[0]
        for k in range(1, len(dx_list)):
            new_area = rectangles[k]
            self.play(Transform(first_area, new_area), run_time=2)
            self.wait(0.5)

        self.wait()

コードの説明については、私も完全に理解しているわけではないので省略します。pythonの文法を知っていればなんとなくわかると思います。
質問があればぜひしてください。私がわかる範囲で頑張って答えます。

google colabで使うために

自分のパソコンに自分の環境を作るのは少し手間がかかるので、google colabでの使い方を説明します。
google colabを使うにはgoogleアカウントが必要なので持っていない方は作成してください。

https://docs.manim.community/en/stable/installation/jupyter.html#google-colaboratory に書いてあることをコピペしてgoogle colabで実行するだけです。

実行した後はセルに上のコードをコピペして実行してください。

参考文献

manimの公式サイトに使い方は載っています。

かなりお世話になったyoutubeチャンネルです。ただし、少しversionが古いので使えないものもあります。公式サイトできちんと確認しておくようにしましょう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0