Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Implementing Series in Python | Section
Python Math Module Essentials: Trigonometry, Logarithms, and Constants - 1769704232288

Implementing Series in Python

Deslize para mostrar o menu

In Python, you can efficiently generate, manipulate, and visualize arithmetic and geometric series using lists and Matplotlib. These tools make it easy to model numerical patterns and analyze their behavior.

Defining an Arithmetic Series

An arithmetic series follows the formula:

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

Where:

  • a is the first term;
  • d is the common difference;
  • n is the number of terms;
  • A list comprehension generates n terms of the sequence;
  • Each term increases by d from the previous term.

Example Calculation:

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]

Defining a Geometric Series

A geometric series follows the formula:

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

Where:

  • a is the first term;
  • r is the common ratio (each term is multiplied by r from the previous term);
  • n is the number of terms.

Example Calculation:

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]

Plotting the Series in Python

To visualize the sequences, we plot them using 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()
question mark

How do you define an arithmetic series function in Python?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 15

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 15
some-alt