Skip to content

Commit 56b1e14

Browse files
committed
added material on polyfit
1 parent bbd16e4 commit 56b1e14

File tree

2 files changed

+211
-0
lines changed

2 files changed

+211
-0
lines changed

numpy-tutorial-exercises.ipynb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,148 @@
19771977
"Create a 2d array containing random numbers and generate a vector containing for each row the entry closest to one-half."
19781978
]
19791979
},
1980+
{
1981+
"cell_type": "code",
1982+
"execution_count": null,
1983+
"metadata": {
1984+
"collapsed": true
1985+
},
1986+
"outputs": [],
1987+
"source": []
1988+
},
1989+
{
1990+
"cell_type": "markdown",
1991+
"metadata": {},
1992+
"source": [
1993+
"## Polynomials"
1994+
]
1995+
},
1996+
{
1997+
"cell_type": "code",
1998+
"execution_count": null,
1999+
"metadata": {
2000+
"collapsed": true
2001+
},
2002+
"outputs": [],
2003+
"source": [
2004+
"from numpy.polynomial import polynomial as P"
2005+
]
2006+
},
2007+
{
2008+
"cell_type": "markdown",
2009+
"metadata": {},
2010+
"source": [
2011+
"Powers increase from left to right (index corresponds to power)"
2012+
]
2013+
},
2014+
{
2015+
"cell_type": "code",
2016+
"execution_count": null,
2017+
"metadata": {
2018+
"collapsed": true,
2019+
"scrolled": true
2020+
},
2021+
"outputs": [],
2022+
"source": [
2023+
"p1 = P.Polynomial([1, 2])"
2024+
]
2025+
},
2026+
{
2027+
"cell_type": "code",
2028+
"execution_count": null,
2029+
"metadata": {
2030+
"collapsed": false
2031+
},
2032+
"outputs": [],
2033+
"source": [
2034+
"p1.degree()"
2035+
]
2036+
},
2037+
{
2038+
"cell_type": "code",
2039+
"execution_count": null,
2040+
"metadata": {
2041+
"collapsed": false
2042+
},
2043+
"outputs": [],
2044+
"source": [
2045+
"p1.roots()"
2046+
]
2047+
},
2048+
{
2049+
"cell_type": "code",
2050+
"execution_count": null,
2051+
"metadata": {
2052+
"collapsed": false,
2053+
"scrolled": true
2054+
},
2055+
"outputs": [],
2056+
"source": [
2057+
"p4 = P.Polynomial([24, -50, 35, -10, 1])"
2058+
]
2059+
},
2060+
{
2061+
"cell_type": "code",
2062+
"execution_count": null,
2063+
"metadata": {
2064+
"collapsed": false
2065+
},
2066+
"outputs": [],
2067+
"source": [
2068+
"p4.degree()"
2069+
]
2070+
},
2071+
{
2072+
"cell_type": "code",
2073+
"execution_count": null,
2074+
"metadata": {
2075+
"collapsed": false
2076+
},
2077+
"outputs": [],
2078+
"source": [
2079+
"p4.roots()"
2080+
]
2081+
},
2082+
{
2083+
"cell_type": "code",
2084+
"execution_count": null,
2085+
"metadata": {
2086+
"collapsed": false
2087+
},
2088+
"outputs": [],
2089+
"source": [
2090+
"p4.deriv()"
2091+
]
2092+
},
2093+
{
2094+
"cell_type": "code",
2095+
"execution_count": null,
2096+
"metadata": {
2097+
"collapsed": false
2098+
},
2099+
"outputs": [],
2100+
"source": [
2101+
"p4.integ()"
2102+
]
2103+
},
2104+
{
2105+
"cell_type": "code",
2106+
"execution_count": null,
2107+
"metadata": {
2108+
"collapsed": false
2109+
},
2110+
"outputs": [],
2111+
"source": [
2112+
"P.polydiv(p4.coef, p1.coef)"
2113+
]
2114+
},
2115+
{
2116+
"cell_type": "markdown",
2117+
"metadata": {},
2118+
"source": [
2119+
"## Application: polynomial fit"
2120+
]
2121+
},
19802122
{
19812123
"cell_type": "code",
19822124
"execution_count": null,

presentation.tex

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,4 +810,73 @@
810810
\raisebox{0.7truecm}{hint: use \texttt{np.argsort}}
811811
\end{frame}
812812

813+
\begin{frame}{Polynomials in NumPy}
814+
Power series: \texttt{numpy.polynomial.polynomial}
815+
816+
\vspace{0.2truecm}
817+
\structure{Polynomial Class}\\
818+
\small{Polynomial}\\[0.1truecm]
819+
\structure{Basics}\\
820+
\small{polyval, polyval2d, polyval3d, polygrid2d, polygrid3d, polyroots,
821+
polyfromroots}\\[0.1truecm]
822+
\structure{Fitting}\\
823+
\small{polyfit, polyvander, polyvander2d, polyvander3d}\\[0.1truecm]
824+
\structure{Calculus}\\
825+
\small{polyder, polyint}\\[0.1truecm]
826+
\structure{Algebra}\\
827+
\small{polyadd, polysub, polymul, polymulx, polydiv, polypow}\\[0.1truecm]
828+
\structure{Miscellaneous}\\
829+
\small{polycompanion, polydomain, polyzero, polyone, polyx, polytrim, polyline}
830+
831+
\vspace{0.2truecm}
832+
\small{also: Chebyshev, Legendre, Laguerre, Hermite polynomials}
833+
\end{frame}
834+
835+
\begin{frame}{Some examples}
836+
\begin{small}
837+
\structure{\texttt{P.Polynomial([24, -50, 35, -10, 1])}}
838+
\begin{displaymath}
839+
p_4(x) = x^4-10x^3+35x^2-50x+24 = (x-1)(x-2)(x-3)(x-4)
840+
\end{displaymath}
841+
842+
\structure{\texttt{p4.deriv()}}
843+
\begin{displaymath}
844+
\frac{\text{d}p_4(x)}{\text{d}x} = 4x^3-30x^2+70x-50
845+
\end{displaymath}
846+
847+
\structure{\texttt{p4.integ()}}
848+
\begin{displaymath}
849+
\int p_4(x)\text{d}x = \frac{1}{5}x^5-\frac{5}{2}x^4+\frac{35}{3}x^3
850+
-25x^2+24x+C
851+
\end{displaymath}
852+
853+
\structure{\texttt{p4.polydiv()}}
854+
\begin{displaymath}
855+
\frac{p_4(x)}{2x+1} = \frac{1}{2}x^3-\frac{21}{4}x^2+\frac{161}{8}x-\frac{561}{16}
856+
+\frac{945}{16p_4(x)}
857+
\end{displaymath}
858+
\end{small}
859+
\end{frame}
860+
861+
\begin{frame}{Application: polynomial fit}
862+
\begin{center}
863+
\includegraphics[width=0.7\textwidth]{polyfit}
864+
\end{center}
865+
866+
\vspace{-0.5truecm}
867+
\begin{columns}
868+
\begin{column}{0.4\textwidth}
869+
\begin{center}
870+
\includegraphics[width=3truecm]{yourturn}
871+
\end{center}
872+
\end{column}%
873+
\begin{column}{0.6\textwidth}
874+
add some noise to a function and fit it to a polynomial
875+
\end{column}%
876+
\end{columns}
877+
878+
\vspace{0.3truecm}
879+
\small{see \texttt{scipy.optimize.curve\_fit} for general fit functions}
880+
\end{frame}
881+
813882
\end{document}

0 commit comments

Comments
 (0)