2019年10月1日火曜日

学習環境

新装版 数学読本2 (松坂 和夫(著)、岩波書店)の第7章(急速・緩慢に変化する関係 - 指数関数・対数関数)、7.3(対数関数の性質)、常用対数の問29の解答を求めてみる。



    1. log 10 4.5 = 0.6532

    2. log 10 52.83 = 1 + log 10 5.283 = 1.7228

    3. log 10 600 = 2 + log 10 6 = 2.7782

    4. log 10 72.35 = 1 + log 10 7.235 = 1.8594

    5. log 10 0.325 = - 1 + 0.5119 = - 0.4881

    6. log 10 0.4857 = - 1 + 0.6863 = - 0.3137

    7. log 10 0.008098 = - 3 + log 10 8.098 = - 3 + 0.9083 = - 2.0917

コード

Python 3

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import pprint, symbols, log, plot
import math

print('29.')

spam = [4.5, 52.83, 600, 72.35, 0.325, 0.4857, 0.008098]
egg = [0.6532, 1.7228, 2.7782, 1.8594, -0.4881, -0.3137, -2.0917]


class MyTestCase(TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_math_log10(self):
        for s, t in zip(spam, egg):
            self.assertAlmostEqual(math.log10(s), t, places=3)

    def test_sympy_log10(self):
        for s, t in zip(spam, egg):
            self.assertAlmostEqual(float(log(s, 10)), t, places=3)


if __name__ == '__main__':
    x = symbols('x')
    p = plot(log(x, 10),
             (x, 0.1, 5.1),
             ylim=(-2.5, 2.5),
             legend=True,
             show=False)
    colors = ['red', 'green', 'blue', 'brown', 'orange',
              'purple', 'pink', 'gray', 'skyblue', 'yellow']

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

    p.show()
    p.save(f'sample29.png')
    main()

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

$ ./sample29.py -v
29.
test_math_log10 (__main__.MyTestCase) ... ok
test_sympy_log10 (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.010s

OK
$ 

0 コメント:

コメントを投稿