Electronics > Projects, Designs, and Technical Stuff

4th order polynomial coefficients for pressure at temperature readings, Help!!!

<< < (7/12) > >>

ahbushnell:

--- Quote from: itsbiodiversity on August 19, 2020, 02:51:43 pm ---Thank you for such a detailed explanation, and I can clearly see how that technique would be superior.  Would employing that technique produce similarly effective temperature coefficients?  Is there any way to help me identify what those a0-a4 coefficients would be in both of these scenarios.  I can apply this to the unit and report back the effects.

--- End quote ---

Sounds like going back to the source of the parameters and getting clarification would be a good idea. 

Nominal Animal:

--- Quote from: itsbiodiversity on August 19, 2020, 06:35:17 pm ---For all you've done to get me this close I truly appreciate it.  I am trying to calculate these formulas provided using t=21 (is that correct).

--- End quote ---
No, not exactly.  Also check if your environment uses ** for exponentiation and ^ for exclusive-or.

For temperature \$t\$ in °C, we use the formulae

--- Code: ---a0 = 0
a1 = 0.9946289230769221 + 0.0003833040293040879 * t - 0.000006073260073258604 * t * t
a2 = 0.00008539823846153845 - 0.000006168612014652013 * t + 0.00000010009663003663 * t * t
a3 = 0.000003624177384615384 - 0.0000002384970677655678 * t + 0.000000003138913919413919 * t * t
a4 = 0.00000004226482307692308 - 0.0000000009584721611721613 * t - 0.00000000005019706959706964 * t * t

--- End code ---
so for \$t = 21\$ (°C), they are a0=0, a1=1, a2=0, a3=0, and a4=0.

As an example, for \$t = 24\$ (°C), they are 0 +1.000330021978023 -4.992790989010987E-6 -2.91737824175825E-7 -9.652020879120904E-9.

For \$t = 18\$(°C), they are 0 +0.9995606593406599 +6.794530329670339E-6 +3.482382747252735E-7 +8.748473626373614E-9.

The coefficients you specified, best match temperatures \$t \approx -2\$ (°C) or \$t \approx 64\$ (°C), so it is not a surprise the sensor reported pressure out of range, unless the ambient temperature was that extreme (compared to the calibrated range, +15°C to +28°C).


If you want to use the temperature counts (\$c\$) instead, then use

--- Code: ---a0 = 0
a1 = 0.9938037796576857 + 1.268726552948896E-6 * c - 5.8192843849845E-11 * c * c
a2 = 9.895794611233592E-5 - 2.053746129643471E-8 * c + 9.666149779038884E-13 * c * c
a3 = 4.061479723198378E-6 - 7.569847447073744E-10 * c + 2.803896602141317E-14 * c * c
a4 = 3.655168497054565E-8 + 1.412920651658525E-13 * c - 6.891513009452426E-16 * c * c

--- End code ---
At c = 7386 (which corresponds to \$t = 21°C\$), we have a0 = 0, a1 = 0.999999999999997 ≃ 1.000000, a2 = 7.453889935837843e-20 ≃ 0.000000, a3 = -2.117582368135751e-22 ≃ -0.000000, and a4 = 1.985233470127266E-23 ≃ 0.000000 .


Let's check the math anyways, that's always the best option.  I'll use Maxima syntax, but you can adjust it trivially to SageMath, Maple, Mathematica etc.

Let's use the temperature counts.  The weight functions, the fitted pressure compensation functions, and the combined pressure estimate are

--- Code: ---w15(t) := 34976403/3788693 - 16857/7577386*t + 1/7577386*t^2 $
w21(t) := -3463229/264239 + 14956/3963585*t - 1/3963585*t^2 $
w28(t) := 1350407/277027 -12871/8310810*t + 1/8310810*t^2 $
p15(x) := 0.999012*x + 0.0000153908*x^2 + 0.000000752977*x^3 + 0.0000000165934*x^4 $
p21(x) := x $
p28(x) := 1.0006*x - 0.00000884714*x^2 - 0.000000592832*x^3 - 0.0000000239269*x^4 $
p(x,t) := w15(t)*p15(x) + w21(t)*p21(x) + w28(t)*p28(x) $

--- End code ---
The temperature count at \$21°C\$ is \$7386\$, so let's look what the pressure compensation function looks like at that temperature:

--- Code: ---expand(p(x,7386));
--- End code ---
It yields simply x , like it should: no compensation.

The temperature count at \$15°C\$ is \$5485\$, and expand(p(x,5485)); yields 1.65934E-8*x^4+7.52977E-7*x^3+1.53908E-5*x^2+0.999012*x , which is the same as p15(x) :

--- Code: ---expand(p(x,5485) - p15(x));
--- End code ---
yields 0.0.  Similarly for \$28°C\$ at \$9471\$: expand(p(x,9471)-p28(x)) yields 0.

So, we know the weighting function works, we haven't goofed there.

