2020-05-03 課題

自分用メモ

解答例

プログラムのコピペの功罪

matplotlib

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import matplotlib.pyplot as plt

def koch_snowflake(order, scale=10):
    """
    Return two lists x, y of point coordinates of the Koch snowflake.

    Arguments
    ---------
    order : int
        The recursion depth.
    scale : float
        The extent of the snowflake (edge length of the base triangle).
    """
    def _koch_snowflake_complex(order):
        if order == 0:
            # initial triangle
            angles = np.array([0, 120, 240]) + 90
            return scale / np.sqrt(3) * np.exp(np.deg2rad(angles) * 1j)
        else:
            ZR = 0.5 - 0.5j * np.sqrt(3) / 3

            p1 = _koch_snowflake_complex(order - 1)  # start points
            p2 = np.roll(p1, shift=-1)  # end points
            dp = p2 - p1  # connection vectors

            new_points = np.empty(len(p1) * 4, dtype=np.complex128)
            new_points[::4] = p1
            new_points[1::4] = p1 + dp / 3
            new_points[2::4] = p1 + dp * ZR
            new_points[3::4] = p1 + dp / 3 * 2
            return new_points

    points = _koch_snowflake_complex(order)
    x, y = points.real, points.imag
    return x, y

x, y = koch_snowflake(order=5)

plt.figure(figsize=(8, 8))
plt.axis('equal')
plt.fill(x, y)
plt.show()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import numpy as np
import matplotlib.pyplot as plt

plt.rcParams['legend.fontsize'] = 10

fig = plt.figure()
ax = fig.gca(projection='3d')


theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

ax.plot(x, y, z, label='parametric curve')
ax.legend()

plt.show()

TeX でいろいろな式を書こう

Poisson 積分

\begin{align} u\left(z_{0}\right) &= \frac{1}{2 \pi} \int_{0}^{2 \pi} u\left(e^{i \psi}\right) \frac{1-\left|z_{0}\right|^{2}}{| z_{0}-e^{\left.j \phi\right|^{2}}} d \psi, \ u(x, y) &= \frac{1}{2 \pi} \int_{0}^{2 \pi} u(a \cos \phi, a \sin \phi) \frac{a^{2}-R^{2}}{a^{2}+R^{2}-2 a R \cos (\theta-\phi)} d \phi, \ u(x, y, z)&=\frac{1}{4 \pi a} \int_{S} u \frac{a^{2}-R^{2}}{\left(a^{2}+R^{2}-2 a R \cos \theta\right)^{3 / 2}} d S. \end{align}

いろいろなプログラムを書こう

numpy でのグラフ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-np.pi / 4, np.pi / 4)
plt.plot(x, np.sin(4 * x), label="sin 4x")
plt.plot(x, np.cos(4 * x), label="cos 4x")
plt.plot(x, np.tan(x), label="tan x")

plt.xlabel("x", size=20)
plt.ylabel("y", size=14)
plt.grid()
plt.legend()

plt.show()

sympy

1
2
3
4
from IPython.display import Math, HTML
def load_mathjax_in_cell_output():
    display(HTML("<script src='https://www.gstatic.com/external_hosted/mathjax/latest/MathJax.js?config=default'></script>"))
get_ipython().events.register('pre_run_cell', load_mathjax_in_cell_output)
1
2
3
4
5
x,n = symbols('x,n')

int = Integral(x**n, (x, 0, x))
print("積分それ自体を表示できる")
display(int)
1
2
3
4
5
積分それ自体を表示できる



<IPython.core.display.Javascript object>

$$\int_{0}^{x} x^{n}\, dx$$

1
2
print("単純な計算結果:場合分けまでしてくれるし、0 での無限大もそう書いてくれる")
display(int.doit())
1
2
3
4
5
単純な計算結果:場合分けまでしてくれるし、0 での無限大もそう書いてくれる



<IPython.core.display.Javascript object>

$$\begin{cases} \log{\left (x \right )} + \infty & \text{for}\: n = -1 \- \frac{0^{n + 1}}{n + 1} + \frac{x^{n + 1}}{n + 1} & \text{otherwise} \end{cases}$$

1
2
print("simplify() で 0 を消せる")
display(simplify(int.doit()))
1
2
3
4
5
simplify() で 0 を消せる



<IPython.core.display.Javascript object>

$$\begin{cases} \log{\left (x \right )} + \infty & \text{for}\: n = -1 \\frac{x^{n + 1}}{n + 1} & \text{otherwise} \end{cases}$$

1
2
3
4
from IPython.display import Math, HTML
def load_mathjax_in_cell_output():
    display(HTML("<script src='https://www.gstatic.com/external_hosted/mathjax/latest/MathJax.js?config=default'></script>"))
get_ipython().events.register('pre_run_cell', load_mathjax_in_cell_output)

微分方程式

\begin{align} y_{t}&=c\left(y-\frac{y^{3}}{3}-x+I(t)\right),\x_{t}&=y-bx+a. \end{align}

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from IPython.display import HTML
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy.integrate as integrate

I = 0.34 #external stimulus
a = 0.7
b = 0.8
c = 10

def FHN(state, t):
    """
    FitzHugh-Nagumo Equations
    u : the membrane potential
    v : a recovery variable
    """
    u, v = state
    dot_u = c * (-v + u - pow(u,3)/3 + I)
    dot_v = u - b * v + a
    return dot_u, dot_v

#initial state
u0 = 2.0
v0 = 1.0

fig = plt.figure()
t = np.arange(0.0, 10, 0.01)
len_t = len(t)
dt = 5 #time steps

#animationの1step
def update(i):
    global y, y0

    #y0の初期値の設定
    if i ==0:
        y0 = [u0, v0]

    plt.cla() #現在描写されているグラフを消去

    #微分方程式を解く
    y = integrate.odeint(FHN, y0, t)

    #1Step(=dt)後のyの値を次のステップでのy0の値に更新する
    y0 = (y[dt,0], y[dt,1])

    #配列としてu,vを取得
    u = y[:,0]
    v = y[:,1]

    #描画
    plt.plot(t, u, label="u : membrane potential", color="#ff7f0e")
    plt.plot(t, v, label="v : recovery variable", color="#1f77b4")
    plt.plot(t[len_t-1], u[len_t-1],'o--', color="#ff7f0e") #uのmarker
    plt.plot(t[len_t-1], v[len_t-1],'o--', color="#1f77b4") #vのmarker
    plt.title("Membrane Potential / Volt")
    plt.grid()
    plt.legend(bbox_to_anchor=(0, 1),
               loc='upper left',
               borderaxespad=0)

    return (u, v)

anim = animation.FuncAnimation(fig, update, interval=100,
                              frames=300)
plt.close(anim._fig)
HTML(anim.to_jshtml(default_mode='reflect'))

質問メモ

シューティングゲーム - 敵の弾(概念):クラス:弾がどこにあるか x,y - 具体的に画面に現れる弾: x=1, y=1

1
2
3
import numpy as np
print(type(np.array))
#print(type(np.array()))
1
<class 'builtin_function_or_method'>
1
2
from numpy import *
print(type(array))
1
<class 'builtin_function_or_method'>
1
2
a = [1,2]
print(len(a))