Let’s learn to perform Calculus in Python. Calculus is a part of mathematics. It was discovered by Isaac Newton and Gottfried. In a nutshell, is an answer to two big questions related to functions.
- The First Question: At a particular point, how steep is a function? The solution to this question can be obtained by using Derivatives. At a particular point, it measures the rate of change of a function.
- The Second Question: What is the area under the graph over some region? The solution to this question can be obtained by using Integration. It combines the values of the function over a range of numbers.
Using the SymPy Module to Perform Calculus in Python
SymPy in Python Programming stands for Symbolic Python. It is a python library for symbolic mathematics. It does not require any external libraries. For executing python programs for calculus we need to import the module SymPy. SymPy is a module that allows us to interact with mathematical objects in a symbolic way.
To install SymPy module on your Windows System, follow the following Steps :
- Open your windows/Linux terminal
- We’ll use the pip package manager to install Sympy. Type
pip3 install sympy.
- Hit enter and your SymPy module will start downloading.
C:\Users\Admin> pip3 install sympy
Calculating Limits in Python
Limits in calculus are used to define continuity, derivatives, and integrals of a function sequence. To calculate limits in Python we use the following syntax:
Now, take for example a limit function as mentioned below:
limit = f(y) y-->a
In the above-mentioned syntax for calculating the limit in Python the parameters mentioned are function, variable, and value.
- f(y) is the function on with the limit operation will be performed.
- y is the variable of the function.
- a is the value which the limit tends to.
Equation example 1: limit x–>0= sin(x) / x
>>> import sympy as sp >>> x=sp.Symbol('x') >>> f=sp.sin(x)/x >>> L=sp.limit(f,x,0) >>> print("The Limit is:",L) The Limit is: 1
Equation example 2 : limit y–>0 = sin(2y) / y
>>> import sympy as sp >>> y=sp.Symbol('y') >>> f=sp.sin(2*y)/y >>> L=sp.limit(f,y,0) >>> print("The limit is:",L) The limit is: 2
Calculating Derivatives in Python
A major part of performing calculus in Python is derivatives. For differentiation or finding out the derivatives in limits, we use the following syntax:
Equation Example 1 : f(x) = sin(x) + x2 + e4x
>>> import sympy as sp >>> x=sp.Symbol('x') >>> f=sp.sin(x)+x**2+sp.exp(4*x) >>> print(f) x**2 + exp(4*x) + sin(x) >>> df=sp.diff(f,x) >>> print(df) 2*x + 4*exp(4*x) + cos(x) >>> ddf=sp.diff(f,x,2) >>> print(ddf) 16*exp(4*x) - sin(x) + 2
Equation Example 2 : f(y) = cos(y) + 4y + y3
>>> import sympy as sp >>> y=sp.Symbol('y') >>> f=sp.cos(y)+4*y+y**3 >>> print(f) y**3 + 4*y + cos(y) >>> df=sp.diff(f,y) >>> print(df) 3*y**2 - sin(y) + 4
Calculating Integration in Python
The SymPy module in Integration consists of integral modules. The syntax for calculating Integration in python is as followed:
Equation Example 1: y3 + y + 4
>>> from sympy import* >>> x,y=symbols('x y') >>> exp=y**3+y+4 >>> integrate(exp,y) y**4/4 + y**2/2 + 4*y
Equation Example 2: x2 + 4x + 12
>>> from sympy import* >>> x=symbols('x') >>> exp= x**2 + 4*x + 12 >>> integrate(exp,x) x**3/3 + 2*x**2 + 12*x
This brings us to the end of our brief tutorial on performing calculus in Python with the Sympy module. You can learn in detail about the Sympy module in its official documentation.