1

I am making a class (in software) in wich several algorithms are used to convert values. Now almost all functions need to go two way, so sg_to_plato(sg) and plato_to_sg(plato)

These algorithms I gather from online sources and literature. Is is all working within reasonable range, but the vice-versa results aren't exactly the same

sg_to_plato(plato_to_sg(18)) is not 18 but 17,9.

I now use for example:

def sg_to_plato(sg):
    return 190.74*math.pow(sg,3) - 800.47*math.pow(sg,2) + 1286.4*sg - 676.67

def plato_to_sg(plato):
    return plato/(258.6-(plato/258.2*227.1))+1

but these two formulas do not seem related.

Can't I "inverse" or "reverse" the formula I use for the first to use in the other? I looked at:Learning how to flip equations but my math is not sufficient to apply the theory.

Question one: Is a formula given 1 input and 1 output always reversible?

Question two: Would someone like to explain how this is done

EDIT: As Gerry Myerson correctly pointed out, the discrepancy could be accounted to the fact I am rounding, but this is not the case. Even unrounded i get approx the same result. I removed the round bit all the same.

Berdus
  • 13
  • 1
    18 is pretty close to 17.9, and considering you have that "round" thing in your code, it's probably entirely rounding error. – Gerry Myerson Jul 10 '13 at 09:31
  • Ah sorry... I did remove the round() function, it did not have the desired effect. The values are expected to be rounded to these specifications by humans – Berdus Jul 10 '13 at 09:34
  • 1
    So are you saying the formulas in the question are not the formulas you actually used? In which case, why have you not edited the formulas in the question so they show what you actually did? How can anyone help you without knowing what you did? – Gerry Myerson Jul 10 '13 at 09:37
  • I adjusted the formulas to remove the round function, but the actual question is not really about the formula at hand, they are to illustrate the problem. The questions are can it be done, and how – Berdus Jul 10 '13 at 09:41
  • The two formulas you are using are not the inverse of one another. Therefore, you should not expect the result to be perfectly accurate. – zuggg Jul 10 '13 at 09:46
  • OK, if I understand your code, you have $$p(s)=190.74s^3-800.47s^2+1286.4s-676.67$$ and $$s(p)=p/(258.6-(p/258.2\times227.1))+1$$ I agree that the two formulas don't seem related, and I can't see any reason for thinking that either one is the inverse of the other. Inverting the formula for $p$ to get a formula for $s$ will be very unpleasant. – Gerry Myerson Jul 10 '13 at 09:46

2 Answers2

0

It appears that (after removing the rounding) your first function is a cubic polynomial $$f(x)=190.72x^3-800.47x^2+1286.4x-676.67. $$ In order to be invertible at all, this must be an injective function, which can be checked via its derivative $$ f'(x)=572.16x^2-1600.94x+1286.4$$ This has no real roots, so $f$ is strictly increasing and hence injective. However, expressing the inverse function is not that easy. There do exist formulas to sovle cubic equations and you'd have to either implement these ore use iterative methods for inversion. Also, I don't know if maybe your supposed inverse function $$ g(x) = \frac{x}{258.6-\frac x{258.2}\cdot 227.1}+1$$ is maybe more reliable than $f$ (as a matter of fact, an inverse of $g$ is quite easy to find).

  • Thanks Hagen, zuggg's answer fits the bill (as does yours), but as zuggg "came first" I accepted his. – Berdus Jul 10 '13 at 10:29
0

It is not always possible to invert a formula. The function at hand has to be bijective. For instance, consider $y(x)=x^2$, for $y=1$, there are two possible values of $x$, $1$ and $-1$, so you cannot express $x$ as a function of $y$.

I do not know where the functions you are using come from, but my guess is that they are only approximations, since they are not the inverse of one another: let $p$ be plato, $s$ be sg, $a=258.6$ and $b=227.1/258.2$, then, using your second algorithm, we have $$ s= \frac{p}{a-bp}+1 $$ so \begin{align} &s(a-bp)=p+a-bp\\ &p(1-b+sb)=sa-a\\ \end{align} $$ p=\frac{a(s-1)}{1+b(s-1)} $$ which is not the formula used in the first algorithm.

zuggg
  • 1,374