euler

 avatar
unknown
python
3 years ago
664 B
8
Indexable
import matplotlib.pyplot as plt
import math

PI = math.pi

def euler(theta0, v0, dt, T):
    t = 0

    v = v0
    theta = theta0

    ys = []
    xs = []
    zs = []

    delta = PI / dt;

    while t <= T:
        O = theta

        xs.append(t)
        ys.append(v)
        zs.append(theta)

        theta += v / delta
        v += -math.sin(O / delta)

        t += dt


    return xs, ys, zs


def plot_graphs(dt, T):
    xs, ys, zs = euler(0, 1, dt, T)

    plt.figure()
    plt.plot(xs, ys)
    plt.legend('v')

    plt.figure()
    plt.plot(xs, zs)
    plt.legend('theta')
    plt.show()


plot_graphs(6 * PI / 200, 6 * PI)
plot_graphs(6 * PI / 20, 6 * PI)
Editor is loading...