学習環境
- Surface
- Windows 10 Pro (OS)
- Nebo(Windows アプリ)
- iPad
- MyScript Nebo - MyScript(iPad アプリ(iOS))
- 参考書籍
解析入門(上) (松坂和夫 数学入門シリーズ 4) (松坂 和夫(著)、岩波書店)の第8章(積分の計算)、8.2(定積分の計算)、問題1の解答を求めてみる。
とおくと、
コード
#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, symbols, sqrt, sin, cos, asin, exp, Integral, pi, Rational, oo, plot
print('1.')
x = symbols('x')
a = symbols('a', positive=True)
b = symbols('b', nonzero=True)
fs = [sqrt(a ** 2 - x ** 2),
1 / sqrt(a ** 2 - x ** 2),
1 / (1 + x + x ** 2),
1 / (1 + x ** 2) ** 2,
1 / (1 + Rational(1, 2) * cos(x)),
x * asin(x) / sqrt(1 - x ** 2),
exp(-1 * 1 * x) * sin(2 * x)]
class MyTestCase(TestCase):
def test1(self):
self.assertEqual(Integral(fs[0], (x, 0, a)).doit(), pi * a ** 2 / 4)
def test2(self):
a = symbols('a', positive=True)
self.assertEqual(Integral(fs[1], (x, 0, a)).doit(), pi / 2)
def test3(self):
self.assertEqual(Integral(fs[2], (x, 0, 1)).doit(), pi / (3 * sqrt(3)))
def test4(self):
self.assertEqual(
Integral(fs[3], (x, -1, 1)).doit(), Rational(1, 2) + pi / 4)
def test5(self):
d = {a: Rational(1, 2)}
self.assertEqual(
Integral(fs[4].subs(d), (x, 0, pi)).doit(), (pi / sqrt(1 - a ** 2)).subs(d))
def test6(self):
self.assertEqual(Integral(fs[5], (x, -1, 1)).doit(), 2)
def test7(self):
d = {a: 1, b: 2}
self.assertEqual(
Integral(fs[6].subs(d), (x, 0, oo)).doit(
), (b / (a ** 2 + b ** 2)).subs(d))
p = plot(*[f.subs({a: 1, b: 2}) for f in fs],
(x, -5, 5),
ylim=(-5, 5),
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.show()
p.save('sample1.png')
if __name__ == '__main__':
main()
入出力結果(Zsh、PowerShell、Terminal、Jupyter(IPython))
% ./sample1.py -v
1.
test1 (__main__.MyTestCase) ... ok
test2 (__main__.MyTestCase) ... ok
test3 (__main__.MyTestCase) ... ok
test4 (__main__.MyTestCase) ... ok
test5 (__main__.MyTestCase) ... ok
test6 (__main__.MyTestCase) ... ok
test7 (__main__.MyTestCase) ... ok
----------------------------------------------------------------------
Ran 7 tests in 2.425s
OK
%
0 コメント:
コメントを投稿