Using Python for Advanced Mathematical Computations

Function Limits and Important Limits

Python facilitates the computation of mathematical concepts such as limits, derivatives, integrals, and differential equations. This section explores using smybolic mathematics to evaluate limits.

Example 1: Computing Left and Right Limits

Consider the function $ y = \arctan(1/x) $ at $ x = 0 $. We compute its left and right limits:

import matplotlib.pyplot as plt
import numpy as np
import sympy as sp

x = sp.Symbol('x')
expr = sp.atan(1 / x)
left_limit = sp.limit(expr, x, 0, dir='-')
right_limit = sp.limit(expr, x, 0, dir='+')
print(f"Left limit of {expr}: {left_limit}")
print(f"Right limit of {expr}: {right_limit}")

# Plotting the function
x_vals = np.arange(-6, 6, 0.01)
y_vals = np.arctan(1 / x_vals)
plt.plot(x_vals, y_vals)
plt.title('y = arctan(1/x)')
plt.show()

The output shows:

Left limit of atan(1/x): -pi/2
Right limit of atan(1/x): pi/2

Example 2: Validating Two Key Limits

We verify two fundamental limits using Python:

import matplotlib.pyplot as plt
import numpy as np
import sympy as sp

x = sp.Symbol('x')
f1 = sp.sin(x) / x
f2 = (1 + 1 / x) ** x
limit1 = sp.limit(f1, x, 0)
limit2 = sp.limit(f2, x, 'oo')
print(f"First limit {f1}: {limit1}")
print(f"Second limit {f2}: {limit2}")

# Plotting for visual confirmation
x1 = np.arange(-3, 3, 0.01)
x2 = np.arange(0.01, 100, 0.1)
y1 = np.sin(x1) / x1
y2 = (1 + 1 / x2) ** x2

plt.figure(figsize=(12, 5))
plt.subplot(121)
plt.plot(x1, y1)
plt.title('y = sin(x)/x')
plt.subplot(122)
plt.plot(x2, y2)
plt.title('y = (1+1/x)^x')
plt.show()

Results:

sin(x)/x First limit: 1
(1 + 1/x)**x Second limit: E

Derivatives and Differentiation

Example 1: Derivative and Tangent Line

Compute the derivative of $ f(x) = 2x^3 + 3x^2 - 12x + 7 $ and find the tangent line at $ x = -1 $:

import matplotlib.pyplot as plt
import numpy as np
import sympy as sp

x = sp.Symbol('x')
f = 2 * x ** 3 + 3 * x ** 2 - 12 * x + 7
f_prime = sp.diff(f)
print(f"Derivative of {f}: {f_prime}")

slope = f_prime.subs(x, -1)
y_value = f.subs(x, -1)
tangent_line = slope * (x + 1) + y_value
print(f"Tangent line equation: {tangent_line}")

# Plotting function and tangent
x_vals = np.arange(-4, 3, 0.01)
y_func = 2 * x_vals ** 3 + 3 * x_vals ** 2 - 12 * x_vals + 7
y_tangent = 8 - 12 * x_vals
plt.plot(x_vals, y_func, x_vals, y_tangent)
plt.title('Function and Tangent Line')
plt.show()

Output:

Derivative of 2*x**3 + 3*x**2 - 12*x + 7: 6*x**2 + 6*x - 12
Tangent line equation: 8 - 12*x

Example 2: Higher Order Derivatives

Calculate up to the 11th derivative of $ y = x^{10} + 2(x - 10)^9 $:

import sympy as sp

x = sp.Symbol('x')
y = x ** 10 + 2 * (x - 10) ** 9
for n in range(1, 12):
    y = sp.diff(y)
    print(f"{n}th derivative: {y}")

Example 3: Implicit Differentiation

Find the derivative of an implicit function defined by $ 2x^2 - 2xy + y^2 + x + 2y + 1 = 0 $:

import sympy as sp

x, y = sp.symbols('x y')
z = 2 * x ** 2 - 2 * x * y + y ** 2 + x + 2 * y + 1
implicit_derivative = -sp.diff(z, x) / sp.diff(z, y)
print(f"Implicit derivative: {implicit_derivative}")

Result:

Implicit derivative: (-4*x + 2*y - 1)/(-2*x + 2*y + 2)

