← Back to list

Using Python for Calculus

Engineers use calculus every day to design parts that can handle real-world forces and stresses. It helps them predict how materials will…

Mason Shearin · 2025-11-06 03:10 · 6 claps · 4.7 min read
#python #python-programming #sympy #calculus #engineering
Open on Medium ↗
Wiki topics: 💻 · Programming 📐 · Mathematics 🧠 · Mental Wellness

Using Python for Calculus

Engineers use calculus every day to design parts that can handle real-world forces and stresses. It helps them predict how materials will behave and make sure components are strong, stable, and reliable over time. But doing all those calculations by hand can take a lot of time and leave room for mistakes. That’s where Python comes in. With tools like SymPy, Python can handle complex calculus problems quickly and accurately, saving engineers time while improving precision. By letting the computer do the heavy lifting, engineers can focus more on creativity, problem-solving, and building better designs.

Setup

First, you need to install SymPy, which is very easy to do. You just need to run this script, and it will be installed into the Kernel.

#script for installing SymPy
!pip install sympy 

Requirement already satisfied: sympy in c:\users\mason\anaconda3\lib\site-packages (1.13.3)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\users\mason\anaconda3\lib\site-packages (from sympy) (1.3.0)

Afterwards, you need to import SymPy into your code to use its functions. In this case, I imported SymPy as the variable sym. Then, I set the variables I will be using to x, y, and z, as found on a graph.

#Importing SymPy
import sympy as sym
#Declaring the variables we are using in the equations
x, y, z, = sym.symbols('x y z')

After defining the variables, I created an expression to take the derivative of. In engineering, expressions like these often come from part simulations, which help us understand how materials respond to different forces. By analyzing the derivative, we can determine how quickly stresses or loads change — information that’s key to designing parts strong enough to perform safely and efficiently in their intended applications.

# expression of which we have to find derivative
exp = x**3 * y + y**3 + z

First Derivative

With SymPy, we can efficiently compute the derivative of an expression using only a few lines of code. This makes it easy to analyze how one variable changes in relation to another — an essential part of understanding relationships such as force, stress, or motion within an engineering system. By automating this process, SymPy helps engineers perform precise calculations more quickly and focus on interpreting results rather than performing repetitive math.

# Differentiating exp with respect to x
derivative1_x = sym.diff(exp, x)
print('derivative w.r.t x: ',
      derivative1_x)
# Differentiating exp with respect to y
derivative1_y = sym.diff(exp, y)
print('derivative w.r.t y: ', 
      derivative1_y)

The result of this expression is:

derivative w.r.t x:  3*x**2*y
derivative w.r.t y:  x**3 + 3*y**2

This means that the derivative of x is: 3x²y and the derivative of y is: x³ + 3y².

Second Derivative

SymPy can also compute higher-order derivatives, such as the second derivative of a function. Using the same expression from before, we can easily obtain the second derivative with just a simple line of code. This is especially useful in engineering applications, where the second derivative often represents important physical quantities like acceleration or the rate of change in stress, helping engineers better understand system behavior and performance.

# Finding second derivative 
# of exp with respect to x
derivative2_x = sym.diff(exp, x, 2)
print('second derivative w.r.t. x: ', 
      derivative2_x)

# Finding second derivative 
# of exp with respect to y
derivative2_y = sym.diff(exp, y, 2)
print('second derivative w.r.t. y: ', 
      derivative2_y)

The result of taking the second derivative is:

second derivative w.r.t. x:  6*x*y
second derivative w.r.t. y:  6*y

This means the second derivative of x is: 6xy, and the second derivative of y is: 6y.

Integrals

In engineering, differentiation is only part of the process — integrating functions is equally important for analyzing quantities such as total force, energy, or displacement. SymPy provides a straightforward way to perform these integrations with just a few lines of code. It can handle both definite and indefinite integrals, including those with infinite limits (where oo in SymPy represents infinity). This capability allows engineers to apply advanced calculus directly within Python, improving both efficiency and accuracy in their analyses.

integral1 = sym.integrate(sym.cos(x), x)
print('indefinite integral of cos(x): ',
      integral1)

integral2 = sym.integrate(sym.cos(x), (x, -1, 1))
print('definite integral of cos(x) between -1 to 1: ',
      integral2)

integral3 = sym.integrate(sym.exp(-x), (x, 0, sym.oo))
print('definite integral of exp(-x) between 0 to ∞: ',
      integral3)

The results of these expressions are:

indefinite integral of cos(x):  sin(x)
definite integral of cos(x) between -1 to 1:  2*sin(1)
definite integral of exp(-x) between 0 to ∞:  1

Limits

SymPy can also be used to calculate the limits of functions using the limit(function, variable, point) syntax. This feature helps analyze the behavior of functions as they approach specific values or boundaries. In this example, I’ll first find the limit of f(x) = x as x approaches infinity, then the limit of f(x) = 1/x as x approaches infinity, and finally the limit of f(x) = sin(x)/x as x approaches zero. These examples demonstrate how SymPy simplifies evaluating limits, a key concept in understanding continuity and behavior in engineering models.

# Calculating limit of f(x) = x as x->∞
limit1 = sym.limit(x, x, sym.oo)
print(limit1)

# Calculating limit of f(x) = 1/x as x->∞
limit2 = sym.limit(1/x, x, sym.oo)
print(limit2)

# Calculating limit of f(x) = sin(x)/x as x->0
limit3 = sym.limit(sym.sin(x)/x, x, 0)
print(limit3)

The results of these limits are:

oo
0
1

This means that the limit of the first function is infinity, the limit of the second is zero, and the limit of the third is one.

Taylor Series

Finally, SymPy can also be used to compute Taylor Series expansions of functions around a specific point. This is done using the series(f, x, x0, n) function. If the parameters x₀ (the expansion point) and n (the number of terms) are omitted, SymPy defaults to x₀ = 0 and n = 6. This feature allows engineers to approximate complex functions with polynomials, making it easier to analyze system behavior near a given point. For example:

# assign series
series1 = sym.series(sym.cos(x), x)
print(series1)

# assign series
series2 = sym.series(1/sym.cos(x), x, 0, 4)
print(series2)

The output of this code is:

1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)

This means that the result of the first series is 1 — x²/2 + x⁴/24 + 0x⁶, and the result of the second series is 1 + x²/2 + 0x⁴. Also, note that all x terms with a power greater than or equal to x⁴ or x⁶ are omitted.

In conclusion, calculus is essential to engineering, but performing complex calculations manually can be time-consuming and error-prone. By using Python’s SymPy library, engineers can automate differentiation, integration, and equation solving with high accuracy and speed. This not only streamlines the design and analysis process but also allows engineers to focus on innovation and optimization rather than tedious computation. Python, therefore, serves as a powerful tool to enhance precision, efficiency, and productivity in engineering work.

Works Cited

[embed]How to Do Calculus with Python ? - GeeksforGeeks Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across…www.geeksforgeeks.org

[embed]"Efficiency Revolution: 5 Calculus Mastery Strategies for Industrial Engineering" Discover the vital role of calculus in industrial engineering, from optimization to dynamic modeling. Explore how math…aiiem.org


메타데이터
post_id
0aff11decf2e
slug
using-python-for-calculus-0aff11decf2e
url
https://medium.com/@masonshearin257/using-python-for-calculus-0aff11decf2e
canonical_url
https://medium.com/@masonshearin257/using-python-for-calculus-0aff11decf2e
author_url
https://medium.com/@masonshearin257
status
ok
fetched_at
2026-07-15 16:27:25