Polynomials & Polyfit

I’m looking at basic use of Polynomials.jl with polyfit, polyval, etc., and have 3 questions:

  1. According to the Polynomials.jl documentation, there is a fourth argument for polyfit: sym, which I can use to specify a symbol of the variable name (e.g., use something else than x).
    -> When I use the sym argument just as in the documentation, I get an error message that this keyword doesn’t exist??
  2. I also tried to Latexify the polynomial, but that didn’t work: is there another way to convert the polynomial to LaTeX code?
  3. I’m trying to fit some thermodynamics data to a polynomial, and get an answer with many digits in each coefficient (obviously).
    -> Is there a “smart” way to explore how much I can reduce the number of digits in each coefficient, and still preserve decent accuracy? (See below… it looks better in presentations if I use few digits in each coefficient, but I don’t want to use so few that the expression is rendered useless…)

Using saturated water data from Table A.6 in:

  • Incropera, F. P., Dewitt, D. P., Bergman, T. L. & Lavine, A. S. (2013), Principles
    of Heat and Mass Transfer, International Student Version, 7 edn
    , John Wiley &
    Sons, Inc., Singapore.

Incropera et al. provide a number of data pairs (T_j, \hat{c}_p(T_j)). I try to fit a polynomial of (fairly standard) form:

\frac{\hat{c}_p(T)}{\hat{c}_p(T^\circ)}\cdot\left(\frac{T}{T^\circ}\right)^2 = a_0 + a_1\frac{T-T^\circ}{T_\circ } + a_2\left( \frac{T-T^\circ}{T^\circ } \right)^2 + \cdots

Since I don’t know \hat{c}_p at the “standard state temperature” T^\circ (which typically is 25\ {}^\circ \mathrm{C}), I use T^\circ=300\ \mathrm{K}.

Here is the resulting polynomial:

0.9998941580920472 + 1.9814548071784748∙x + 1.4911575856609915∙x2 - 2.1653161605721643∙x3 + 6.115963263091598∙x4

This looks ugly when presented in a paper/lecture notes/etc., so I want to make it simpler – without loosing accuracy, which is a rather manual process. I’ve so far ended up with:

0.9999 + 1.981*x + 1.491*x^2 - 2.165*x^3 + 6.12*x^4

leading to:

So my 3rd question is really: can this be automated somehow? Just out of the box, I’m thinking of:

  • testing all possible combinations of number of digits in all coefficients,
  • computing the norm of the error in all cases
  • set up an error tolerance to remove those cases which are too poor
  • set up a measure of the quality of simplification, e.g., the sum of digits over all terms, and select the one with fewest digits over all cases that satisfy the error tolerance

OK – is there a smart way to achieve the same?