## 2020年4月8日水曜日

### 数学 - Python - 解析学 - 多変数の関数 - 多変数の関数 - 微分可能性と勾配 - 偏微分

1. $\begin{array}{l}\frac{\partial }{\partial x}f\left(x,y\right)\\ =\frac{\partial }{\partial x}\left(2x-3y\right)\\ =2\\ \frac{\partial }{\partial y}\left(2x-3y\right)\\ =-3\end{array}$

コード

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

print('1.')

x, y = symbols('x, y')
f = 2 * x - 3 * y
fx = 2
fy = -3

class TestPartialDerivative(TestCase):
def test_dx(self):
self.assertEqual(Derivative(f, x, 1).doit(), fx)

def test_dy(self):
self.assertEqual(Derivative(f, y, 1).doit(), fy)

p = plot3d(f, show=False)
t = symbols('t')

for _ in range(10):
px = random.randrange(-5, 6)
py = random.randrange(-5, 6)
x0 = px + t * fx
y0 = px + t * fy
p.append(plot3d_parametric_line(x0, y0, f.subs({x: x0, y: y0}),
legend=True,
show=False)[0])
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

for o, color in zip(p, colors):
o.line_color = color

p.show()
p.save('sample1.png')

if __name__ == "__main__":
main()


% ./sample1.py -v
1.
test_dx (__main__.TestPartialDerivative) ... ok
test_dy (__main__.TestPartialDerivative) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK
%