2019年5月17日金曜日

数学 - Python - 関連しながら変化する世界 - 簡単な関数 - 分数関数・無理関数 - 簡単な分数方程式・分数不等式(直線との交点座標、解)

1. $\begin{array}{l}x+\frac{3}{2}=\frac{1}{x}\\ 2{x}^{2}+3x-2=0\\ \left(2x-1\right)\left(x+2\right)=0\\ x=-2,\frac{1}{2}\end{array}$

よって、求める不等式の解は、

$-2

2. $\begin{array}{l}\frac{5}{x+2}=x-2\\ {x}^{2}-4=5\\ {x}^{2}=9\\ x=±3\\ x\le -3,-2

3. $\begin{array}{l}\frac{x}{x-1}=2x-2\\ x=2\left({x}^{2}-2x+1\right)\\ 2{x}^{2}-5x+2=0\\ \left(2x-1\right)\left(x-2\right)=0\\ x=\frac{1}{2},2\\ \frac{x}{x-1}\\ =\frac{x-1+1}{x-1}\\ =\frac{1}{x-1}+1\\ \frac{1}{2}\le x<1,2\le x\end{array}$

コード

Python 3

#!/usr/bin/env python3
from sympy import pprint, symbols, plot, Rational, Poly
from sympy.solvers.inequalities import reduce_rational_inequalities
print('33.')

x = symbols('x', real=True)
fs = [x + Rational(3, 2), 5 / (x + 2), x / (x - 1)]
gs = [1 / x, x - 2, 2 * x - 2]
inequalities = ['>', '>=', '<=']

for i, (a, b, c) in enumerate(zip(fs, gs, inequalities), 1):
print(f'({i})')
pprint(reduce_rational_inequalities([[(a - b, c)]], x))
print()

p = plot(*fs,
*gs,
(x, -5, 5),
ylim=(-5, 5),
legend=True,
show=False)

colors = ['red', 'green', 'blue', 'brown', 'orange',
'purple', 'gray', 'skyblue', 'yellow']

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

p.show()
p.save('sample33.png')


C:\Users\...>py sample33.py
33.
(1)
(-2 < x ∧ x < 0) ∨ (1/2 < x ∧ x < ∞)

(2)
(x ≤ -3 ∧ -∞ < x) ∨ (x ≤ 3 ∧ -2 < x)

(3)
(1/2 ≤ x ∧ x < 1) ∨ (2 ≤ x ∧ x < ∞)

C:\Users\...>