<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Polynomials in python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/01/22/Polynomials-in-python</link>
      <pubDate>Tue, 22 Jan 2013 09:00:00 EST</pubDate>
      <category><![CDATA[math]]></category>
      <category><![CDATA[polynomials]]></category>
      <guid isPermaLink="false">o_7DLuKrCG7L5GyaT9VQwy3LacA=</guid>
      <description>Polynomials in python</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://matlab.cheme.cmu.edu/2011/08/01/polynomials-in-matlab/" &gt;Matlab post&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
Polynomials can be represented as a list of coefficients. For example, the polynomial \(4*x^3 + 3*x^2 -2*x + 10 = 0\) can be represented as [4, 3, -2, 10]. Here are some ways to create a polynomial object, and evaluate it.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np

ppar = [4, 3, -2, 10]
p = np.poly1d(ppar)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; p(3)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; np.polyval(ppar, 3)

x = 3
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; 4*x**3 + 3*x**2 -2*x + 10
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
139
139
139
&lt;/pre&gt;

&lt;p&gt;
numpy makes it easy to get the derivative and integral of a polynomial.
&lt;/p&gt;

&lt;p&gt;
Consider: \(y = 2x^2 - 1\). We know the derivative is \(4x\). Here we compute the derivative and evaluate it at x=4.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np

p = np.poly1d([2, 0, -1])
p2 = np.polyder(p)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; p2
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; p2(4)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
 
4 x
16
&lt;/pre&gt;

&lt;p&gt;
The integral of the previous polynomial is \(\frac{2}{3} x^3 - x + c\). We assume \(C=0\). Let us compute the integral \(\int_2^4 2x^2 - 1 dx\).
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np

p = np.poly1d([2, 0, -1])
p2 = np.polyint(p)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; p2
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; p2(4) - p2(2)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
        3
0.6667 x - 1 x
35.3333333333
&lt;/pre&gt;

&lt;p&gt;
One reason to use polynomials is the ease of finding all of the roots using numpy.roots. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; np.roots([2, 0, -1]) &lt;span style="color: #ff0000; font-weight: bold;"&gt;# roots are +- sqrt(2)&lt;/span&gt;

&lt;span style="color: #ff0000; font-weight: bold;"&gt;# note that imaginary roots exist, e.g. x^2 + 1 = 0 has two roots, +-i&lt;/span&gt;
p = np.poly1d([1, 0, 1])
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; np.roots(p)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[ 0.70710678 -0.70710678]
[ 0.+1.j  0.-1.j]
&lt;/pre&gt;

&lt;p&gt;
There are applications of polynomials in thermodynamics. The van der waal equation is a cubic polynomial \(f(V) = V^3 - \frac{p n b + n R T}{p} V^2 + \frac{n^2 a}{p}V - \frac{n^3 a b}{p} = 0\), where \(a\) and \(b\) are constants, \(p\) is the pressure, \(R\) is the gas constant, \(T\) is an absolute temperature and \(n\) is the number of moles. The roots of this equation tell you the volume of the gas at those conditions.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; np
&lt;span style="color: #ff0000; font-weight: bold;"&gt;# numerical values of the constants&lt;/span&gt;
a = 3.49e4
b = 1.45
p = 679.7   &lt;span style="color: #ff0000; font-weight: bold;"&gt;# pressure in psi&lt;/span&gt;
T = 683     &lt;span style="color: #ff0000; font-weight: bold;"&gt;# T in Rankine&lt;/span&gt;
n = 1.136   &lt;span style="color: #ff0000; font-weight: bold;"&gt;# lb-moles&lt;/span&gt;
R = 10.73       &lt;span style="color: #ff0000; font-weight: bold;"&gt;# ft^3 * psi /R / lb-mol&lt;/span&gt;

ppar = [1.0, -(p*n*b+n*R*T)/p, n**2*a/p,  -n**3*a*b/p];
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; np.roots(ppar)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[ 5.09432376+0.j          4.40066810+1.43502848j  4.40066810-1.43502848j]
&lt;/pre&gt;

&lt;p&gt;
Note that only one root is real (and even then, we have to interpet 0.j as not being imaginary. Also, in a cubic polynomial, there can only be two imaginary roots). In this case that means there is only one phase present.
&lt;/p&gt;

&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Summary&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
Polynomials in numpy are even better than in Matlab, because you get a polynomial object that acts just like a function. Otherwise, they are functionally equivalent.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/01/22/Polynomials-in-python.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
