Darwinbots Forum
General => Off Topic => Topic started by: Botsareus on April 15, 2009, 03:04:12 PM
-
Rewrite the following equation in terms of A:
A ^ A = B
A = ?
-
You gotta use something called the Lambert W (http://en.wikipedia.org/wiki/Lambert_W_function) function. It's an atomic operation, like trig functions and sqrt is, so your final answer will be in terms of it. Something like: ln(B )/LambertW(ln(B ))
-
Like I said, I can't read calculus, so I need the actual math as an equation.
For example: Let's say :
LambertW(x) = x ^ x
Then I will need the whole thing to be written as:
ln(B )/((ln(B )^ln(B ))
I am however impressed people know this stuff...
-
It can't be done. Just like sin(x) can't be expressed completely using other functions, neither can Lambert W.
If you want to approximate it, try its taylor series (http://en.wikipedia.org/wiki/Lambert_W_function#Taylor_series).
-
A ^ A = B
I have no idea how to read a "Lagrange inversion theorem " too.
Numsgil or somebody, If you have time can you write vb code that solves: Rewrite the equation above terms of A.
As much as I do know the infinity sign means you can sum the calculations infinity. For my purposes a loop from 1 to 100 should be fine, If not at least I can change it later... Just specificity where this loop is in the code.
P.S.
I tried plugging "=LN(A2)/(LN(A2)-LN(A2)^2)" into Microsoft Exsel (A2 = B). It was not even close...
-
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: (http://upload.wikimedia.org/math/5/5/1/55185bdf3010f3768d42951638b72850.png). So choose some arbitrary number of terms (say 5), and there's your Lambert W function.
-
: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.
-
Don't think I could do it in VB, but I could probably do it in C/C++/C#/Java pseudocode. Let me see...
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;
}
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 (http://en.wikipedia.org/wiki/Infinite_series) article at wikipedia is a good start.
-
This is a reasonably good intro article to Lambert W (http://www.americanscientist.org/issues/id.3448,y.0,no.,content.true,page.1,css.print/issue.aspx).
-
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
-
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
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
^
That | is why I hate writing code in BASIC :/
Basic is soooooooo ugly to look at, even if it does run really fast. Oh well, I have to get used to it eventually I suppose.
Oh, and good job understanding Num's math help; he tries to explain Matrices to me for working on DB3, and all I have to say is "Come again?"
-
Sorry, I was a math major after all
-
the source code for that "Lagrange inversion theorem"
-
Ok, ok, here we go, This is done the Russian way:
VERSION 5.00
Begin VB.Form Form1
Caption = "A ^ A = B"
ClientHeight = 5850
ClientLeft = 60
ClientTop = 450
ClientWidth = 8295
LinkTopic = "Form1"
ScaleHeight = 5850
ScaleWidth = 8295
StartUpPosition = 3 'Windows Default
Begin VB.TextBox outp2
Height = 375
Left = 3120
Locked = -1 'True
TabIndex = 3
Top = 4440
Visible = 0 'False
Width = 3255
End
Begin VB.TextBox outp1
Height = 375
Left = 3120
Locked = -1 'True
TabIndex = 2
Top = 3840
Visible = 0 'False
Width = 3255
End
Begin VB.CommandButton solve
Caption = "Solve"
Height = 975
Left = 3120
TabIndex = 1
Top = 2640
Width = 3255
End
Begin VB.TextBox inp
Height = 375
Left = 3120
TabIndex = 0
Top = 1680
Width = 3255
End
Begin VB.Label AAA
Caption = "A:"
BeginProperty Font
Name = "Arial"
Size = 72
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1455
Left = 120
TabIndex = 9
Top = 3360
Visible = 0 'False
Width = 1455
End
Begin VB.Label BBB
Caption = "B:"
BeginProperty Font
Name = "Arial"
Size = 72
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1455
Left = 120
TabIndex = 8
Top = 1080
Width = 1455
End
Begin VB.Label sol2
Caption = "Solution2:"
Height = 255
Left = 2160
TabIndex = 7
Top = 4440
Visible = 0 'False
Width = 855
End
Begin VB.Label sol1
Caption = "Solution1:"
Height = 255
Left = 2160
TabIndex = 6
Top = 3960
Visible = 0 'False
Width = 855
End
Begin VB.Label aprox2
Height = 255
Left = 120
TabIndex = 5
Top = 720
Width = 8055
End
Begin VB.Label aprox1
Height = 255
Left = 120
TabIndex = 4
Top = 240
Width = 8055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Const e As Double = 2.71828
Dim solution1 As Double
Dim solution2 As Double
Dim totsolutions As Byte
Private Sub solve_Click()
Dim mainin As Double
start:
mainin = Val(inp.Text)
If mainin > 1000000000000# Then
If MsgBox("The value you entered is too big. Press OK to put it back in range.", vbOKCancel, "A ^ A = B") = vbOK Then
inp.Text = 1000000000000#
GoTo start
Else
GoTo getout
End If
End If
If mainin < ((1 / e) ^ (1 / e)) Then
If MsgBox("The value you entered is too small. Press OK to put it back in range.", vbOKCancel, "A ^ A = B") = vbOK Then
inp.Text = ((1 / e) ^ (1 / e))
GoTo start
Else
GoTo getout
End If
End If
calc mainin
aprox1.Caption = ""
aprox2.Caption = ""
AAA.Visible = True
outp1.Visible = True
sol1.Visible = False
sol2.Visible = False
outp2.Visible = False
outp1.Text = solution1
If totsolutions = 1 Then
aprox1.Caption = "Approximation difference: " & Abs(solution1 ^ solution1 - mainin)
Else
aprox1.Caption = "Solution1 aproximation difference: " & Abs(solution1 ^ solution1 - mainin)
aprox2.Caption = "Solution2 aproximation difference: " & Abs(solution2 ^ solution2 - mainin)
sol1.Visible = True
sol2.Visible = True
outp2.Text = solution2
outp2.Visible = True
End If
Exit Sub
getout:
aprox1.Caption = ""
aprox2.Caption = ""
AAA.Visible = False
outp1.Visible = False
sol1.Visible = False
sol2.Visible = False
outp2.Visible = False
End Sub
Sub calc(c As Double)
Dim x As Double 'guess
Dim res As Double 'result
If c > 1 Then
q = 1
x = 0
totsolutions = 1
Else
totsolutions = 2
q = 0.1
x = (1 / e)
End If
Do
Do
x = x + q
res = x ^ x
Loop Until Round(res, 14 - Log( c )/ 2) >= Round(c, 14 - Log( c )/ 2)
x = x - q
q = q / 10
Loop Until Round(res, 14 - Log( c )/ 2) = Round(c, 14 - Log( c )/ 2)
x = x + q * 10
solution1 = Round(x, 15)
If c <= 1 Then
q = 0.1
x = (1 / e)
Do
Do
x = x - q
If x < 0 Then res = -1 Else res = x ^ x
Loop Until (Round(res, 14 - Log( c )/ 2) >= Round(c, 14 - Log( c )/ 2)) Or (x < 0)
x = x + q
q = q / 10
Loop Until Round(res, 14 - Log( c )/ 2) = Round(c, 14 - Log( c )/ 2)
x = x - q * 10
solution2 = Round(x, 15)
End If
End Sub