LoginSignup
3
5

More than 3 years have passed since last update.

高校数学の「高次方程式・恒等式」関連の問題をPythonで解く

Last updated at Posted at 2019-07-25

概要

 高次方程式に関する問題(高校数学)を Sympy で解いていきます。練習問題と解答例の作成支援を目的としています(教員向けものです)。

問1

$f(x)=x^3-1$ について、次の問いに答えよ。

(1) $f(2)$ の値を求めよ。
(2) $f(1)$ の値を求めよ。
(3) $f(x)=x^3-1$ を因数分解せよ。
(4) 方程式 $x^3-1=0$ の解を全て求めよ。

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

sympy を使用して解決していきます。

Python
import sympy

x = sympy.Symbol('x')

fx = x**3 - 1

print(f'(1)の解答  ', end='' )
print(f'f(2)={fx.subs(x,2)}')

print(f'(2)の解答  ', end='' )
print(f'f(1)={fx.subs(x,1)}')

print(f'(3)の解答  ', end='' )
print(f'f(x)={sympy.factor(fx)}')

print(f'(4)の解答  ', end='' )
sol = sympy.solve(fx)
print(f'{sol}')
実行結果
(1)の解答  f(2)=7
(2)の解答  f(1)=0
(3)の解答  f(x)=(x - 1)*(x**2 + x + 1)
(4)の解答  [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]

問2

(1) $x^4-10x^3+35x^2-50x+24=0$ の解を全て求めよ。
(2) $x^4-10x^2+9=0$ の解を全て求めよ。

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

Python
import sympy

x = sympy.Symbol('x')

fx = x**4 - 10*x**3 + 35*x**2 -50*x + 24
print(f'(1)の解答  ')
print(f'因数分解すると {sympy.factor(fx)}=0 となり、')
ans = ', '.join(list(map(str, sympy.solve(fx))))
print(f'これより、x = {ans} となる。')

fx = x**4 - 10*x**2 + 9
print(f'')
print(f'(2)の解答  ')
print(f'因数分解すると {sympy.factor(fx)}=0 となり、')
ans = ', '.join(list(map(str, sympy.solve(fx))))
print(f'これより、x = {ans} となる。')
実行結果
(1)の解答  
因数分解すると (x - 4)*(x - 3)*(x - 2)*(x - 1)=0 となり、
これより、x = 1, 2, 3, 4 となる。

(2)の解答  
因数分解すると (x - 3)*(x - 1)*(x + 1)*(x + 3)=0 となり、
これより、x = -3, -1, 1, 3 となる。

問3

(1) 次の等式が全ての $x$ に対して成り立つように定数 $a$、$b$、$c$ の値を求めよ。

$$ x^2 = a(x-2)^2 + b(x-2) + c $$

(2) 次の等式が全ての $x$ に対して成り立つように定数 $a$、$b$ の値を求めよ。

$$ \frac{3x-4}{(x+2)(x-3)} = \frac{a}{x+2} + \frac{b}{x-3} $$

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

(2) は部分分数分解です。

Python
import sympy

x, a, b, c = sympy.symbols('x a b c')

print(f'(1)の解答   ',end='')
fx1 = x**2
fx2 = a*(x-2)**2 + b*(x-2) + c
sol = sympy.solve( fx1 - fx2, [a, b, c])
ans = ', '.join(list(map(lambda x: '{0}={1}'.format(x,sol[x]), sol.keys())))
print(ans)

print(f'(2)の解答   ',end='')
fx1 = (3*x-4)/((x+2)*(x-3))
fx2 = a/(x+2)+b/(x-3)
sol = sympy.solve( fx1 - fx2, [a, b])
ans = ', '.join(list(map(lambda x: '{0}={1}'.format(x,sol[x]), sol.keys())))
print(ans)
実行結果
(1)の解答   a=1, b=4, c=4
(2)の解答   a=2, b=1

問4

次の方程式を満たす $x$ の値を求めよ。

(1) $\frac{3}{x+2}+\frac{3}{x-2}=2$
(2) $\frac{3}{x+2}+\frac{3}{x-2}=0$
(3) $\sqrt{x+3}=x+1$
(4) $-\sqrt{x+3}=x+1$

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

Python
import sympy

x = sympy.Symbol('x')

fx1 = 3/(x+2) + 3/(x-2)
fx2 = 2
ans = ', '.join(list(map(str, sympy.solve(fx1-fx2))))
print(f'(1)の解答  x = {ans}')

fx1 = 3/(x+2) + 3/(x-2)
fx2 = 0
ans = ', '.join(list(map(str, sympy.solve(fx1-fx2))))
print(f'(2)の解答  x = {ans}')

fx1 = sympy.sqrt(x+3)
fx2 = x+1
ans = ', '.join(list(map(str, sympy.solve(fx1-fx2))))
print(f'(3)の解答  x = {ans}')

fx1 = -sympy.sqrt(x+3)
fx2 = x+1
ans = ', '.join(list(map(str, sympy.solve(fx1-fx2))))
print(f'(4)の解答  x = {ans}') 
実行結果
(1)の解答  x = -1, 4
(2)の解答  x = 0
(3)の解答  x = 1
(4)の解答  x = -2
3
5
0

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
3
5