Symbolic equation solving in Lua

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Symbolic equation solving in Lua

Hur Butun
Hello,

Is there a symbolic equation solver / package in Lua?

I am trying to solve the Colebrook equation (below)

I saw that there are some simple equation solvers available online but I couldn't find anything to solve an equation of this kind.

Best,
Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Gavin Wraith
In message <CAGmA6cFbvCgcKVKdpL3Y2wFH3N=[hidden email]>
          Hur Butun <[hidden email]> wrote:

>Is there a symbolic equation solver / package in Lua?
>
>I am trying to solve the Colebrook equation (below)
>https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html
>
>I saw that there are some simple equation solvers available online but I
>couldn't find anything to solve an equation of this kind.

Depends very much on what you mean by solve.

I hope you will excuse me using this reply to mention the arrest of Abbas
Edalat, a professor of Computer Science at Imperial College London, during
an academic function in Iran. He would be just the man to help you. His
thesis was on a practical package to implement a real number representation, using
lazy streams of rationals, in which the standard laws of arithmetic hold on
the nose.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Hur Butun
Thank you for your reply Gavin. I am sorry for Prof. Edalat, I hope they will release him asap!

I don't really understand what could be the different meanings of solve in this case. As I mentioned in my previous message, I would like to obtain the value of the friction factor using Colebrook equation.



On 30 April 2018 at 11:47, Gavin Wraith <[hidden email]> wrote:
In message <CAGmA6cFbvCgcKVKdpL3Y2wFH3N=[hidden email]>
          Hur Butun <[hidden email]> wrote:

>Is there a symbolic equation solver / package in Lua?
>
>I am trying to solve the Colebrook equation (below)
>https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html
>
>I saw that there are some simple equation solvers available online but I
>couldn't find anything to solve an equation of this kind.

Depends very much on what you mean by solve.

I hope you will excuse me using this reply to mention the arrest of Abbas
Edalat, a professor of Computer Science at Imperial College London, during
an academic function in Iran. He would be just the man to help you. His
thesis was on a practical package to implement a real number representation, using
lazy streams of rationals, in which the standard laws of arithmetic hold on
the nose.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/


Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Gavin Wraith
In message <[hidden email]>
          Hur Butun <[hidden email]> wrote:

> I don't really understand what could be the different meanings of solve in
> this case. As I mentioned in my previous message, I would like to obtain
> the value of the friction factor using Colebrook equation.

Sorry - I meant "solve for which variable?". If that is "k" then
what you need is

local x = math.sqrt(lambda)
(exp(-1/(2*x))-2*51/(R*x))/(3.72*d)

On the other hand if you want lambda in terms of R, the Reynold's number, k and
d (hydraulic diameter) then you need

lambda = 1/(math.sqrt (fixpoint (f)))

where

f = function (x)
  local a = ((2.51/R)*x + k/(3.72*d))
  return -2*math.log(a)
  end

where fixpoint(f) solves f(x) == x. To do this you need an initial guess, say 1,
a choice of accuracy epsilon, say 10^(-7) and the Newton-Raphson method:

local df = function (x) -- derivative of f
           local c = -2*(2.51)/R
           local y = (2.51*x)/R + k/(3.72*d))
           return c/y
           end

fixpoint = function (f)
           local g = guess
           while math.abs(g-f(g)) < epsilon do
              g = g - f(g)/df(g)
           end
           return g
           end

This is totally off the cuff, so E&OE.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Gavin Wraith
In reply to this post by Hur Butun
Oh *******!. That line should have been

while math.abs(g-f(g)) > epsilon do

in the definition of fixpoint. The moral is, if you actually want to understand
what you are doing rather than simply use a "package", then read up
on the Newton-Raphson method of finding fixpoints (Wikipedia is your friend),
read SICP (Structure and Interpretation of Computer Programs) by Abelson
and Sussman, who cover the whole business rather readably, and translate from
Scheme to Lua. That way you will avoid the stupid mistakes of dumbasses like me
and you will be doing yourself a favour, and perhaps enjoy yourself.

--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Albert Chan
In reply to this post by Gavin Wraith
On Apr 30, 2018, at 7:11 AM, Gavin Wraith <[hidden email]> wrote:
fixpoint = function (f)
          local g = guess
          while math.abs(g-f(g)) < epsilon do
             g = g - f(g)/df(g)
          end
          return g
          end

Using while do loop is a bad idea, as it can easily get 
into infinite loop.  It would be better if the solver "give-up" 
if it cannot converge within some maximum iterations.

