Re: SciPy-User Digest, Vol 231, Issue 1

Rusty Gentile <[email protected]> Mon, 23 Jan 2023 14:08:09 -0500
Newsgroups gmane.comp.python.scientific.user
Message-ID <CA+3FKvadiouU73FwJqKAXkaLBUK_Ug88b4pPjMDyPsTsP6aXgg@mail.gmail.com>
--===============0336487365651225451==
Content-Type: multipart/alternative; boundary="000000000000366ef105f2f31f4f"

--000000000000366ef105f2f31f4f
Content-Type: text/plain; charset="UTF-8"

Sam,

Your variable 'dydx_observed' gives the derivative of 'y_observed' with
respect to 'xx_observed', while 'dydx' is the derivative with respect to
'x_observed'. To get the missing scaling factor, you can apply the chain
rule.

As to your other question, how does the interpolator "know" the function,
it doesn't. It uses a spline approximation for f(x). And a spline is just a
piecewise polynomial, so the derivatives are easily calculated using the
coefficients.

Best,
Rusty




On Mon, Jan 23, 2023 at 1:31 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-=
> [email protected]>
> > 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]
>

--000000000000366ef105f2f31f4f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Sam,<div><br></div><div>Your variable &#39;dydx_observed&#=
39; gives=C2=A0the derivative of &#39;y_observed&#39; with respect to &#39;=
xx_observed&#39;,=C2=A0while &#39;dydx&#39; is the derivative with respect =
to &#39;x_observed&#39;. To get the missing scaling factor, you can apply t=
he chain rule.</div><div><br></div><div>As to your other=C2=A0question, how=
 does the interpolator &quot;know&quot; the function, it doesn&#39;t. It us=
es a spline approximation for f(x). And a spline is just a piecewise polyno=
mial, so the derivatives are easily calculated using the coefficients.</div=
><div><br></div><div>Best,</div><div>Rusty</div><div><br></div></div><div d=
ir=3D"ltr"><div><br></div></div><div dir=3D"ltr"><div><br></div></div><br><=
div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Jan=
 23, 2023 at 1:31 PM Samuel Dupree &lt;<a href=3D"mailto:sdupree@speakeasy.=
