## 2020年1月12日日曜日

### 数学 - Python - 解析学 - 積分の計算 - 定積分の計算 - 3次以下の整式の定積分

1. 3次以下の整式 f を

$f\left(x\right)=\sum _{k=0}^{3}{c}_{k}{x}^{k}$

とおく。

$\begin{array}{l}{\int }_{a}^{b}f\left(x\right)\mathrm{dx}\\ ={\int }_{a}^{b}\sum _{k=0}^{3}{c}_{k}{x}^{k}\mathrm{dx}\\ =\sum _{k=0}^{3}{\left[\frac{{c}_{k}}{k+1}{x}^{k+1}\right]}_{a}^{b}\\ =\sum _{k=0}^{3}\frac{{c}_{k}}{k+1}\left({b}^{k+1}-{a}^{k+1}\right)\\ ={c}_{0}\left(b-a\right)+\frac{{c}_{1}}{2}\left({b}^{2}-{a}^{2}\right)+\frac{{c}_{2}}{3}\left({b}^{3}-{a}^{3}\right)+\frac{{c}_{3}}{4}\left({b}^{4}-{a}^{4}\right)\\ =\left(b-a\right)\left({c}_{0}+\frac{{c}_{1}}{2}\left(b+a\right)+\frac{{c}_{2}}{3}\left({a}^{2}+ab+{b}^{2}\right)+\frac{{c}_{3}}{4}\left({b}^{2}+{a}^{2}\right)\left(b+a\right)\right)\\ =\frac{b-a}{6}\left(6{c}_{0}+3{c}_{1}\left(b+a\right)+2{c}_{2}\left({a}^{2}+ab+{b}^{2}\right)+\frac{3{c}_{3}}{2}\left({a}^{3}+{a}^{2}b+a{b}^{2}+{b}^{3}\right)\right)\\ f\left(a\right)+4f\left(\frac{a+b}{2}\right)+f\left(b\right)\\ ={c}_{0}+{c}_{1}a+{c}_{2}{a}^{2}+{c}_{3}{a}^{3}\\ +4\left({c}_{0}+{c}_{1}·\frac{a+b}{2}+{c}_{2}·{\left(\frac{a+b}{2}\right)}^{2}+{c}_{3}{\left(\frac{a+b}{2}\right)}^{3}\right)\\ +{c}_{0}+{c}_{1}b+{c}_{2}{b}^{2}+{c}_{3}{b}^{3}\\ =6{c}_{0}+{c}_{1}\left(a+2\left(a+b\right)+b\right)+{c}_{2}\left({a}^{2}+{\left(a+b\right)}^{2}+{b}^{2}\right)+{c}_{3}\left({a}^{3}+\frac{{\left(a+b\right)}^{3}}{2}+{b}^{3}\right)\\ =6{c}_{0}+3{c}_{1}\left(a+b\right)+2{c}_{2}\left({a}^{2}+ab+{b}^{2}\right)\\ +\frac{1}{2}{c}_{3}\left(2{a}^{3}+{a}^{3}+3{a}^{2}b+3a{b}^{2}+{b}^{3}+2{b}^{3}\right)\\ =6{c}_{6}+3{c}_{1}\left(a+b\right)+2{c}_{2}\left({a}^{2}+ab+{b}^{2}\right)+\frac{3}{2}{c}_{3}\left({a}^{3}+{a}^{2}b+a{b}^{2}+{b}^{3}\right)\end{array}$

よって、

${\int }_{a}^{b}f\left(x\right)\mathrm{dx}=\frac{b-a}{6}\left(f\left(a\right)+4f\left(\frac{a+b}{2}\right)+f\left(b\right)\right)$

コード

#!/usr/bin/env python3
from unittest import TestCase, main
from sympy import symbols, Integral

print('4.')

class MyTestCase(TestCase):
def test(self):
x, a, b = symbols('x, a, b')
f = sum([symbols(f'c{i}') * x ** i for i in range(4)])
self.assertEqual(Integral(f, (x, a, b)).doit().expand(),
((b - a) / 6 * (f.subs({x: a}) + 4 * f.subs({x: (a + b) / 2}) + f.subs({x: b}))).expand())

if __name__ == '__main__':
main()


% ./sample4.py -v
4.
test (__main__.MyTestCase) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.091s

OK
%