## 2020年4月3日金曜日

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

1. $\begin{array}{l}gradf\left(x,y,z\right)\\ =grad\mathrm{log}\left(z+\mathrm{sin}\left({y}^{2}-x\right)\right)\\ =\left(\frac{-\mathrm{cos}\left({y}^{2}-x\right)}{z+\mathrm{sin}\left({y}^{2}-x\right)},\frac{2y\mathrm{cos}\left({y}^{2}-x\right)}{z+\mathrm{sin}\left({y}^{2}-x\right)},\frac{1}{z+\mathrm{sin}\left({y}^{2}-x\right)}\right)\\ gradf\left(P\right)\\ =gradf\left(1,-1,1\right)\\ =\left(-1,-2,1\right)\end{array}$

コード

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

print('13.')

x, y, z = symbols('x, y, z', real=True)
f = log(z + sin(y ** 2 - x))
p = {x: 1, y: -1, z: 1}
xyz = Matrix([Derivative(f, o, 1).doit() for o in [x, y, z]])

def test(self):
self.assertEqual(xyz.subs(p), Matrix([-1, -2, 1]))

t = symbols('t')

for i, z0 in enumerate(list(range(-2, 3)), 1):
d = {z: z0}
p0 = plot3d(f.subs(d), show=False)
p0.append(plot3d_parametric_line(1 - t, -1 - 2 * t, 1 + t, show=False)[0])
p0.save(f'sample13_{i}.png')
p0.show()

if __name__ == "__main__":
main()


% ./sample13.py -v
13.