I have the following equation that I want to solve with respect to a
:
x = (a-b-c+d)/log((a-b)/(c-d))
where x
, b
, c
, and d
are known. I used Wolfram Alpha to solve the equation, and the result is:
a = b-x*W(-((c-d)*exp(d/x-c/x))/x)
where W
is the is the product log function (Lambert W function). It might be easier to see it at the Wolfram Alpha page.
I used the Matlab's built-in lambertW
function to solve the equation. This is rather slow, and is the bottleneck in my script. Is there another, quicker, way to do this? It doesn't have to be accurate down to the 10th decimal place.
EDIT: I had no idea that this equation is so hard to solve. Here is a picture illustrating my problem. The temperatures b-d plus LMTD varies in each time step, but are known. Heat is transferred from red line (CO2) to blue line (water). I need to find temperature "a". I didn't know that this was so hard to calculate! :P
a
? As part of some other solver? Are the values ofx,b,c,...
which you want to evaluate all already known? – Elianorex,b,c,d
? My first naive thought is to try to usefzero()
to find the root(s) of(a-b-c+d)/ln((a-b)/(c-d)) - x
, but this can lead to complex answers for some of the random parameter values I've tried. – Devlandlambertw
for a vector instead of a single value? Instead offor i = 1:100, lambertw(i), end
, you can uselambertw(1:100)
, which is about 30 times faster than the original. For 30.000 values, this takes about 30 seconds. – Elianorelambertw
in matlab uses symbolic math, that's a huge overhead. Switch to a numeric implementation. The octave version might be the easiest to port: octave-specfun.sourcearchive.com/documentation/1.0.9-1/… Did not benchmark the code, but with a for loop with only 10 iterations and no other loops it should be fast. – Foramena
,b
,c
,d
, and/orx
? Are there any assumptions on these variables that limit their domain, i.e, do you know if any/all are real-valued or not, are any/all non-negative or or greater/lesser than some value? In some cases, such constraints and assumptions may allow you to simplify things. – Sirenasirenicx
, are real-valued (they're physical values) and positive. Also,a>b
,c>d
always given the logarithmic mean temperature difference problem. Also, using a function specific to the Lambert W or Wright Omega may be faster and more reliable than using a generic root solver likefzero
, but that could work (just be sure to validate it with something else). – Sirenasirenicsingle
precision (this isn't always faster and you have to be careful when and how you convert back and forth todouble
so as not to slow things down unnecessarily or introduce more imprecision), 2) I don't know if or which of your parameter are scalars or vectors, but it would simplify your equation slightly if you solvedx=(delta_ab-delat_cd)/log(delat_ab/delta_cd)
fordelta_ab
and then solved fora
fromdelta_ab=a-b
. If this would sped things up or not would depend on your implementation, and the dimensions of your parameters. – Sirenasirenic