## 2020年5月1日金曜日

### 数学 - Python - 解析学 - 多変数の関数 - 合成微分律と勾配ベクトル - 楕円、パラメーター表示

1. $\begin{array}{l}x=a\left(\mathrm{cos}t\right)\\ y=b\left(\mathrm{sin}t\right)\\ \frac{{x}^{2}}{{a}^{2}}+\frac{{y}^{2}}{{b}^{2}}\\ =\frac{{a}^{2}{\mathrm{cos}}^{2}t}{{a}^{2}}+\frac{{b}^{2}{\mathrm{sin}}^{2}t}{{b}^{2}}\\ ={\mathrm{cos}}^{2}t+{\mathrm{sin}}^{2}t\\ =1\\ F\left(t\right)=\left(a\left(\mathrm{cos}t\right),b\mathrm{sin}t\right)\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
import random
from sympy import symbols, sin, cos, pi, solve, plot
from sympy.plotting import plot_parametric

print('15.')

x, y, t = symbols('x, y, t', real=True)
a = 2
b = 1
eq = x ** 2 / a ** 2 + y ** 2 / b ** 2 - 1
ys = solve(eq, y)
x0 = a * cos(t)
y0 = b * sin(t)

class Test(TestCase):
def test(self):
for _ in range(10):
self.assertEqual(
float((x0 ** 2 / a ** 2 + y0 ** 2 / b ** 2)
.subs({t: random.randrange(-10, 10)})),
1)

px = plot(*ys, (x, -5, 5), ylim=(-5, 5),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(px, colors):
o.line_color = color
px.save('sample15_1.png')

pt = plot_parametric(x0, y0,
xlim=(-5, 5),
ylim=(-5, 5),
show=True)
pt.save('sample15_2.png')

if __name__ == "__main__":
main()


% ./sample15.py -v
15.
test (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.010s

OK
%