[Numpy-discussion] Getting scipy.interpolate.pchip_interpolate to return the first derivative of a pchip interpolation

2023-01-21 Thread Samuel Dupree
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.


Any thoughts or suggestions?

Sam Dupree
#!/Users/user/opt/anaconda3/bin/python3

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()
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Getting scipy.interpolate.pchip_interpolate to return the first derivative of a pchip interpolation

2023-01-21 Thread Charles R Harris
You should post at the scipy-user mailing list. See
https://svn.scipy.org/scipylib/mailing-lists.html.

On Sat, Jan 21, 2023 at 4:08 PM Samuel Dupree  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.
>
> Any thoughts or suggestions?
>
> Sam Dupree
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: charlesr.har...@gmail.com
>
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com