Another problem is the df function.
A symbolic df function may be hard to get (or messy)

Even the f function may be hard to get.
Most engineering problem involve simultaneous system of
equations, which were hard to simplify into a single f

It is much easier using, say TK solver, instead of Lua.
Instead of a bunch of equations for solving different variable,
Just leave the input field(s) blank (or guess values), and type !

Excel spreadsheet solver is another option.



Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Gavin Wraith
In message <[hidden email]>
          Albert Chan <[hidden email]> wrote:

>Using while do loop is a bad idea, as it can easily get
>into infinite loop.  It would be better if the solver "give-up"
>if it cannot converge within some maximum iterations.
>
>Another problem is the df function.
>A symbolic df function may be hard to get (or messy)

Yes, there are lots of ways to find fixed points without using
derivatives.

>Even the f function may be hard to get.
>Most engineering problem involve simultaneous system of
>equations, which were hard to simplify into a single f
>
>It is much easier using, say TK solver, instead of Lua.
>Instead of a bunch of equations for solving different variable,
>Just leave the input field(s) blank (or guess values), and type !
>
>Excel spreadsheet solver is another option.

I agree with you Albert. If what you want is just a number then use a package,
where the gotchas have been properly sorted. If what you want is understanding
(not clear from original question) then reinvent wheel. Educational administrators
frequently fail to understand that learning means wheel-reinvention, but that
research students need to build on other people's efforts, i.e. packages. So
the administrators should be the last people to have a say in what software to use.
--
Gavin Wraith ([hidden email])
Home page: http://www.wra1th.plus.com/

Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Albert Chan

> On Apr 30, 2018, at 9:03 AM, Gavin Wraith <[hidden email]> wrote:
>
> If what you want is just a number then use a package,
> where the gotchas have been properly sorted. If what you want is understanding (not clear from original question) then reinvent wheel.

Also, do not trust the "number" too much.

Engineering formula is normally not "derived", but fitted.
The data points (to be fitted) also have errors.

-> number is an estimate of an estimate of an estimate
-> do not expect the package to remove all the gothas



Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Viacheslav Usov
In reply to this post by Gavin Wraith
On Mon, Apr 30, 2018 at 1:11 PM, Gavin Wraith <[hidden email]> wrote:

f = function (x)
>  local a = ((2.51/R)*x + k/(3.72*d))
>  return -2*math.log(a)
>  end

This can be simplified further, to z = A - 2 log (z + B), to be solved for z.

There is actually a closed form solution using the Lambert W function [1], but I am afraid computing the value of the function is equivalent to solving the equation numerically.

Cheers,
V.


Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Albert Chan
In reply to this post by Hur Butun

> On Apr 30, 2018, at 5:21 AM, Hur Butun <[hidden email]> wrote:
>
> Is there a symbolic equation solver / package in Lua?
>

I don't think there is any (emphasis: symbolic)


Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Dirk Laurie-2
In reply to this post by Hur Butun
2018-04-30 11:21 GMT+02:00 Hur Butun <[hidden email]>:

> Is there a symbolic equation solver / package in Lua?
>
> I am trying to solve the Colebrook equation (below)
> https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html
>
> I saw that there are some simple equation solvers available online but I
> couldn't find anything to solve an equation of this kind.

As that website says, the equation can only be solved by iteration,
except, as Vladimir says, you admit the Lambert W function into your
repertoire.

The best, if not the only, package allowing you to program in Lua
while accessing a formidable array of scientific software packages is
gsl-shell.

http://www.nongnu.org/gsl-shell

Reply | Threaded
Open this post in threaded view
|

Re: Symbolic equation solving in Lua

Hur Butun
Thank you all for your replies.

I decided to use one of the explicit estimations of the Colebrook equation.

Best,

On 30 April 2018 at 19:34, Dirk Laurie <[hidden email]> wrote:
2018-04-30 11:21 GMT+02:00 Hur Butun <[hidden email]>:

> Is there a symbolic equation solver / package in Lua?
>
> I am trying to solve the Colebrook equation (below)
> https://www.engineeringtoolbox.com/colebrook-equation-d_1031.html
>
> I saw that there are some simple equation solvers available online but I
> couldn't find anything to solve an equation of this kind.

As that website says, the equation can only be solved by iteration,
except, as Vladimir says, you admit the Lambert W function into your
repertoire.

The best, if not the only, package allowing you to program in Lua
while accessing a formidable array of scientific software packages is
gsl-shell.

http://www.nongnu.org/gsl-shell