How to: Numerical Derivative in Python
Learn how to take a simple numerical derivative of data using a difference formula in Python.
Script and resources to download can be found at: https://www.hageslab.com/python.html
Here we use "Spyder" IDE as the Python Shell and the following libraries: numpy and matplotlib
Here is the script:
import numpy as np
import matplotlib.pyplot as plt
def func(x,A,B):
return A*x**2+B*x
xlist = np.linspace(0,10,100)
ylist = func(xlist,2,1.4)
plt.figure(1,dpi=120)
plt.plot(xlist,ylist,label="Function")
def D(xlist,ylist):
yprime = np.diff(ylist)/np.diff(xlist)
xprime=[]
for i in range(len(yprime)):
xtemp = (xlist[i+1]+xlist[i])/2
xprime = np.append(xprime,xtemp)
return xprime, yprime
xprime, yprime = D(xlist,ylist)
plt.plot(xprime,yprime,label="Derivative")
xprime2, yprime2 = D(xprime,yprime)
plt.plot(xprime2,yprime2,label="2nd Derivative")
plt.legend()
Видео How to: Numerical Derivative in Python канала HagesLab
Script and resources to download can be found at: https://www.hageslab.com/python.html
Here we use "Spyder" IDE as the Python Shell and the following libraries: numpy and matplotlib
Here is the script:
import numpy as np
import matplotlib.pyplot as plt
def func(x,A,B):
return A*x**2+B*x
xlist = np.linspace(0,10,100)
ylist = func(xlist,2,1.4)
plt.figure(1,dpi=120)
plt.plot(xlist,ylist,label="Function")
def D(xlist,ylist):
yprime = np.diff(ylist)/np.diff(xlist)
xprime=[]
for i in range(len(yprime)):
xtemp = (xlist[i+1]+xlist[i])/2
xprime = np.append(xprime,xtemp)
return xprime, yprime
xprime, yprime = D(xlist,ylist)
plt.plot(xprime,yprime,label="Derivative")
xprime2, yprime2 = D(xprime,yprime)
plt.plot(xprime2,yprime2,label="2nd Derivative")
plt.legend()
Видео How to: Numerical Derivative in Python канала HagesLab
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
Derivatives In PYTHON (Symbolic AND Numeric)How to: Import, Plot, Fit, and Integrate Data in PythonSolving Differential Equations In Python In Less Than 5 Minutes (General Solution)you need to learn Python RIGHT NOW!! // EP 1How To Use Functions In Python (Python Tutorial #3)The Fastest Way to Loop in Python - An Unfortunate Truth1st Year Calculus, But in PYTHONNumerical derivatives in python using numpy.gradient() function: 1-dimensional case.Python: Simples courbes / matplotlib - Exemple.Python Solution for Nonlinear System of Equations - Tutorial 18How To Automate Excel Using Python | Combine Files & Create Charts 🤓How To Tell If Someone Is A Physics/Engineering StudentDerivatives, Symbolic and Numeric in PythonPython Full Course 🐍【𝙁𝙧𝙚𝙚】How to model a traveling wave in pythonPython NumPy Tutorial for BeginnersSpring Break Day 3: How To Find Derivatives In PythonPython Video 07d: Plotting Contour and Surface Plots with MatplotlibHow to: Plot a Function in PythonNewton-Raphson Method | Numerical Computing in Python