numerical differentiation in python...in python we can use the polyder()function to perform...
TRANSCRIPT
-
Numerical Differentiation in Python
Hans-Petter Halvorsen
https://www.halvorsen.blog
-
https://www.halvorsen.blog/documents/programming/python/
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
-
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
https://www.halvorsen.blog/documents/programming/python/
-
β’ The Derivativeβ’ Numerical Differentiationβ’ Python Examples
It is assumed that already know about the derivative from mathematics courses and that you want to use Python to find numerical solutions
Contents
-
The Derivative The derivative of a function π¦ = π(π₯) is a measure of how π¦ changes with π₯
We have the following definition:
The derivative of a function π(π₯) is denoted !"($)!$
ππ(π₯)ππ₯
= lim!β#
π π₯ + β β π(π₯)β
π(π₯)
π(π₯ + β)
βπ₯ π₯ + β
Secant
ππ(π₯)ππ₯ = π¦
!(π₯) = οΏ½ΜοΏ½(π₯)
Different notation is used:
-
The Derivative
π₯
π(π₯)
Tangent line
The derivative of a function of a single variable at a chosen input value, when it exists, is the slope of the tangent line to the graph of the function at that point.
π(π₯")
π₯"
ππ(π₯")ππ₯
Example:
π π₯ = π₯#
ππ(π₯)ππ₯ = 2π₯
ππ(2)ππ₯ = 2Γ2 = 4
π₯ = 2https://en.wikipedia.org/wiki/Derivative
https://en.wikipedia.org/wiki/Derivative
-
Derivative RulesThere are many derivative rules (as you probably know from mathematics courses)
We will focus on the the basic rule:
π π₯ = ππ₯! ππ(π₯)ππ₯
= π ( π ( π₯!"#
Example:
π π₯ = 4π₯$
ππ(π₯)ππ₯ = 4Γ3π₯
# = 12π₯#ππ(3)ππ₯ = 12Γ3
# = 12Γ9 = 108
-
Basic Numerical ApproachA numerical approach to the derivative of a function π¦ = π(π₯) is:
ππ¦ππ₯
ββπ¦βπ₯
=π¦6 β π¦7π₯6 β π₯7
π(π₯)
π(π₯ + β)
βπ₯ π₯ + β
Note! We will use Python in order to find the numeric solution β not the analytic solution
-
Exampleπ¦ π₯ = π₯#
ππ¦ππ₯
= 2π₯We know for this simple example that the exact analytical solution is:
ππ¦ππ₯ =?
Given the following values:
x y
-2 4
-1 1
0 0
1 1
2 4
ππ¦ππ₯ π₯ = β2 = β4ππ¦ππ₯ π₯ = β1 = β2ππ¦ππ₯ π₯ = 0 = 0ππ¦ππ₯
π₯ = 1 = 2ππ¦ππ₯ π₯ = 2 = 4
Let's use Python to see if we get the same values?
-
Python Codeimport numpy as npimport matplotlib.pyplot as plt
xstart = -2xstop = 2.1increment = 0.1x = np.arange(xstart,xstop,increment)
y = x**2
plt.plot(x,y)
xstart = -2xstop = 3increment = 1x = np.arange(xstart,xstop,increment)
y = x**2;
plt.plot(x,y, '-o')plt.title("y(x)")
We start to plot the function:
π¦ π₯ = π₯&
x y
-2 4
-1 1
0 0
1 1
2 4
We use the following data points: Resulting plot:
-
Python Codeimport numpy as npimport matplotlib.pyplot as plt
xstart = -2xstop = 3increment = 1x = np.arange(xstart,xstop,increment)
y = x**2;
# Exact/Analytical Solutiondydx_exact = 2*x
print("dydx_exact=", dydx_exact)
plt.plot(x, dydx_exact, 'o-')
# Numerical Solutiondydx_num = np.diff(y) / np.diff(x);
print("dydx_num", dydx_num)
xstart = -2xstop = 2
x = np.arange(xstart,xstop,increment)
plt.plot(x, dydx_num, 'o-')plt.title("dy/dx")plt.legend(["Exact", "Numeric"])
We will use numerical differentiation to find !'
!$for the following function:
π¦ π₯ = π₯&
x dy/dx
-2
-1
0
1
2
We use the following data points: Results:
dydx_exact= [-4 -2 0 2 4]dydx_num [-3. -1. 1. 3.]
-
Comments to the Resultsπ¦ π₯ = π₯#
ππ¦ππ₯
=?
Exact Solution vs. Python Solution:
dydx_exact= [-4 -2 0 2 4]dydx_num [-3. -1. 1. 3.]
x dy/dx (Exact solution)
dy/dx (Numeric solution)
-2 -4 -3
-1 -2 -1
0 0 1
1 2 3
2 4 -
ππ¦ππ₯
ββπ¦βπ₯
=π¦6 β π¦7π₯6 β π₯7
Results from Python Script:
β’ The accuracy of the results are not so good.
β’ Can we expect better results when we increase number of data points?
β’ Let's Try!
-
Python Codeimport numpy as npimport matplotlib.pyplot as plt
xstart = -2xstop = 2.1increment = 0.1
x = np.arange(xstart,xstop,increment)
y = x**2;
# Exact/Analytical Solutiondydx_exact = 2*x
print("dydx_exact=", dydx_exact)plt.plot(x, dydx_exact, 'o-')
# Numerical Solutiondydx_num = np.diff(y) / np.diff(x);
print("dydx_num", dydx_num)
xstart = -2xstop = 2
x2 = np.arange(xstart,xstop,increment)
plt.plot(x2, dydx_num, 'o-')plt.title("dy/dx")plt.legend(["Exact", "Numeric"])
We increase number of data points to see if we get better results
Previously: x = -2,-1,0,1,2Now: x = -2.0,-1.9, -1.8, β¦ 0, 0.1, β¦, 1.9, 2.0
Yes!
-
Comments to the Results
β’ We see that the numeric solutions becomes very close to the exact solutions. β’ When β β 0 we should expect that the numerical solutions should exactly
match the exact solutions.
ππ(π₯)ππ₯
= lim!β#
π π₯ + β β π(π₯)β
ππ¦ππ₯ β
βπ¦βπ₯ =
π¦6 β π¦7π₯6 β π₯7
-
Differentiation on Polynomials
Hans-Petter Halvorsen
https://www.halvorsen.blog
-
PolynomialsA polynomial is expressed as:
π π₯ = π*π₯+ + π,π₯+-* +β―+ π+π₯ + π+.*
where π*, π,, π/, β¦ are the coefficients of the polynomial.
In Python we can use the polyder() function to perform differentiation on polynomials. This function works the same way as the polyint()function which performs integration on polynomials.
-
Derivative RulesThere are many derivative rules (as you probably know from mathematics courses)
This basic rule is valid for Polynomials:
π π₯ = ππ₯! ππ(π₯)ππ₯
= π ( π ( π₯!"#
Example:
π π₯ = 4π₯$
ππ(π₯)ππ₯ = 4Γ3π₯
# = 12π₯#ππ(3)ππ₯ = 12Γ3
# = 12Γ9 = 108
-
ExampleGiven the following polynomial:
π π₯ = π₯/ + 2π₯, β π₯ + 3
Note!!! In order to use it in Python, we reformulate:
π π₯ = 3 βπ₯ + 2π₯,+ π₯/
We find:01(2)02
= 0 β 1 + 4π₯ + 3π₯, = β1 + 4π₯ + 3π₯,
-
Pythonimport numpy.polynomial.polynomial as poly
p = [3, -1, 2, 1]
dpdx = poly.polyder(p)
print("dpdx =", dpdx)
The Python solution:dpdx = [-1. 4. 3.]
π π₯ = 3 βπ₯ + 2π₯6+ π₯:
ππ(π₯)ππ₯ = β1 + 4π₯ + 3π₯
#
We see that the Python script gives the correct answer!
-
Another Python Exampleimport numpy.polynomial.polynomial as poly
p = [2, 0, 0, 1]
dpdx = np.polyder(p)
print("dpdx =", dpdx)
The Python solution:dpdx = [0. 0. 3.]
π π₯ = 2 + π₯:
ππ(π₯)ππ₯
= π = π₯ + 0 = 2π₯ + ππ₯#= 3π₯#
We see that the Python script gives the correct answer!
π π₯ = π + π 5 π₯ + π 5 π₯6 + π 5 π₯:
We need to reformulate to make it work with Python:
Given the Polynomial:
We find the derivative:
-
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
https://www.halvorsen.blog/documents/programming/python/
-
Hans-Petter Halvorsen
University of South-Eastern Norwaywww.usn.no
E-mail: [email protected]: https://www.halvorsen.blog
http://www.usn.no/mailto:[email protected]://www.halvorsen.blog/