# Python Programs for Calculus Using SymPy Module

Calculus:

Calculus is a branch of mathematics. Limits, functions, derivatives, integrals, and infinite series are all topics in calculus. To do calculus in Python, we will utilize the SymPy package.

Derivatives:

How steep is a function at a given point? Derivatives can be used to get the answer to this question. It measures the rate of change of a function at a specific point.

Integration:
what is the area beneath the graph over a particular region? Integration can be used to get the answer to this question. It combines the function’s values over a range of numbers.

#### SymPy Module:

SymPy is a Python symbolic mathematics library. It aspires to be a full-featured computer algebra system (CAS) while keeping the code as basic or simple as possible in order to be understandable and easily expandable. SymPy is entirely written in Python.

Before performing calculus operations install the sympy module as shown below:

Installation of sympy:

pip install sympy

To write any sympy expression, we must first declare its symbolic variables. To accomplish this, we can employ the following two functions:

sympy.Symbol(): This function is used to declare a single variable by passing it as a string into its parameter.

sympy.symbols(): This function is used to declare multivariable by passing the variables as a string as an argument. Each variable must be separated by a space to produce a string.

##### 1)Limits Calculation in Python

Limits are used in calculus to define the continuity, derivatives, and integrals of a function sequence. In Python, we use the following syntax to calculate limits:

Syntax:

sympy.limit(function,variable,value)

For Example:

limit = f(x)
x–>k

The parameters specified in the preceding syntax for computing the limit in Python are function, variable, and value.

f(x): The function on which the limit operation will be conducted is denoted by f(x).
x: The function’s variable is x.

k: k is the value to which the limit tends to.

Example1: limit y–>0.4= sin(y) / y

Approach:

• Import sympy module as ‘sp’ using the import keyword
• Pass the argument y to symbol() function which is LHS in given limit and store it in a variable
• Create the RHS of the limit using the above LHS limit and sin function and sympy module.
• Pass the given function, variable, value as the arguments to the limit() function to get the limit value.
• Store it in a variable.
• Print the above-obtained limit value for the given function.
• The Exit of the Program.

Below is the implementation:

# limit y–>0.4= sin(y) / y

# Import sympy module as 'sp' using the import keyword
import sympy as sp
# pass the argument y to symbol function which is LHS in given limit and store it in a variable
y = sp.Symbol('y')
# Create the RHS of the limit using the above LHS limit and sin function and sympy module
func = sp.sin(y)/y
# Pass the given function, variable, value as the arguments to the limit() function
# to get the limit value.
# Store it in a variable.
rslt_lmt = sp.limit(func, y, 0.4)
# Print the above obtained limit value for the given function.
print("The result limit value for the given function = ", rslt_lmt)

Output:

The result limit value for the given function = 0.973545855771626

Example2:  limit y–>0 = sin(3y) / y

# limit y–>0= sin(3y) / y

# Import sympy module as 'sp' using the import keyword
import sympy as sp
# pass the argument y to symbol function which is LHS in given limit and store it in a variable
y = sp.Symbol('y')
# Create the RHS of the limit using the above LHS limit and sin function and sympy module
func = sp.sin(3*y)/y
# Pass the given function, variable, value as the arguments to the limit() function
# to get the limit value.
# Store it in a variable.
rslt_lmt = sp.limit(func, y, 0)
# Print the above obtained limit value for the given function.
print("The result limit value for the given function = ", rslt_lmt)

Output:

The result limit value for the given function = 3
##### 2)Derivatives Calculation in Python

Derivatives are an important aspect of conducting calculus in Python. We use the following syntax to differentiate or find the derivatives in limits:

Syntax:

sympy.diff(function,variable)

Example: f(y) = sin(y) + y2 + e^3y

Below is the implementation:

# f(y) = sin(y) + y2 + e^3y

# Import sympy module as 'sp' using the import keyword
import sympy as sp
# pass the argument y to symbol function which is LHS in given limit and store it in a variable

y=sp.Symbol('y')
#Create the RHS of the limit using the above LHS limit and sin function,exp function and sympy module
func=sp.sin(y)+y**2+sp.exp(3*y)
#get the first differentiation value by passing the function and lhs to diff function and print it
fst_diff=sp.diff(func,y)
print('The value of first differentation of function',func,'is :\n',fst_diff)
#get the second differentiation value by passing the function and lhs to diff function and extra argument 2(which implies 2nd differentitation) and print it
scnd_diff=sp.diff(func,y,2)
print('The value of second differentation of function',func,'is :\n',scnd_diff)

Output:

The value of first differentation of function y**2 + exp(3*y) + sin(y) is :
2*y + 3*exp(3*y) + cos(y)
The value of second differentation of function y**2 + exp(3*y) + sin(y) is :
9*exp(3*y) - sin(y) + 2

Example: f(y) = cos(y) + y2 + e^3y

# f(y) = cos(y) + y2 + e^3y

# Import sympy module as 'sp' using the import keyword
import sympy as sp
# pass the argument y to symbol function which is LHS in given limit and store it in a variable

y=sp.Symbol('y')
# Create the RHS of the limit using the above LHS limit and cos function,exp function and sympy module
func=sp.cos(y)+y**2+sp.exp(3*y)
# get the first differentiation value by passing the function and lhs to diff function and print it
fst_diff=sp.diff(func,y)
print('The value of first differentation of function',func,'is :\n',fst_diff)
# get the second differentiation value by passing the function and lhs to diff function and extra argument 2
# (which implies 2nd differentitation) and print it
scnd_diff=sp.diff(func,y,2)
print('The value of second differentation of function',func,'is :\n',scnd_diff)

Output:

The value of first differentation of function y**2 + exp(3*y) + cos(y) is :
2*y + 3*exp(3*y) - sin(y)
The value of second differentation of function y**2 + exp(3*y) + cos(y) is :
9*exp(3*y) - cos(y) + 2
##### 3)Integration Calculation in Python

Integration’s SymPy module is made up of integral modules. In Python, the syntax for calculating integration is as follows:

Syntax:

integrate(function, value)

Example:     x3 + 2x + 5

Below is the implementation:

# Function: x^3 + 2x + 5

# Import all functions from sympy module using the import keyword
from sympy import*
x,y=symbols('x y')
gvn_expresn = x**3+2*x+ 5
print("The integration for the given expression is:")
integrate(gvn_expresn ,x)



Output: