Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Pythonでの数列の実装 | 集合と級数
Pythonによるデータサイエンスのための数学

bookPythonでの数列の実装

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

Python では、リストや Matplotlib を用いて算術級数や等比級数を効率的に生成、操作、可視化可能。 これらのツールにより、数値パターンのモデル化やその挙動の分析が容易。

算術級数の定義

算術級数は次の式に従う:

def arithmetic_series(n, a, d):
    return [a + i * d for i in range(n)]

ここで:

  • a:初項
  • d:公差
  • n:項数
  • リスト内包表記により**n項**の数列を生成
  • 各項は前の項から d ずつ増加

計算例:

1234
def arithmetic_series(n, a, d): return [a + i * d for i in range(n)] print(arithmetic_series(5, 2, 3)) # Output: [2, 5, 8, 11, 14]
copy

等比数列の定義

等比数列は次の式に従う:

def geometric_series(n, a, r):
    return [a * r**i for i in range(n)]

ここで:

  • a は初項;
  • r は公比(各項は前の項に r掛けることで得られる);
  • n は項数。

計算例:

1234
def geometric_series(n, a, r): return [a * r**i for i in range(n)] print(geometric_series(5, 2, 2)) # Output: [2, 4, 8, 16, 32]
copy

Pythonで数列をプロットする

数列を可視化するために、matplotlibを使ってプロットする。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
import numpy as np import matplotlib.pyplot as plt # Define parameters n = 10 a = 2 d = 3 r = 2 # Series generating functions def arithmetic_series(n, a, d): return [a + i * d for i in range(n)] def geometric_series(n, a, r): return [a * r**i for i in range(n)] # Generate series arith_seq = arithmetic_series(n, a, d) geo_seq = geometric_series(n, a, r) # Generate indices for x-axis x_values = np.arange(1, n + 1) # Create figure plt.figure(figsize=(10, 5)) # Plot Arithmetic Series plt.subplot(1, 2, 1) plt.plot(x_values, arith_seq, 'bo-', label='Arithmetic Series') plt.xlabel("n (Term Number)") plt.ylabel("Value") plt.title("Arithmetic Series: a + (n-1)d") plt.grid(True) plt.legend() # Plot Geometric Series plt.subplot(1, 2, 2) plt.plot(x_values, geo_seq, 'ro-', label='Geometric Series') plt.xlabel("n (Term Number)") plt.ylabel("Value") plt.title("Geometric Series: a * r^n") plt.grid(True) plt.legend() # Show plots plt.tight_layout() plt.show()
copy
question mark

Pythonで等差数列の関数をどのように定義しますか?

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

すべて明確でしたか?

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

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

セクション 2.  5

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 2.  5
some-alt