# Polynomials & Polyfit

**URL:** <https://discourse.julialang.org/t/polynomials-polyfit/36309>\
**Category:** General Usage\
**Created:** [March 21, 2020, 6:26pm UTC](https://discourse.julialang.org/t/polynomials-polyfit/36309 "2020-03-21T18:26:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 21, 2020, 6:26pm UTC](https://discourse.julialang.org/t/polynomials-polyfit/36309/1 "2020-03-21T18:26:58Z")

</div>

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:

```julia
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:

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

```

leading to:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b4b322fab4cca2491803b0b38fa8b81ade0d4db.png)

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?
