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 'dydx_observed&#= 39; gives=C2=A0the derivative of 'y_observed' with respect to '= xx_observed',=C2=A0while 'dydx' is the derivative with respect = to 'x_observed'. 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 "know" the function, it doesn'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 <<a href=3D"mailto:sdupree@speakeasy.= net" target=3D"_blank">[email protected]</a>> 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], "=C2=A0 "= , dydx[i] )<br> <br> <br> plt.plot(x_observed,=C2=A0=C2=A0=C2=A0 y_observed, "bo" , label= =3D"observation funct")<br> plt.plot(x_observed, dydx_observed, "rx" , label=3D"observat= ion deriv")<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 , "c-", la= bel=3D"pchip interpolation funct")<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 , "k-", label=3D"pch= ip interpolation deriv")<br> plt.legend()<br> plt.grid()<br> plt.savefig("pchip_example_01.png")<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> > Send SciPy-User mailing list submissions to<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:[email protected]" tar= get=3D"_blank">[email protected]</a><br> ><br> > To subscribe or unsubscribe via the World Wide Web, visit<br> >=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> > or, via email, send a message with subject or body 'help' to<b= r> >=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:scipy-user-request@python.= org" target=3D"_blank">[email protected]</a><br> ><br> > You can reach the person managing the list at<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0<a href=3D"mailto:[email protected]= g" target=3D"_blank">[email protected]</a><br> ><br> > When replying, please edit your Subject line so it is more specific<br= > > than "Re: Contents of SciPy-User digest..."<br> ><br> > Today's Topics:<br> ><br> >=C2=A0 =C2=A0 =C2=A01. Re: Getting scipy.interpolate.pchip_interpolate = to return the first derivative of a pchip interpolation<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 (Evgeni Burovski)<br> ><br> ><br> > ----------------------------------------------------------------------= <br> ><br> > Message: 1<br> > Date: Mon, 23 Jan 2023 11:20:45 +0300<br> > From: Evgeni Burovski <<a href=3D"mailto:[email protected]= " target=3D"_blank">[email protected]</a>><br> > Subject: [SciPy-User] Re: Getting scipy.interpolate.pchip_interpolate<= br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0to return the first derivative of a pchip in= terpolation<br> > To: SciPy Users List <<a href=3D"mailto:[email protected]" targ= et=3D"_blank">[email protected]</a>><br> > Message-ID:<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0<CAMRo0ita9yhVLcETaBNPOaL5mYMvm8N1KQV-=3D= <a href=3D"mailto:km0r%[email protected]" target=3D"_blank">km0r+p5= [email protected]</a>><br> > Content-Type: text/plain; charset=3D"UTF-8"<br> ><br> > Well, the derivative of sin(x*pi/180) is pi/180 * cos(x*pi/180), and<b= r> > the interpolator picks this up.<br> ><br> > On Mon, Jan 23, 2023 at 8:50 AM Samuel Dupree <<a href=3D"mailto:sd= [email protected]" target=3D"_blank">[email protected]</a>> wrote:= <br> >> I'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> >><br> >> The test program I'm using is given below (and attached to thi= s note).<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 =3D np.linspace(0.0, 360.0, 51)<br> >> y_observed=C2=A0 =3D np.sin(np.pi*x_observed/180)<br> >> dydx_observed =3D np.cos(np.pi*x_observed/180)<br> >><br> >> x=C2=A0 =C2=A0 =3D np.linspace(min(x_observed), max(x_observed), n= um=3D100)<br> >> y=C2=A0 =C2=A0 =3D pchip_interpolate(x_observed, y_observed, x, de= r=3D0, axis=3D0)<br> >> dydx =3D pchip_interpolate(x_observed, y_observed, x, der=3D1, axi= s=3D0)<br> >><br> >> plt.plot(x_observed,=C2=A0 =C2=A0 y_observed, "bo" , lab= el=3D"observation funct")<br> >> plt.plot(x_observed, dydx_observed, "rx" , label=3D"= ;observation deriv")<br> >> 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 , "c-", label=3D"pchip interpolatio= n funct")<br> >> plt.plot(x=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0, dydx=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0, "k-", label=3D"pchip interpolation der= iv")<br> >> plt.legend()<br> >> plt.savefig("pchip_example_01.png")<br> >> plt.show()<br> >><br> >><br> >> 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> >><br> >> Upon closer inspection, I believe I know what is going on, but I d= on't understand why.<br> >><br> >> 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> >><br> >> What I don't understand is how the interpolator came up with t= he scale factor it did and applied it using pure numbers.<br> >><br> >> Any thoughts?<br> >><br> >> Sam Dupree<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:scipy-user-leave= @python.org" target=3D"_blank">[email protected]</a><br> >> <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> >> Member address: <a href=3D"mailto:[email protected]" targ= et=3D"_blank">[email protected]</a><br> > ------------------------------<br> ><br> > Subject: Digest Footer<br> ><br> > _______________________________________________<br> > SciPy-User mailing list -- <a href=3D"mailto:[email protected]" ta= rget=3D"_blank">[email protected]</a><br> > To unsubscribe send an email to <a href=3D"mailto:scipy-user-leave@pyt= hon.org" target=3D"_blank">[email protected]</a><br> > <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> ><br> ><br> > ------------------------------<br> ><br> > End of SciPy-User Digest, Vol 231, Issue 1<br> > ******************************************<br> ><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==--