SoFunction
Updated on 2025-03-02

Example of calculating indefinite integrals using python

In Python, calculating indefinite integrals (i.e., original function or anti-derivative) can be implemented through the SymPy library. SymPy is a Python library for symbolic mathematics, supporting many types of mathematical objects, including integers, rational numbers, real numbers, complex numbers, functions, limits, integrals, differentials, equations, geometry, etc.

1. Example 1: Use the SymPy library to calculate indefinite integrals

Here is a detailed example of using the SymPy library to calculate indefinite integrals. We will calculate the indefinite integral of a common function ∫(x2+3x+2)d**x.

First, make sure we have the SymPy library installed. If you haven't installed it, you can install it through pip:

pip install sympy

We can then use the following Python code to calculate this indefinite integral:

# Import symbol variables and integral functions in the SymPy libraryfrom sympy import symbols, integrate  
# Define variable xx = symbols('x')  
# Define function f(x) = x^2 + 3x + 2f = x**2 + 3*x + 2  
# Calculate indefinite integrals# integrate(function, variable)indefinite_integral = integrate(f, x)  
# Print the resultsprint("Indefinite Points Results:", indefinite_integral)

After running the above code, we will get the output:

Indefinite integral result: x**3/3 + 3*x**2/2 + 2*x

This result indicates that the indefinite integral of the function x2+3x+2 is 3x3+23x2+2x, where the constant term (integral constant) is omitted because the indefinite integral usually does not include the integral constant.

Extended application

SymPy can not only be used to calculate simple indefinite integrals, but also handle more complex symbolic expressions and equations. For example, we can use it to solve differential equations, perform symbolic simplification, perform matrix operations, etc.

Things to note

(1) When using SymPy, make sure our expressions and variables are symbolic types.

(2) The constant term (integral constant) in the integral result is usually omitted in indefinite integrals because indefinite integrals represent a type of function, not a specific function value.

(3) For fixed integrals (i.e. points given upper and lower limits of a certain integral), SymPy also providesintegratefunction, but we need to specify additional integral intervals.

2. Example 2: Calculate the indefinite integral of the basic polynomial function

# Import the SymPy libraryfrom sympy import symbols, integrate, Expr  
# Define variablesx = symbols('x')  
# Define polynomial functionsf = x**2 + 3*x + 2  
# Calculate indefinite integralsindefinite_integral = integrate(f, x)  
# Print the resultsprint("Indefinite Points Results:", indefinite_integral)

3. Example 3: Calculate the indefinite integral that includes exponential functions and trigonometric functions

# Import the SymPy libraryfrom sympy import symbols, integrate, sin, exp  
# Define variablesx = symbols('x')  
# Define a function that contains exponential functions and trigonometric functionsf = exp(x) * sin(x)  
# Calculate indefinite integralsindefinite_integral = integrate(f, x)  
# Print the results# Note: The result of this integral is a special function, and SymPy will give an accurate expressionprint("Indefinite Points Results:", indefinite_integral)

4. Example 4: Calculate indefinite integral using the exchange integral method

Sometimes, it can be difficult to directly earn points, but the problem can be simplified by changing the amount. However, for complex element swaps, SymPy may not be automatically performed. But we can do the exchange manually and show how to deal with this situation. However, for simple cases, SymPy can usually automatically recognize and apply meta changes. Here we show a direct integrable example, but illustrate the idea of ​​changing elements.

Suppose we want to calculate ∫1−x2d**x, this can be solved by letting x=sin(u) exchange. But in this example, we directly let SymPy calculate it.

# Import the SymPy libraryfrom sympy import symbols, integrate, sqrt  
# Define variablesx = symbols('x')  
# Define functionsf = sqrt(1 - x**2)  
# Calculate indefinite integrals# Note: This integral is actually part of the area function of the semicircle, and SymPy will give an accurate expressionindefinite_integral = integrate(f, x)  
# Print the resultsprint("Indefinite Points Results:", indefinite_integral)

For complex cases where the need to manually replace elements, we usually need to define new variables, replace parts of the original function with expressions, and adjust the integral limit accordingly (for fixed integrals). But in the case of indefinite integrals, we mainly focus on the expression itself, and SymPy'sintegrateFunctions are usually powerful enough to handle many cases where elements need to be replaced.

5. Example 5: Calculate the indefinite integral of a rational function

A rational function is a ratio of polynomial functions. SymPy can handle the integral of many rational functions.

# Import the SymPy libraryfrom sympy import symbols, integrate  
# Define variablesx = symbols('x')  
# Define rational functionsf = (x**2 + 1) / (x**3 + x)  
# Calculate indefinite integralsindefinite_integral = integrate(f, x)  
# Print the results# Note: The result may contain logarithmic functions or inverse trigonometric functionsprint("Indefinite Points Results:", indefinite_integral)

These examples show how to calculate indefinite integrals of different types of functions in Python using the SymPy library. In practical applications, we can adjust functions and variables as needed.

This is the end of this article about how to calculate indefinite points using python. For more related content on python calculation of indefinite points, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!