## 2020年4月2日木曜日

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

1. $\begin{array}{l}grad\mathrm{cos}\left(xy\right)\\ =\left(-y\mathrm{sin}\left(xy\right),-x\mathrm{sin}\left(xy\right)\right)\\ gradf\left(1,\pi ,\pi \right)\\ =\left(-\pi \mathrm{sin}\pi ,-\pi \mathrm{sin}\pi \right)\\ =\left(0,0\right)\end{array}$

2. $\begin{array}{l}grad\mathrm{sin}\left(xyz\right)\\ =\left(yz\mathrm{cos}\left(xyz\right),xz\mathrm{cos}\left(xyz\right),xy\mathrm{cos}\left(xyz\right)\right)\\ gradf\left(1,\pi ,\pi \right)\\ =\left({\pi }^{2}\mathrm{cos}\left({\pi }^{2}\right),\pi \mathrm{cos}\left({\pi }^{2}\right),\pi \mathrm{cos}\left({\pi }^{2}\right)\right)\end{array}$

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

コード

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

print('12.')

x, y, z = symbols('x, y, z', real=True)
fs = [cos(x * y),
sin(x * y * z),
x ** 2 * sin(y * z)]

xyzs = [[Derivative(f, o, 1).doit() for o in [x, y, z]]
for f in fs]

p = {x: 1, y: pi, z: pi}
[pi ** 2 * cos(pi ** 2),
pi * cos(pi ** 2),
pi * cos(pi ** 2)],
[2 * sin(pi ** 2), pi * cos(pi ** 2), pi * cos(pi ** 2)]]

def test(self):

gs = [sin(x * y), x ** sin(y)]
p0 = plot3d(cos(x * y), show=False)
p0.save('sample12.png')
t = symbols('t')

for i, g in enumerate(gs):
p0 = plot3d(g, show=False)
x0, y0, z0 = grad_fps[i + 1]
p0.append(plot3d_parametric_line(
1 + t * x0, pi + t * y0, pi + t * z0, show=False)[0])
p0.save(f'sample12_{i}.png')
p0.show()

if __name__ == "__main__":
main()


% ./sample12.py -v
12.

----------------------------------------------------------------------
Ran 1 test in 0.008s

OK
%