## 2020年4月5日日曜日

### 数学 - Python - 解析学 - 多変数の関数 - 多変数の関数 - 偏微分 - 指数関数、三角関数(正弦と余弦)、積、勾配ベクトル

1. $\begin{array}{l}gradf\left(x,y,z\right)\\ =grad\left({e}^{-2x}\mathrm{cos}\left(yz\right)\right)\\ =\left(-2{e}^{-2x}\mathrm{cos}\left(yz\right),-z{e}^{-2x}\mathrm{sin}\left(yz\right),-y{e}^{-2x}\mathrm{sin}\left(yz\right)\right)\\ gradf\left(1,\pi ,\pi \right)\\ =\left(-2{e}^{-2}\mathrm{cos}\left({\pi }^{2}\right),-\pi {e}^{-2}\mathrm{sin}\left({\pi }^{2}\right),-\pi {e}^{-2}\mathrm{sin}\left({\pi }^{2}\right)\right)\end{array}$

2. $\begin{array}{l}grad\left({e}^{3x+y}\mathrm{sin}\left(5z\right)\right)\\ =\left(3{e}^{3x+y}\mathrm{sin}\left(5z\right),{e}^{3x+y}\mathrm{sin}\left(5z\right),5{e}^{3x+y}\mathrm{cos}\left(5z\right)\right)\\ gradf\left(0,0,\frac{\pi }{6}\right)\\ =\left(\frac{3}{2},\frac{1}{2},-\frac{5\sqrt{3}}{2}\right)\end{array}$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, exp, cos, sin, Derivative, Matrix, pi, Rational
from sympy import sqrt
from sympy.plotting import plot3d

print('15, 16.')

x, y, z = symbols('x, y, z')
xyz = [x, y, z]
f1 = exp(-2 * x) * cos(y * z)
df1 = Matrix([Derivative(f1, o, 1).doit() for o in xyz])
p1 = {x: 1, y: pi, z: pi}
f2 = exp(3 * x + y) * sin(5 * z)
df2 = Matrix([Derivative(f2, o, 1).doit() for o in xyz])
p2 = {x: 0, y: 0, z: pi / 6}

def test_15(self):
self.assertEqual(
df1.subs(p1),
Matrix([-2 * exp(-2) * cos(pi ** 2),
-pi * exp(-2) * sin(pi ** 2),
-pi * exp(-2) * sin(pi ** 2)])
)

def test_16(self):
self.assertEqual(
df2.subs(p2),
Matrix([Rational(3, 2), Rational(1, 2), - 5 * sqrt(3) / 2])
)

for i, f in enumerate([f1, f2], 15):
for j in range(5):
try:
p = plot3d(f.subs({z: j - 2}), show=False)
p.save(f'sample{i}_{j}.png')
except:
pass

p.show()

if __name__ == "__main__":
main()


% ./sample15.py -v
15, 16.