Re: SciPy-User Digest, Vol 231, Issue 1
Evgeni Burovski <[email protected]> Mon, 23 Jan 2023 22:14:04 +0300
| Newsgroups | gmane.comp.python.scientific.user |
|---|---|
| Message-ID | <CAMRo0iurX7hEAwftP_GPCgT8ZoPMnjUNTGb9OpfOqGD6E32KBw@mail.gmail.com> |
Your question is equivalent to asking how a finite-difference approximation, (e.g. (f(x + h) - f(x)) / h ), "knows" about differentiating composite functions (e.g. f(g(x)) ). The answer is essentially a calculus textbook, the section which derives the differentiation table from the definition of the derivative as a limit of divided differences. On Mon, Jan 23, 2023 at 9:30 PM Samuel Dupree <[email protected]> wrote: > > The problem I have is if the interpolator only sees numbers. Unless > there is some undercover means by which the interpolator sees the > function used to generate the numbers passed to it, how does it know the > conversion factor is used argument to the sine function? Furthermore, If > I were to rewrite the program as follows, I still get the same results. > > > > import numpy as np > import matplotlib.pyplot as plt > from scipy.interpolate import pchip_interpolate > > x_observed = np.linspace(0.0, 360.0, 100) > xx_observed = np.pi*x_observed/180 > > y_observed = np.sin(xx_observed) > dydx_observed = np.cos(xx_observed) > > x = np.linspace(min(x_observed), max(x_observed), num=100) > y = pchip_interpolate(x_observed, y_observed, x, der=0, axis=0) > dydx = pchip_interpolate(x_observed, y_observed, x, der=1, axis=0) > > > ndim = len( dydx ) > print() > for i in range(0,ndim): > print( dydx_observed[i], " ", dydx[i] ) > > > plt.plot(x_observed, y_observed, "bo" , label="observation funct") > plt.plot(x_observed, dydx_observed, "rx" , label="observation deriv") > > plt.plot(x , y , "c-", label="pchip interpolation funct") > plt.plot(x , dydx , "k-", label="pchip interpolation deriv") > plt.legend() > plt.grid() > plt.savefig("pchip_example_01.png") > plt.show() > > > Any thoughts? > > Sam Dupree. > > > On 1/23/23 12:00, [email protected] wrote: > > Send SciPy-User mailing list submissions to > > [email protected] > > > > To subscribe or unsubscribe via the World Wide Web, visit > > https://mail.python.org/mailman3/lists/scipy-user.python.org/ > > or, via email, send a message with subject or body 'help' to > > [email protected] > > > > You can reach the person managing the list at > > [email protected] > > > > When replying, please edit your Subject line so it is more specific > > than "Re: Contents of SciPy-User digest..." > > > > Today's Topics: > > > > 1. Re: Getting scipy.interpolate.pchip_interpolate to return the first derivative of a pchip interpolation > > (Evgeni Burovski) > > > > > > ---------------------------------------------------------------------- > > > > Message: 1 > > Date: Mon, 23 Jan 2023 11:20:45 +0300 > > From: Evgeni Burovski <[email protected]> > > Subject: [SciPy-User] Re: Getting scipy.interpolate.pchip_interpolate > > to return the first derivative of a pchip interpolation > > To: SciPy Users List <[email protected]> > > Message-ID: > > <CAMRo0ita9yhVLcETaBNPOaL5mYMvm8N1KQV-=km0r+p5cJPekg@mail.gmail.com> > > Content-Type: text/plain; charset="UTF-8" > > > > Well, the derivative of sin(x*pi/180) is pi/180 * cos(x*pi/180), and > > the interpolator picks this up. > > > > On Mon, Jan 23, 2023 at 8:50 AM Samuel Dupree <[email protected]> wrote: > >> I'm running SciPy ver. 1.9.3 under Python ver. 3.9.15 on a Mac Pro (2019) desktop running Mac OSX ver. 13.1 Ventura. The problem I'm having is getting scipy.interpolate.pchip_interpolate to return the first derivative of a pchip interpolation. > >> > >> The test program I'm using is given below (and attached to this note). > >> > >> > >> import numpy as np > >> import matplotlib.pyplot as plt > >> from scipy.interpolate import pchip_interpolate > >> > >> x_observed = np.linspace(0.0, 360.0, 51) > >> y_observed = np.sin(np.pi*x_observed/180) > >> dydx_observed = np.cos(np.pi*x_observed/180) > >> > >> x = np.linspace(min(x_observed), max(x_observed), num=100) > >> y = pchip_interpolate(x_observed, y_observed, x, der=0, axis=0) > >> dydx = pchip_interpolate(x_observed, y_observed, x, der=1, axis=0) > >> > >> plt.plot(x_observed, y_observed, "bo" , label="observation funct") > >> plt.plot(x_observed, dydx_observed, "rx" , label="observation deriv") > >> plt.plot(x , y , "c-", label="pchip interpolation funct") > >> plt.plot(x , dydx , "k-", label="pchip interpolation deriv") > >> plt.legend() > >> plt.savefig("pchip_example_01.png") > >> plt.show() > >> > >> > >> The program generates values of the sine function (y_observed) over the range of 0 to 360 degrees. (x_observed). In a similar fashion, the cosine function (first derivative of the sine function) is generated over the same range (dydx_observed). pchip_interpolate is used to perform the interpolation over a specified range for the function and its first derivative. A composite plot is generated showing the points for the function (the sine) and its first derivative (cosine). The interpolated points overlay the function (sine) as expected. However, the first derivative returned fails to overlay the cosine function. The plot is attached to this note. > >> > >> Upon closer inspection, I believe I know what is going on, but I don't understand why. > >> > >> The line for the first derivative that failed to coincide with the points in the plot for the cosine is actually the interpolated first derivative scaled by the factor pi/180. When I multiply the interpolated values for the first derivative by 180/pi, the interpolated first derivative coincides with the points for the cosine as expected. > >> > >> What I don't understand is how the interpolator came up with the scale factor it did and applied it using pure numbers. > >> > >> Any thoughts? > >> > >> Sam Dupree > >> > >> _______________________________________________ > >> SciPy-User mailing list -- [email protected] > >> To unsubscribe send an email to [email protected] > >> https://mail.python.org/mailman3/lists/scipy-user.python.org/ > >> Member address: [email protected] > > ------------------------------ > > > > Subject: Digest Footer > > > > _______________________________________________ > > SciPy-User mailing list -- [email protected] > > To unsubscribe send an email to [email protected] > > https://mail.python.org/mailman3/lists/scipy-user.python.org/ > > > > > > ------------------------------ > > > > End of SciPy-User Digest, Vol 231, Issue 1 > > ****************************************** > > > > _______________________________________________ > SciPy-User mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/scipy-user.python.org/ > Member address: [email protected] _______________________________________________ SciPy-User mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/scipy-user.python.org/ Member address: [email protected]