How to Do Calculus with Python ?
Last Updated :
07 Aug, 2024
Calculus is a branch of mathematics focused on limits, functions, derivatives, integrals, and infinite series. We will use SymPy library to do calculus with python. SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.
Installation:
pip install sympy
If we want to write any sympy expression, first we have to declare its symbolic variables. To do this, we can use the following two functions :
- sympy.Symbol(): It is used to declare a single variable by passing the variable as a string into its parameter.
- sympy.symbols(): It is used to declare multivariable by passing the variables as a string into its parameter. All the variables must be separated by a space forming a string.
Differentiation
We can differentiate any sympy expression by using diff(func, var) method. The parameter func denotes the sympy expression to be differentiated and var denotes the variable with respect to which we have to differentiate.
Example 1:
Python
# Importing library
import sympy as sym
# Declaring variables
x, y, z = sym.symbols('x y z')
# expression of which we have to find derivative
exp = x**3 * y + y**3 + z
# 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)
Output:
derivative w.r.t x: 3*x**2*y
derivative w.r.t y: x**3 + 3*y**2
We can also find higher derivatives using the diff(func, var, n) method. Here, the parameter n denotes the nth derivative to be found.
Example 2:
Python
# 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)
Output:
second derivative w.r.t. x: 6*x*y
second derivative w.r.t. y: 6*y
Integration
You can do indefinite and definite integration of transcendental elementary and special functions via integrate() function.
Syntax for indefinite integration: sympy.integrate(func, var)
Syntax for definite integration: sympy.integrate(func, (var, lower_limit, upper_limit))
The parameter func denotes the sympy expression to be differentiated, var denotes the variable with respect to which we have to differentiate, lower_limit denotes to the lower limit of the definite integration and upper_limit denotes the upper limit of the definite integration.
Note: ∞ in SymPy is oo.
Example 1:
Python
# Indefinite integration of cos(x) w.r.t. dx
integral1 = sym.integrate(sym.cos(x), x)
print('indefinite integral of cos(x): ',
integral1)
# definite integration of cos(x) w.r.t. dx between -1 to 1
integral2 = sym.integrate(sym.cos(x), (x, -1, 1))
print('definite integral of cos(x) between -1 to 1: ',
integral2)
# definite integration of exp(-x) w.r.t. dx between 0 to ∞
integral3 = sym.integrate(sym.exp(-x), (x, 0, sym.oo))
print('definite integral of exp(-x) between 0 to ∞: ',
integral3)
Output:
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
You can calculate limit of a function by using limit(function, variable, point). So, if you want to compute the limit of f(x) as x->0, you would issue limit(f, x, 0).
Example:
Python
# 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)
Output:
oo
0
1
Series Expansion
We can also compute Taylor series expansions of functions around a point. To compute the expansion of f(x) around the point x=x0 terms of order xn, use sympy.series(f, x, x0, n). x0 and n can be omitted, in which case the defaults x0=0 and n=6 will be used.
Example:
Python
# 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)
Output:
1 - x**2/2 + x**4/24 + O(x**6)
1 + x**2/2 + O(x**4)
The O(x4) or O(x6) term at the end means that all x terms with a power greater than or equal to x4 or x6 are omitted.
Similar Reads
What Can I Do With Python?
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Let's see what Python programming does: Uses of PythonIn terms of
5 min read
Calculate Pi with Python
Pi is an irrational number having non-recurring decimal values. We commonly know Pi=3.14 or Pi=22/7, but it is just an approximation for our ease. In this article, we will see how to calculate PI with Python and also how to use PI in Python. Calculate and Use PI in PythonBelow are the ways by which
3 min read
How to Learn Python Basics With ChatGPT
Python is one of the most popular programming languages, known for its simplicity and versatility. Whether you're a complete beginner or an experienced programmer looking to expand your skillset, mastering the basics of Python is essential. In this guide, we'll walk you through the fundamentals of P
4 min read
How to make calculator using kivy | Python
Kivy is a platform-independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. In this artic
3 min read
How to call a function in Python
Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de
5 min read
Python | Loan calculator using Tkinter
Prerequisite: Tkinter Introduction Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest
5 min read
Timeit in Python with Examples
The timeit module in Python accurately measures the execution time of small code snippets, offering more consistent results than time.time() by avoiding background interference and disabling garbage collection. Itâs ideal for comparing code performance, benchmarking and optimization and can be used
3 min read
How to Define and Call a Function in Python
In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde
3 min read
Faulty calculator using Python
A faulty calculator is simply a calculator which operates simple tasks, but in some cases (set by the programmer) it gives the wrong output. You all must be wondering why do we need a faulty calculator? This type of calculator is not needed unless you want to prank someone or prove them wrong in cas
2 min read
How To Convert Data Types in Python 3?
Type Conversion is also known as typecasting, is an important feature in Python that allows developers to convert a variable of one type into another. In Python 3, type conversion can be done both explicitly (manual conversion) and implicitly (automatic conversion by Python).Table of ContentTypes of
4 min read