lua-users home
lua-l archive

Re: Symbolic equation solving in Lua

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


In message <CAGmA6cGxgim9d1GQB0+r6nb4KOkBcmVxBAYo5QEntokkQpbNEg@mail.gmail.com>
 Hur Butun <hurbutun@gmail.com> 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 (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

AltStyle によって変換されたページ (->オリジナル) /