Let's check some of the calibration values, using p(x,t); with x from the fourth column in the tabulated data, t from the third column, and the expected result from the first column. In tabulated form, the results are

--- Code: ---   x     │   t  │   p(x,t)   │ Expectd │ Error
─────────┼──────┼────────────┼─────────┼─────────────────
 -15.017 │ 5485 │ -15.000398 │  -15    │ -0.000398
  -9.01  │ 5485 │  -9.000290 │   -9    │ -0.000290
  -3.004 │ 5485 │  -3.000912 │   -3    │ -0.000912
   0     │ 5485 │   0        │    0    │  0
   3.002 │ 5485 │   2.999194 │    3    │  0.000806
   9.007 │ 5485 │   9.000009 │    9    │  0.000009
  15.008 │ 5485 │  15.000026 │   25    │  0.000026
─────────┼──────┼────────────┼─────────┼─────────────────
 -15     │ 7386 │ -15        │  -15    │  0
  -9     │ 7386 │  -9        │   -9    │  0
  -3     │ 7386 │  -3        │   -3    │  0
   3     │ 7386 │   3        │    3    │  0
   9     │ 7386 │   9        │    9    │  0
  15     │ 7386 │  15        │   15    │  0
─────────┼──────┼────────────┼─────────┼─────────────────
 -14.99  │ 9471 │ -15.000193 │  -15    │ -0.000193
  -8.994 │ 9471 │  -8.999837 │   -9    │  0.000163
  -2.998 │ 9471 │  -2.999864 │   -3    │  0.000136
   0     │ 9471 │   0        │    0    │  0
   2.998 │ 9471 │   2.999701 │    3    │ -0.000299
   8.996 │ 9471 │   9.000093 │    9    │  0.000093
  14.996 │ 9471 │  14.999799 │   15    │ -0.000201

--- End code ---
From this, we can see that the absolute errors are less than 0.001.  The spreadsheet says tolerance is 0.035% of full range, which corresponds to 30×0.035/100 = 0.0105.  Using the function shown above, the results are correct (compared to calibration values) with less than 0.001/30×100% = 0.0033% of (absolute) error.


Let's verify the coefficient functions I gave earlier, for the temperature counts:

--- Code: ---A4(t) := -6.891513009452426E-16*t^2
         +1.412920651658525E-13*t
         +3.655168497054565E-8      $
A3(t) := +2.803896602141317E-14*t^2
         -7.569847447073744E-10*t
         +4.061479723198378E-6      $
A2(t) := +9.666149779038884E-13*t^2
         -2.053746129643471E-8*t
         +9.895794611233592E-5      $
A1(t) := -5.8192843849845E-11*t^2
         +1.268726552948896E-6*t
         +0.9938037796576857        $
A0(t) := 0 $
q(x, t) := A0(t) + A1(t)*x + A2(t)*x^2 + A3(t)*x^3 + A4(t)*x^4 $

--- End code ---
and repeating the table, but using q(x, t) instead of p(x, t), we see that the results are the same.  In fact, expand(p(x,t) - q(x,t)); gives a polynomial with coefficients of the order 10-21 to 10-29, which is close enough to zero for us to say the two polynomials are the same to within the precision we represent them.


If the correctly calculated coefficients – please don't round them, all digits matter – still produce erroneous pressures, then a core assumption I made is incorrect.

I assume the formula the device uses to report the pressure is \$P(p) = C_0 + C_1 p + C_2 p^2 + C_3 p^3 + C_4 p^4\$ (with \$p\$ the internal factory-calibrated sensor value, and \$P(p)\$ the pressure it provides to the user).  (Mainly, because the defaults being 0 1 0 0 0 , it would make sense for it to correspond to \$P(p) = p\$.  However, if OP is not getting sane results, this assumption is wrong.)

If we look at the spreadsheet, it has a separate cell for "constant" (zero – equal to my assumption about no zero drift!), and four X coefficients, but it is the second X coefficient (corresponding to \$C_2\$).  Could it be that the coefficients the device wants are for the temperature and not the pressure?  That it provides \$P(p, t)\$ to the user, internally calculating the pressure as \$P(p, t) = p + T_0 + T_1 t + T_2 t^2 + T_3 t^3 + T_4 t^4\$, with \$T_0\$ to \$T_4\$ being the four coefficients we can specify?  Well, no. You see, if that was the case, we could not get \$P(p_{-}, t) = -15\$, \$P(0, t) = 0\$, and \$P(p_{+},t) = +15\$ for a constant \$t\$ but \$p_{-} \ne -p_{+}\$.

In fact, I cannot imagine right now what the exact formula for \$P(p, t)\$ the device uses, if not \$C_0 + C_1 p + C_2 p^2 + C_3 p^3 + C_4 p^4\$.  This does not mean it cannot be anything else; just that it is something not immediately obvious.  Perhaps \$C_0 + C_1 p + C_2 p^2 + C_3 t + C_4 t^2\$ or something?  I don't know, and I don't see anything specific in the spreadsheet that would help us find what it is.