net" target=3D"_blank">[email protected]</a>&gt; wrote:<br></div><block=
quote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1=
px solid rgb(204,204,204);padding-left:1ex">The problem I have is if the in=
terpolator only sees numbers. Unless <br>
there is some undercover means by which the interpolator sees the <br>
function used to generate the numbers passed to it, how does it know the <b=
r>
conversion factor is used argument to the sine function? Furthermore, If <b=
r>
I were to rewrite the program as follows, I still get the same results.<br>
<br>
<br>
<br>
import numpy as np<br>
import matplotlib.pyplot as plt<br>
from scipy.interpolate import pchip_interpolate<br>
<br>
x_observed=C2=A0=C2=A0=C2=A0 =3D np.linspace(0.0, 360.0, 100)<br>
xx_observed=C2=A0=C2=A0 =3D np.pi*x_observed/180<br>
<br>
y_observed=C2=A0=C2=A0=C2=A0 =3D np.sin(xx_observed)<br>
dydx_observed =3D np.cos(xx_observed)<br>
<br>
x=C2=A0=C2=A0=C2=A0 =3D np.linspace(min(x_observed), max(x_observed), num=
=3D100)<br>
y=C2=A0=C2=A0=C2=A0 =3D pchip_interpolate(x_observed, y_observed, x, der=3D=
0, axis=3D0)<br>
dydx =3D pchip_interpolate(x_observed, y_observed, x, der=3D1, axis=3D0)<br=
>
<br>
<br>
ndim =3D len( dydx )<br>
print()<br>
for i in range(0,ndim):<br>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print( dydx_observed[i], &quot;=C2=A0 &quot;=
, dydx[i] )<br>
<br>
<br>
plt.plot(x_observed,=C2=A0=C2=A0=C2=A0 y_observed, &quot;bo&quot; , label=
=3D&quot;observation funct&quot;)<br>
plt.plot(x_observed, dydx_observed, &quot;rx&quot; , label=3D&quot;observat=
ion deriv&quot;)<br>
<br>
plt.plot(x=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 , y=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 , &quot;c-&quot;, la=
bel=3D&quot;pchip interpolation funct&quot;)<br>
plt.plot(x=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 , dydx=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 , &quot;k-&quot;, label=3D&quot;pch=
ip interpolation deriv&quot;)<br>
plt.legend()<br>
plt.grid()<br>
plt.savefig(&quot;pchip_example_01.png&quot;)<br>
plt.show()<br>
<br>
<br>
Any thoughts?<br>
<br>
Sam Dupree.<br>
<br>
<br>
On 1/23/23 12:00, <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a> wrote:<br>
&gt; Send SciPy-User mailing list submissions to<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:[email protected]" tar=
get=3D"_blank">[email protected]</a><br>
&gt;<br>
&gt; To subscribe or unsubscribe via the World Wide Web, visit<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"https://mail.python.org/mailman3/=
lists/scipy-user.python.org/" rel=3D"noreferrer" target=3D"_blank">https://=
mail.python.org/mailman3/lists/scipy-user.python.org/</a><br>
&gt; or, via email, send a message with subject or body &#39;help&#39; to<b=
r>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:scipy-user-request@python.=
org" target=3D"_blank">[email protected]</a><br>
&gt;<br>
&gt; You can reach the person managing the list at<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:[email protected]=
g" target=3D"_blank">[email protected]</a><br>
&gt;<br>
&gt; When replying, please edit your Subject line so it is more specific<br=
>
&gt; than &quot;Re: Contents of SciPy-User digest...&quot;<br>
&gt;<br>
&gt; Today&#39;s Topics:<br>
&gt;<br>
&gt;=C2=A0 =C2=A0 =C2=A01. Re: Getting scipy.interpolate.pchip_interpolate =
to return the first derivative of a pchip interpolation<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 (Evgeni Burovski)<br>
&gt;<br>
&gt;<br>
&gt; ----------------------------------------------------------------------=
<br>
&gt;<br>
&gt; Message: 1<br>
&gt; Date: Mon, 23 Jan 2023 11:20:45 +0300<br>
&gt; From: Evgeni Burovski &lt;<a href=3D"mailto:[email protected]=
" target=3D"_blank">[email protected]</a>&gt;<br>
&gt; Subject: [SciPy-User] Re: Getting scipy.interpolate.pchip_interpolate<=
br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0to return the first derivative of a pchip in=
terpolation<br>
&gt; To: SciPy Users List &lt;<a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a>&gt;<br>
&gt; Message-ID:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0&lt;CAMRo0ita9yhVLcETaBNPOaL5mYMvm8N1KQV-=3D=
<a href=3D"mailto:km0r%[email protected]" target=3D"_blank">km0r+p5=
[email protected]</a>&gt;<br>
&gt; Content-Type: text/plain; charset=3D&quot;UTF-8&quot;<br>
&gt;<br>
&gt; Well, the derivative of sin(x*pi/180) is pi/180 * cos(x*pi/180), and<b=
r>
&gt; the interpolator picks this up.<br>
&gt;<br>
&gt; On Mon, Jan 23, 2023 at 8:50 AM Samuel Dupree &lt;<a href=3D"mailto:sd=
[email protected]" target=3D"_blank">[email protected]</a>&gt; wrote:=
<br>
&gt;&gt; I&#39;m running SciPy ver. 1.9.3 under Python ver. 3.9.15=C2=A0 on=
 a Mac Pro (2019) desktop running Mac OSX ver. 13.1 Ventura. The problem I&=
