{"id":26279,"date":"2021-12-21T09:28:35","date_gmt":"2021-12-21T03:58:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=26279"},"modified":"2021-12-21T09:28:35","modified_gmt":"2021-12-21T03:58:35","slug":"python-programs-for-calculus-using-sympy-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programs-for-calculus-using-sympy-module\/","title":{"rendered":"Python Programs for Calculus Using SymPy Module"},"content":{"rendered":"
Calculus:<\/strong><\/p>\n 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.<\/p>\n Derivatives:<\/strong><\/p>\n 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.<\/p>\n Integration:<\/strong> 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.<\/p>\n Before performing calculus operations install the sympy <\/strong>module as shown below:<\/p>\n Installation of sympy:<\/strong><\/p>\n To write any sympy expression, we must first declare its symbolic variables. To accomplish this, we can employ the following two functions:<\/p>\n sympy.Symbol():<\/strong> This function is used to declare a single variable by passing it as a string into its parameter.<\/p>\n sympy.symbols():<\/strong> 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.<\/p>\n 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:<\/p>\n Syntax:<\/strong><\/p>\n For Example:<\/strong><\/p>\n limit = f(x)<\/strong> The parameters specified in the preceding syntax for computing the limit in Python are function, variable, and value.<\/p>\n f(x):<\/strong> The function on which the limit operation will be conducted is denoted by f(x). k:<\/strong> k is the value to which the limit tends to.<\/p>\n Example1: <\/strong>limit y\u2013>0.4= sin(y) \/ y<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:\u00a0 limit\u00a0y\u2013>0<\/sub> = sin(3y) \/ y<\/strong><\/p>\n Output:<\/strong><\/p>\n Derivatives are an important aspect of conducting calculus in Python. We use the following syntax to differentiate or find the derivatives in limits:<\/p>\n Syntax:<\/strong><\/p>\n Example: f(y) = sin(y) + y2<\/sup> + e^3y<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example: f(y) = cos(y) + y2<\/sup> + e^3y<\/strong><\/p>\n Output:<\/strong><\/p>\n Integration’s SymPy module is made up of integral modules. In Python, the syntax for calculating integration is as follows:<\/p>\n Syntax:<\/strong><\/p>\n Example:\u00a0 \u00a0 \u00a0x<\/strong>3<\/sup> + 2x + 5<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\n
\nwhat 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.<\/p>\nSymPy Module:<\/strong><\/h4>\n
pip install sympy<\/pre>\n
1)Limits Calculation in Python<\/h5>\n
sympy.limit(function,variable,value)<\/pre>\n
\nx–>k<\/strong><\/p>\n
\nx:<\/strong> The function’s variable is x.<\/p>\n\n
# limit y\u2013>0.4= sin(y) \/ y\r\n\r\n# Import sympy module as 'sp' using the import keyword\r\nimport sympy as sp\r\n# pass the argument y to symbol function which is LHS in given limit and store it in a variable\r\ny = sp.Symbol('y')\r\n# Create the RHS of the limit using the above LHS limit and sin function and sympy module\r\nfunc = sp.sin(y)\/y\r\n# Pass the given function, variable, value as the arguments to the limit() function\r\n# to get the limit value.\r\n# Store it in a variable.\r\nrslt_lmt = sp.limit(func, y, 0.4)\r\n# Print the above obtained limit value for the given function.\r\nprint(\"The result limit value for the given function = \", rslt_lmt)<\/pre>\n
The result limit value for the given function = 0.973545855771626<\/pre>\n
# limit y\u2013>0= sin(3y) \/ y\r\n\r\n# Import sympy module as 'sp' using the import keyword\r\nimport sympy as sp\r\n# pass the argument y to symbol function which is LHS in given limit and store it in a variable\r\ny = sp.Symbol('y')\r\n# Create the RHS of the limit using the above LHS limit and sin function and sympy module\r\nfunc = sp.sin(3*y)\/y\r\n# Pass the given function, variable, value as the arguments to the limit() function\r\n# to get the limit value.\r\n# Store it in a variable.\r\nrslt_lmt = sp.limit(func, y, 0)\r\n# Print the above obtained limit value for the given function.\r\nprint(\"The result limit value for the given function = \", rslt_lmt)<\/pre>\n
The result limit value for the given function = 3<\/pre>\n
2)Derivatives\u00a0Calculation in Python<\/h5>\n
sympy.diff(function,variable)<\/pre>\n
# f(y) = sin(y) + y2 + e^3y\r\n\r\n# Import sympy module as 'sp' using the import keyword\r\nimport sympy as sp\r\n# pass the argument y to symbol function which is LHS in given limit and store it in a variable\r\n\r\ny=sp.Symbol('y')\r\n#Create the RHS of the limit using the above LHS limit and sin function,exp function and sympy module\r\nfunc=sp.sin(y)+y**2+sp.exp(3*y)\r\n#get the first differentiation value by passing the function and lhs to diff function and print it\r\nfst_diff=sp.diff(func,y)\r\nprint('The value of first differentation of function',func,'is :\\n',fst_diff)\r\n#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\r\nscnd_diff=sp.diff(func,y,2)\r\nprint('The value of second differentation of function',func,'is :\\n',scnd_diff)<\/pre>\n
The value of first differentation of function y**2 + exp(3*y) + sin(y) is :\r\n2*y + 3*exp(3*y) + cos(y) \r\nThe value of second differentation of function y**2 + exp(3*y) + sin(y) is : \r\n9*exp(3*y) - sin(y) + 2<\/pre>\n
# f(y) = cos(y) + y2 + e^3y\r\n\r\n# Import sympy module as 'sp' using the import keyword\r\nimport sympy as sp\r\n# pass the argument y to symbol function which is LHS in given limit and store it in a variable\r\n\r\ny=sp.Symbol('y')\r\n# Create the RHS of the limit using the above LHS limit and cos function,exp function and sympy module\r\nfunc=sp.cos(y)+y**2+sp.exp(3*y)\r\n# get the first differentiation value by passing the function and lhs to diff function and print it\r\nfst_diff=sp.diff(func,y)\r\nprint('The value of first differentation of function',func,'is :\\n',fst_diff)\r\n# get the second differentiation value by passing the function and lhs to diff function and extra argument 2\r\n# (which implies 2nd differentitation) and print it\r\nscnd_diff=sp.diff(func,y,2)\r\nprint('The value of second differentation of function',func,'is :\\n',scnd_diff)<\/pre>\n
The value of first differentation of function y**2 + exp(3*y) + cos(y) is : \r\n2*y + 3*exp(3*y) - sin(y) \r\nThe value of second differentation of function y**2 + exp(3*y) + cos(y) is : \r\n9*exp(3*y) - cos(y) + 2<\/pre>\n
3)Integration\u00a0Calculation in Python<\/h5>\n
integrate(function, value)<\/pre>\n
# Function: x^3 + 2x + 5\r\n\r\n# Import all functions from sympy module using the import keyword\r\nfrom sympy import*\r\nx,y=symbols('x y')\r\ngvn_expresn = x**3+2*x+ 5\r\nprint(\"The integration for the given expression is:\")\r\nintegrate(gvn_expresn ,x)\r\n\r\n<\/pre>\n