Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Pythonによる有理関数の実装 | 関数とその性質
Pythonによるデータサイエンスのための数学

bookPythonによる有理関数の実装

メニューを表示するにはスワイプしてください

これまでの関数とは異なり、有理関数をPythonでプロットする際には特別な注意が必要です。未定義点や無限大となる値が存在するため、定義域を分割してエラーを防ぐ必要があります。

1. 関数の定義

有理関数の定義:

def rational_function(x):
    return 1 / (x - 1)

主な注意点:

  • x=1x = 1 はゼロ除算を避けるため計算から除外する必要があります;
  • 関数は x=1x = 1 の左側と右側で2つの定義域に分割されます。

2. 定義域の分割

ゼロ除算を回避するため、2つの異なるx値の集合を生成。

x_left = np.linspace(-4, 0.99, 250)  # Left of x = 1
x_right = np.linspace(1.01, 4, 250)  # Right of x = 1

0.99 および 1.01x=1x = 1 を含まないように設定されており、エラーを防止。

3. 関数のプロット

plt.plot(x_left, y_left, color='blue', linewidth=2, label=r"$f(x) = \frac{1}{x - 1}$")
plt.plot(x_right, y_right, color='blue', linewidth=2)

関数は x=1x = 1ジャンプするため、2つの部分に分けてプロットする必要があります。

4. 垂直・水平漸近線および切片の表示

  • 垂直漸近線 (x=1x = 1):
plt.axvline(1, color='red', linestyle='--',
            linewidth=1, label="Vertical Asymptote (x=1)")
  • 水平漸近線 (y=0y = 0):
plt.axhline(0, color='green', linestyle='--', 
            linewidth=1, label="Horizontal Asymptote (y=0)")
  • x=0x = 0 におけるY切片:
y_intercept = rational_function(0)
plt.scatter(0, y_intercept, color='purple', label="Y-Intercept")

5. 方向矢印の追加

関数が無限に続くことを示すために:

plt.annotate('', xy=(x_right[-1], y_right[-1]), xytext=(x_right[-2], y_right[-2]), arrowprops=dict(arrowstyle='->', color='blue', linewidth=1.5))
question mark

f(x)=1x1f(x) = \frac{\raisebox{1pt}{$1$}}{\raisebox{-1pt}{$x-1$}} を定義し、ゼロ除算を回避しながらグラフを描画する正しいコードはどれですか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  6

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  6
some-alt