LoginSignup
4
5

More than 3 years have passed since last update.

高校数学の「微分」関連の問題をPythonで解く

Posted at

概要

 関数の微分・導関数に関する問題(高校数学)を Python で解いていきます。

問1

次の関数を微分せよ。

$$ y= (x^2-1)(2x^2 +x-3) $$

問1の解き方(参考)

\begin{align}
y' & = \{ (x^2-1)(2x^2 +x-3) \}' \\
& =  (x^2-1)'(2x^2 +x-3)  +  (x^2-1)(2x^2 +x-3)' \\
& = 2x(2x^2 +x-3) + (x^2-1)(4x+1) \\
& = 8x^3+3x^2-10x-1
\end{align}

問1の解を与えるPythonプログラム

sympy を利用して解きます。sympy は、記号計算(数式処理)のパッケージです。

diff で微分、expand で展開(括弧をとる)をしています。

Python
from sympy import diff, expand, Symbol
x = Symbol('x')
fx = (x**2-1)*(2*x**2+x-3)
ans = expand(diff(fx, x))
print(f'解:{ans}')

実行結果

解:8*x**3 + 3*x**2 - 10*x - 1

見づらいのですが数式表記すると $8x^3+3x^2-10x-1$ になります。環境によっては、あらかじめ init_printing() を書いておくことで見やすい形式で実行結果の出力が得られます。

ちなみに、expand を適用せずに ans = diff(fx, x) として解を表示すると次のようになります。

解:2*x*(2*x**2 + x - 3) + (4*x + 1)*(x**2 - 1)

$2x(2x^2 +x-3) + (4x+1)(x^2-1)$ のような形になります。

問2

次の関数を微分せよ。

$$ y= (x+1)^2(2x-3)^4 $$

問2の解き方(参考)

\begin{align}
y' & = \{ (x+1)^2(2x-3)^4 \}' \\
& = \{(x+1)^2\}'(2x-3)^4  +  (x+1)^2\{(2x-3)^4\}'  \\
& = 2(x+1)(x+1)'\cdot (2x-3)^4  +  (x+1)^2\cdot 4(2x-3)^3(2x-3)'  \\
& = 2(x+1)(2x-3)^4  +  8(x+1)^2(2x-3)^3  \\
& = 2(x+1)(2x-3)^3(6x+1)
\end{align}

問2の解を与えるPythonプログラム

上記に示したように、きれいにまとめるために factor で因数分解をしています。

Python
from sympy import diff, expand, factor, Symbol
x = Symbol('x')
fx = ((x+1)**2)*((2*x-3)**4)
ans = factor(diff(fx, x))
print(f'解:{ans}')

実行結果

解:2*(x + 1)*(2*x - 3)**3*(6*x + 1)

すこし見ずらいですが、$2(x+1)(2x-3)^3(6x+1)$ が求まっています。

もし、因数分解しないで展開した形で解が欲しい場合は、ans =expand(diff(fx, x)) とします。そのときの実行結果は次のようになります。

解:96*x**5 - 320*x**4 + 160*x**3 + 360*x**2 - 270*x - 54

$96x^5-320x^4+160x^3+360x^2-270x-54$ となります。

4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5