Example 4: Parametric Derivatives

For parametric equations $ x = e^t \cos(t) $ and $ y = e^t \sin(t) $, compute the derivative:

import sympy as sp

t = sp.Symbol('t')
x = sp.exp(t) * sp.cos(t)
y = sp.exp(t) * sp.sin(t)
parametric_derivative = sp.diff(y, t) / sp.diff(x, t)
simplified = sp.simplify(parametric_derivative)
print(f"Parametric derivative: {parametric_derivative}")
print(f"Simplified form: {simplified}")

Output:

Parametric derivative: (exp(t)*sin(t) + exp(t)*cos(t))/(-exp(t)*sin(t) + exp(t)*cos(t))
Simplified form: tan(t + pi/4)

Example 5: Partial Derivatives

Compute partial derivatives of $ z = \sin(xy) + \cos^2(xy) $:

import sympy as sp

x, y = sp.symbols('x y')
z = sp.sin(x * y) + (sp.cos(x * y)) ** 2
d1 = sp.diff(z, x)
d2 = sp.diff(z, y)
d3 = sp.diff(z, x, 2)
d4 = sp.diff(sp.diff(z, x), y)
print(f"First partial: {d1}")
print(f"Second partial: {d2}")
print(f"Third partial: {d3}")
print(f"Fourth partial: {d4}")

Integration Calculations

Example 1: Definite and Indefinite Integrals

Evaluate $ \int \sqrt{4 - x^2} dx $ and $ \int_1^2 \sqrt{4 - x^2} dx $:

import sympy as sp

x = sp.Symbol('x')
y = sp.sqrt(4 - x ** 2)
indef_integral = sp.integrate(y, x)
def_integral = sp.integrate(y, (x, 1, 2))
print(f"Indefinite integral: {indef_integral}")
print(f"Definite integral: {def_integral}")

Output:

Indefinite integral: x*sqrt(4 - x**2)/2 + 2*asin(x/2)
Definite integral: -sqrt(3)/2 + 2*pi/3

Example 2: Triple Integral

Compute the triple integral over a region bounded by $ z = \sqrt{2 - x^2 - y^2} $ and $ z = \sqrt{x^2 + y^2} $:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

# Plotting surfaces
x = np.arange(-1, 1, 0.05)
y = np.arange(-1, 1, 0.05)
x, y = np.meshgrid(x, y)
z1 = np.sqrt(x ** 2 + y ** 2)
z2 = np.sqrt(2 - x ** 2 - y ** 2)
ax = Axes3D(plt.figure())
ax.plot_surface(x, y, z1)
ax.plot_surface(x, y, z2)
plt.show()

# Converting to cylindrical coordinates
import sympy as sp

r, s, z = sp.symbols('r s z')
f = (r ** 2 + z) * r
integral_result = sp.integrate(
    sp.integrate(sp.integrate(f, (z, r, sp.sqrt(2 - r ** 2))), (r, 0, 1)), 
    (s, 0, 2 * sp.pi)
)
print(f"Triple integral result: {integral_result}")

Differential Equations

Example 1: General Solution

Solve the differential equation $ y' + 2xy = xe^{-x^2} $:

import sympy as sp

x = sp.Symbol('x')
f = sp.Function('f')
y = f(x)
equation = sp.Eq(y.diff(x) + 2 * x * y, x * sp.exp(-x ** 2))
general_solution = sp.dsolve(equation, y)
print(f"General solution: {general_solution}")

Output:

General solution: Eq(f(x), (C1 + x**2/2)*exp(-x**2))

Example 2: Particular Solution

Find the particular solution of $ xy' + y - e^{-x} = 0 $ with $ y(1) = 2e $:

import sympy as sp

x = sp.Symbol('x')
f = sp.Function('f')
y = f(x)
equation = sp.Eq(x * y.diff(x) + y - sp.exp(-x), 0)
particular_solution = sp.dsolve(equation, y, ics={f(1): 2 * sp.exp(1)})
print(f"Particular solution: {particular_solution}")

Output:

Particular solution: Eq(f(x), ((1 + 2*exp(2))*exp(-1) - exp(-x))/x)

Tags: python Mathematics Calculus SymPy Integration

Posted on Fri, 08 May 2026 08:09:09 +0000 by paul_so40