--- Quote from: itsbiodiversity on August 19, 2020, 06:35:17 pm ---I am kind of "pigeon holed" because the inventor/programmer did the design but all information regarding their application of the statistics is gone to the wind.
--- End quote ---
I know how that feels.  I'd like to help one team to measure pressure in a fusor, and temperature of the vacuum cask, but I seem to be the only one willing to use non-off-the-shelf parts and non-Raspberry Pi SBCs, which makes everything harder.  (Although I do hear that the "I shall randomly drop USB packets because I'm overburdened right now" bugs have mostly worked around nowadays.)
Modular hardware you can build and test in units (with calibration voltage sources, compare to known good measurements) and in increasingly complex combinations, but if you use COTS hardware with LabView-only interfaces or arse-backwards 'Pi libraries that do not even check for short reads and writes, you can only hope stuff works for the duration of the experiment.  (On the other hand, when something goes wrong, you get to blame other people, and not tarnish your own image.)
Me, I like to make robust, reliable stuff on the cheap.

IanB:

--- Quote from: itsbiodiversity on August 19, 2020, 03:49:33 pm ---This may be very dense and my apologies as I am new in this area of science (obviously), but in your calculations, what would the a0, a1, a2, a3, and a4 be?  I am trying to learn how to identify what those are.

--- End quote ---

I'm having trouble following all the details Nominal Animal is posting, but let's simplify it a bit.

There is some difference between the actual pressure reading (call it P) and the assumed true pressure (call it P*). So there is an offset between P and P* (call it deltaP).

So we have:

   deltaP = P - P*

Looking at the data table, it is clear that deltaP depends both on T and P.

So we need to fit a correlating function of the form:

   deltaP = f(P, T)

We might assume, for example:

  deltaP ~ (T-Tref) * (a0 + a1 * P + a2 * P * (T-Tref) + a3 * P^2 + a4 * P^2 * (T-Tref))  (say)

You have to know the actual polynomial assumed by the designer of the equipment. What I wrote here is just for instance.

Then your job is to find values of a0..a4 that best fit the data in the table. You can use Excel for that, or any other tool of your choice.

To simplify matters I chose to use T-Tref because the offset appears to be zero at Tref = 21°C. But really, Tref is just another coefficient in the polynomial.

At the end of the day, the only way to know what a0..a4 are is to know the form of the interpolating polynomial, and the designer has to tell you that.

Nominal Animal:
Yeah, sorry about the excess verbosity, everyone.

I basically just explained, step by step, how to obtain \$P(p) = P_1(t) p + P_2(t) p^2 + P_3(t) p^3 + P_4(t) p^4\$, where \$p\$ is the device-internal pressure calibrated to be correct at \$t = 21°C\$ (which corresponds to \$c = 7386\$), and how to derive \$P_1(t)\$, \$P_2(t)\$, \$P_3(t)\$, and \$P_4(t)\$ from the samples at \$t = 15°C\$, \$t = 21°C\$, and \$t = 28°C\$.  (And the same for using temperature counts \$c\$ instead of temperature \$t\$.)

My core assumption is/was that the five (\$P_0(t) = P_0(c) = 0\$, as there is no zero drift in the calibration data) are coefficients for powers of \$p\$, but that may not be true.  It could be \$P(p, t) = P_0 + P_1 p + P_2 p^2 + P_3 t + P_4 t^2\$, or \$P(p, c) = P_0 + P_1 p + P_2 p^2 + P_3 c + P_3 c^2\$, or something else entirely.

The spreadsheet sorta-kinda suggests the last one (\$P(p,c)\$) – it has no formulae for the derivation of the five coefficients used, just using the column labels as hints –, but \$P(p)\$ would be the most logical one, considering how the device is used.


Actually, if we look at the spreadsheet, a possible form is \$P(p, c) = A_0 p^2 + A_1 p + A_2 c^2 + A_3 c + A_4\$.  If so, the coefficients fitted to the calibration data are
      0.000000669808265654805  0.999876931661194  -0.0000000000881848278027671  0.000000710797927540773  0
If these produce sane/good results, I'll be happy to show how to obtain these using the dataset and Gnuplot, although it is almost trivial.

itsbiodiversity:
HOLY CRAP MAN!!!  That was close.  It actually did not cause the positive pressure linearity to vary from what I set as the "ambient" linearity; however, it caused the formerly near perfect "ambient" negative pressure values to change near identical to the un-adjust reading in Column D (-15.017 psi at a standard of -15 psi).  Basically it went "backwards" to the unadjusted cold temperature value even though I am operating at ambient.  Does this explanation help guide to a portion of the formula???  I am very interested how you got to this point!  Of course the negative pressure was the last data point I took I thought you had it.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod