satej@home:~$

Python for differential calculus



Many would have known about using differential calculus in Maths. How do we use it with Python? Well, sympy and scipy can help us here.

Sympy is used to create symbols for our x, y variables while Scipy helps with the differential bit.

These can be installed using below command:

pip install sympy scipy

Next, import them:

from sympy import *
from scipy.misc import derivative

Create a symbol and use it to create an equation:

x = Symbol('x')
Fx = 7*x**3+x**2+1

Using scipy differentiate it:

Derivative(Fx,x).doit()

To create a derivative at a point:

def f(x):
    return 7*x**3+x**2+1
derivative(f,1.0)

def f(x):
    return 7*x**3+x**2+1
derivative(f,1.0)

An example of Partial Derivatives:

x, y, z = symbols('x, y, z', real=True)
f = x**2 + y**3 + z**4 + 5*x*y*z
f

Create derivative with respect to each variable:

for var in [x, y, z]:
    print(" f_" + str(var) + "=", f.diff(var))

Hope it helps and you explore more. Please do share! :)




LEAVE A COMMENT
Comments are powered by Utterances. A free GitHub account is required. Comments are moderated. Be respectful. No swearing or inflammatory language. No spam.

I reserve the right to delete any inappropriate comments. All comments for all pages can be viewed and searched online here. To edit or delete your comment: click the "Comments" link at the top of the comments section below where it says how many comments have been left (this will take you to a GitHub page with all comments for this page) --> find your comment on this GitHub page and click the 3 dots in the top-right --> click "Edit" or "Delete". Editing or adding a comment from the GitHub page also gives you a nicer editor.