#39;m having is getting scipy.interpolate.pchip_interpolate to return the f=
irst derivative of a pchip interpolation.<br>
&gt;&gt;<br>
&gt;&gt; The test program I&#39;m using is given below (and attached to thi=
s note).<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; import numpy as np<br>
&gt;&gt; import matplotlib.pyplot as plt<br>
&gt;&gt; from scipy.interpolate import pchip_interpolate<br>
&gt;&gt;<br>
&gt;&gt; x_observed=C2=A0 =3D np.linspace(0.0, 360.0, 51)<br>
&gt;&gt; y_observed=C2=A0 =3D np.sin(np.pi*x_observed/180)<br>
&gt;&gt; dydx_observed =3D np.cos(np.pi*x_observed/180)<br>
&gt;&gt;<br>
&gt;&gt; x=C2=A0 =C2=A0 =3D np.linspace(min(x_observed), max(x_observed), n=
um=3D100)<br>
&gt;&gt; y=C2=A0 =C2=A0 =3D pchip_interpolate(x_observed, y_observed, x, de=
r=3D0, axis=3D0)<br>
&gt;&gt; dydx =3D pchip_interpolate(x_observed, y_observed, x, der=3D1, axi=
s=3D0)<br>
&gt;&gt;<br>
&gt;&gt; plt.plot(x_observed,=C2=A0 =C2=A0 y_observed, &quot;bo&quot; , lab=
el=3D&quot;observation funct&quot;)<br>
&gt;&gt; plt.plot(x_observed, dydx_observed, &quot;rx&quot; , label=3D&quot=
;observation deriv&quot;)<br>
&gt;&gt; plt.plot(x=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0, y=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 , &quot;c-&quot;, label=3D&quot;pchip interpolatio=
n funct&quot;)<br>
&gt;&gt; plt.plot(x=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0, dydx=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0, &quot;k-&quot;, label=3D&quot;pchip interpolation der=
iv&quot;)<br>
&gt;&gt; plt.legend()<br>
&gt;&gt; plt.savefig(&quot;pchip_example_01.png&quot;)<br>
&gt;&gt; plt.show()<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; The program generates values of the sine function (y_observed) ove=
r the range of 0 to 360 degrees. (x_observed). In a similar fashion, the co=
sine function (first derivative of the sine function) is generated over the=
 same range (dydx_observed). pchip_interpolate is used to perform the inter=
polation 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 fu=
nction (sine) as expected. However, the first derivative returned fails to =
overlay the cosine function. The plot is attached to this note.<br>
&gt;&gt;<br>
&gt;&gt; Upon closer inspection, I believe I know what is going on, but I d=
on&#39;t understand why.<br>
&gt;&gt;<br>
&gt;&gt; The line for the first derivative that failed to coincide with the=
 points in the plot for the cosine is actually the interpolated first deriv=
ative scaled by the factor pi/180. When I multiply the interpolated values =
for the first derivative by 180/pi, the interpolated first derivative coinc=
ides with the points for the cosine as expected.<br>
&gt;&gt;<br>
&gt;&gt; What I don&#39;t understand is how the interpolator came up with t=
he scale factor it did and applied it using pure numbers.<br>
&gt;&gt;<br>
&gt;&gt; Any thoughts?<br>
&gt;&gt;<br>
&gt;&gt; Sam Dupree<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; SciPy-User mailing list -- <a href=3D"mailto:[email protected]=
" target=3D"_blank">[email protected]</a><br>
&gt;&gt; To unsubscribe send an email to <a href=3D"mailto:scipy-user-leave=
@python.org" target=3D"_blank">[email protected]</a><br>
&gt;&gt; <a href=3D"https://mail.python.org/mailman3/lists/scipy-user.pytho=
n.org/" rel=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailma=
n3/lists/scipy-user.python.org/</a><br>
&gt;&gt; Member address: <a href=3D"mailto:[email protected]" targ=
et=3D"_blank">[email protected]</a><br>
&gt; ------------------------------<br>
&gt;<br>
&gt; Subject: Digest Footer<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; SciPy-User mailing list -- <a href=3D"mailto:[email protected]" ta=
rget=3D"_blank">[email protected]</a><br>
&gt; To unsubscribe send an email to <a href=3D"mailto:scipy-user-leave@pyt=
hon.org" target=3D"_blank">[email protected]</a><br>
&gt; <a href=3D"https://mail.python.org/mailman3/lists/scipy-user.python.or=
g/" rel=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/l=
ists/scipy-user.python.org/</a><br>
&gt;<br>
&gt;<br>
&gt; ------------------------------<br>
&gt;<br>
&gt; End of SciPy-User Digest, Vol 231, Issue 1<br>
&gt; ******************************************<br>
&gt;<br>
<br>
_______________________________________________<br>
SciPy-User mailing list -- <a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a><br>
To unsubscribe send an email to <a href=3D"mailto:[email protected]=
rg" target=3D"_blank">[email protected]</a><br>
<a href=3D"https://mail.python.org/mailman3/lists/scipy-user.python.org/" r=
el=3D"noreferrer" target=3D"_blank">https://mail.python.org/mailman3/lists/=
scipy-user.python.org/</a><br>
Member address: <a href=3D"mailto:[email protected]" target=3D"_blank"=
>[email protected]</a><br>
</blockquote></div>

--000000000000366ef105f2f31f4f--

--===============0336487365651225451==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
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]

--===============0336487365651225451==--