General > Off Topic
Here is an interesting math challenge
Numsgil:
What if I told you I wanted to solve e^x = y for x. What does x equal? ln(y), right? But what if I said, "what is a natural log? I do not know how to use that. Express it just using a polynomial, like x^3 + 15x^2 + 10x +2 or something like that"? What would you respond?
This is exactly like that. You CAN NOT express the answer to A^A = B without the Lambert W function. It's an "elementary function", just like sin(x) is an elementary function, and ln(x) is an elementary function. Visual Basic does not have a Lambert W function, so no one can right code which exactly solves A ^ A = B in visual basic. HOWEVER, there is a way to APPROXIMATE the Lambert W function using just polynomials. This is called its Taylor series. All elementary functions with continuous derivatives have a Taylor series which can be constructed.
For Lambert W, this series looks like this: . So choose some arbitrary number of terms (say 5), and there's your Lambert W function.
Botsareus:
:wacko: No, Numsgil I don't mean write the exact code which exactly solves A ^ A = B in visual basic.
I mean write the Taylor series in visual basic until n=100...
And also, I tried plugging "=LN(A2)/(LN(A2)-LN(A2)^2)" into Microsoft Excel (A2 = B). It was not even close... so, the formula you gave me in the beginning: Something like: ln(B )/LambertW(ln(B )) is not correct (as far as I know)
So, what I need is the correct formula, and the source code for that "Lagrange inversion theorem" specifically the one used in the "Taylor series".
Sorry, I am probably driving you crazy with my bad English.
I know it won't be exact , but it will make me understand how the "Lagrange inversion theorem" specifically the one used in the "Taylor series" works.
Numsgil:
Don't think I could do it in VB, but I could probably do it in C/C++/C#/Java pseudocode. Let me see...
--- Code: ---public double LambertW(double x, int terms)
{
double runningSum = 0;
for(int n =1; n <= terms; ++n)
{
runningSum += pow(-n, n-1) / fact(n) * pow(x, n);
}
return runningSum;
}
double fact(double n)
{
for(int I = n - 1; I > 0; --I)
{
n *= I;
}
return n;
}
--- End code ---
But really you need to get used to reading mathematical notation. Code is not the cleanest way of representing mathematical ideas. The syntax is ugly. The Funny looking E is called a sigma, and it means to series sum. The bottom of the E has n = 1, which is an initial condition, and the top of the series as the infinity sign, which means LambertW exactly equals that series if you run the sum for ever and ever. But usually you just want an approximation and so you just do the first 4 or 5 terms at most. There's actually a way to determine the max error the series will give if you just lop off the infinite series after several terms, but it's probably a bit more than you want at the moment.
Note that the infinite series there has a radius of convergence of 1/e. That means it only gives the right answer if x < 1/e. If x > 1/e, you have to recenter the Taylor series, which isn't something I'll get into here. Also, it only gives one of the valid solutions. Just like sqrt(4) gives 2, which is one valid solution.
So let's plug in some numbers. Let's say A ^ A = e^(1/e^2). That means the answer is (1/e^2) / LambertW(1/e^2). So now we apply the first 5 terms of the Taylor series for LambertW and get approximately 1.200. 1/(e^2)/1.200 ~= 1.127. 1.127^1.127 = 1.1443. And e^(1/e^(2)) = 1.1449. So we were able to pretty closely approximate the right answer.
Infinite series article at wikipedia is a good start.
Numsgil:
This is a reasonably good intro article to Lambert W.
Botsareus:
Thank you , now I can read the mathematical notation.
In vb it looks like this:
Const e As Double = 2.71828
Private Sub UserForm_Click()
'A ^ A = e^(1/e^2).
Dim w As Double
Dim d As Double
w = 0.9 'input
d = Log(w) / Log(e)
If Abs(d) > 1 / e Then MsgBox "big" & " " & (Abs(d) - 1 / e)
If d = 0 Then q = 1 Else q = d / bigW(d) 'output
MsgBox q ^ q 'check this with input
End Sub
Function Fact(ByVal n As Long) As Long
If n = 1 Then GoTo b
Fact = n
Z = n
Do
Z = Z - 1
Fact = Fact * Z
Loop Until Z = 1
Exit Function
b:
Fact = 1
End Function
Function bigW(x As Double) As Double
For n = 1 To 12
bigW = bigW + ((-n) ^ (n - 1) / Fact(n) * x ^ n)
Next
End Function
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version