## 2020年5月11日月曜日

### 数学 - Python - 立体的な広がりの中の図形 - 空間図形 - 空間のベクトル - ベクトルの内積 - ベクトルのなす角、三角関数(余弦)

1. $\begin{array}{l}\mathrm{cos}\theta \\ =\frac{\stackrel{\to }{a}·\stackrel{\to }{b}}{\left|\stackrel{\to }{a}\right|\left|\stackrel{\to }{b}\right|}\\ =\frac{\left(-1,0,1\right)·\left(-1,2,2\right)}{\left|\left(-1,0,1\right)\right|\left|\left(-1,2,2\right)\right|}\\ =\frac{1+2}{\sqrt{1+1}\sqrt{1+4+4}}\\ =\frac{3}{3\sqrt{2}}\\ =\frac{1}{\sqrt{2}}\end{array}$

よって求める問題の2つのベクトルのなす角は

$\frac{\pi }{4}$

2. $\begin{array}{l}\frac{4+2-6}{\sqrt{1+4+9}\sqrt{16+1+4}}=0\\ \frac{\pi }{2}\end{array}$

コード

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

print('13.')

def f(a, b):
theta = symbols('θ')
return solveset(a.dot(b) - a.norm() * b.norm() * cos(theta),
domain=Interval(0, pi))

a1 = Matrix([-1, 0, 1])
b1 = Matrix([-1, 2, 2])
a2 = Matrix([1, 2, -3])
b2 = Matrix([4, 1, 2])

class TestVector(TestCase):
def test1(self):
self.assertEqual(f(a1, b1), {pi / 4})

def test2(self):
self.assertEqual(f(a2, b2), {pi / 2})

t = symbols('t')
p = plot3d_parametric_line(*[(*t * o, (t, -1, 1))
for o in [a2, b2]],
xlim=(-2, 2),
ylim=(-2, 2),
legend=True,
show=False)
colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

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

if __name__ == "__main__":
main()


% ./sample13.py -v
13.
test1 (__main__.TestVector) ... ok
test2 (__main__.TestVector) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.470s

OK
%