Showing posts with label WolframAlpha. Show all posts
Showing posts with label WolframAlpha. Show all posts

Tuesday, 16 February 2021

More on the Lambert W Function

I wrote about the Lambert W function in a post titled The Omega Constant and the Lambert W Function on June 24th 2020. Recently, I watched a blackpenredpen video that involved the use of the Lambert W function or the product log as it's sometimes called.


In the video, use was also made of Geogebra and WolframAlpha so this post repeats what is covered in the video and contains screenshots from both these resources. The video deals mainly with the solution of the equation: $$ \sqrt[3]{x}=\ln(x)$$Firstly, let's look at the graphs of \(y=\sqrt[3]{x}\) and \(y=\ln(x)\) using Geogebra. Figure 1 shows a screenshot.


Figure 1: generated using Geogebra

As can be seen in Figure 1, there are two solutions, \(x \approx 6.41\) and \(x \approx 93.35\). Now let's go about solving the equation:$$
\begin{align}
\sqrt[3]{x} &=\ln(x) \\
x^{1/3} &= \ln(x) \\
\ln(x).x^{-1/3} &=1 \\
-1/3. \ln(x).e^{-1/3. \ln(x)} &=-1/3 \\
W \big (-1/3 .\ln(x).e^{-1/3. \ln(x)} \big ) &=W \big (-1/3 \big ) \\
-1/3.\ln(x) &=W(-1/3)\\
x &= e^{-3.W_0(-1/3)} \text{ and } e^{-3.W_{-1}(-1/3) }\\
x & \approx 6.41 \text{ and } 93.35
\end{align}$$Figures 2 and 3 show how WolframAlpha will return the two approximations using the productlog function:


Figure 2



Figure 3

Figure 4 shows the commands needed in SageMathCell to produce these results:


Figure 4

UPDATE: I thought I'd add the following problems to this post rather than to create a new post. Here is the challenge: 

PROBLEM 1: Find the value of \(x\) for \(a^x=bx\) with \(a>1\) and \(b>1\). Test for \(2^x=8x\).

To solve this, let's take the log of both sides:$$
\begin{align}
\log(a^x) &=\log(bx)\\
x \log a &= \log(bx)\\
bx \log a &=b\log(bx)\\
e^{\log(bx)} \log a &=b\log(bx)\\
\frac{\log a}{b} &=\log(bx) \, e^{-log(bx)} \\
\frac{-\log a}{b} &=- \log(bx) \, e^{-log(bx)} \\
W \big (-\frac{\log a}{b} \big )&=W \big ( - \log(bx) \, e^{-log(bx)} \big )\\
W \big (-\frac{\log a}{b} \big )&=-log(bx)\\
\log(bx) &=-W \big (-\frac{\log a}{b} \big )\\
bx&=e^{-W (-\frac{\log a}{b} ) }\\
x&=\frac{ e^{-W (-\frac{\log a}{b} )}}{b} \\
\end{align}$$When we substitute \(a=2\) and \(b=8\) we get:$$
x=\frac{ e^{-W (-\frac{\log 2}{8} )}}{8} \approx 0.1375$$Figure 5 taken from Geogebra confirms this result. \(f\) is \(y=2^x\), \(g\) is \(y=8x\) and A is given as (0,14, 1.1) by the program:


Figure 5

PROBLEM 2: Here is another equation that I came across on YouTube.$$x^2+\log(x)=0$$Here is the solution using the Lambert W function:$$ \begin{align}
x^2+\log(x) &=0\\
1+\frac{\log(x)}{x^2} &=0\\
\log(x) \, x^{-2} &=-1\\
-2 \, \log(x) \,x^{-2} &=2\\
\log(x^{-2}) \, e^{\log(x^{-2})} &=2\\
W(\log(x^{-2}) \, e^{\log(x^{-2})}) &=W(2)\\
\log(x^{-2}) &=W(2)\\
x^{-2} &=e^{W(2)}\\
x^2 &=e^{-W(2)}\\
x &=\sqrt{e^{-W(2)}}\\
x &\approx 0.6529
\end{align}$$Again, Geogebra confirms the result. See Figure 6.

Figure 6


Monday, 21 December 2020

Continued Fractions of Square Roots

It's all too easy, using online resources like WolframAlpha or SageMathCell, to extract a variety of information about continued fractions. For example, today I turned \(26195\) days old and this number is a member of OEIS A042431:


  A042431

Denominators of continued fraction convergents to \( \sqrt{743} \).   



To confirm that \(26195\) is indeed a member of this sequence, I would normally use SageMathCell but others would turn to WolframAlpha and, using the latter, it can be seen in Figure 1 that \(26195\) is indeed a denominator in one of the convergent fractions, specifically:$$ \displaystyle \frac{714024}{26195}$$

Figure 1

It's easy to take for granted that the continued fraction of an irrational number that is a square root (like \( \sqrt{743} \)) is always periodic. A good explanation of why this is so can be found on this site. In the following, I'm reproducing the site's conversion of \( \sqrt{5} \) to a continued fraction (and practising my LaTeX at the same time).
$$\begin{aligned}
\sqrt{5}&=2+x\\
5&=(2+x)^2 \\
&=4+4x+x^2\\
&=4+x\,(4+x)\\
5-4&=x\,(4+x)\\
1&=x\,(4+x)\\
x&=\frac{1}{4+x}\\
\text{ Thus } \sqrt{5}&=2+\frac{1}{4+x}\\
&=2+\frac{1}{4+\displaystyle \frac{1}{4+x}}\\
&=2+\frac{1}{4+\displaystyle \frac{1}{4+\displaystyle \frac{1}{4+x}}} \text{ etc.}\\
\end{aligned}$$Clearly, we can write \( \sqrt{5}=[2;\overline 4]\) where the overline represents repetition. In the case of \( \sqrt{743}\), we have \([27;\overline{3,1,7,27,7, 1, 3, 54}]\). The site referred to earlier goes on to develop a general algorithm for determining the periodic continued fraction of any irrational square root. The table at the end of this post lists the continued fractions of the square roots of the first 99 natural numbers (even those that aren't irrational). 

√n[ a; Period ]
√1[ 1; ]
√2[ 1; 2 ]
√3[ 1; 1, 2 ]
√4[ 2; ]
√5[ 2; 4 ]
√6[ 2; 2, 4 ]
√7[ 2; 1, 1, 1, 4 ]
√8[ 2; 1, 4 ]
√9[ 3; ]
√10[ 3; 6 ]
√11[ 3; 3, 6 ]
√12[ 3; 2, 6 ]
√13[ 3; 1, 1, 1, 1, 6 ]
√14[ 3; 1, 2, 1, 6 ]
√15[ 3; 1, 6 ]
√16[ 4; ]
√17[ 4; 8 ]
√18[ 4; 4, 8 ]
√19[ 4; 2, 1, 3, 1, 2, 8 ]
√20[ 4; 2, 8 ]
√21[ 4; 1, 1, 2, 1, 1, 8 ]
√22[ 4; 1, 2, 4, 2, 1, 8 ]
√23[ 4; 1, 3, 1, 8 ]
√24[ 4; 1, 8 ]
√25[ 5; ]
√26[ 5; 10 ]
√27[ 5; 5, 10 ]
√28[ 5; 3, 2, 3, 10 ]
√29[ 5; 2, 1, 1, 2, 10 ]
√30[ 5; 2, 10 ]
√31[ 5; 1, 1, 3, 5, 3, 1, 1, 10 ]
√32[ 5; 1, 1, 1, 10 ]
√33[ 5; 1, 2, 1, 10 ]
√34[ 5; 1, 4, 1, 10 ]
√35[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78[ 8; 1, 4, 1, 16 ]
√79[ 8; 1, 7, 1, 16 ]
√80[ 8; 1, 16 ]
√81[ 9; ]
√82[ 9; 18 ]
√83[ 9; 9, 18 ]
√84[ 9; 6, 18 ]
√85[ 9; 4, 1, 1, 4, 18 ]
√86[ 9; 3, 1, 1, 1, 8, 1, 1, 1, 3, 18 ]
√87[ 9; 3, 18 ]
√88[ 9; 2, 1, 1, 1, 2, 18 ]
√89[ 9; 2, 3, 3, 2, 18 ]
√90[ 9; 2, 18 ]
√91[ 9; 1, 1, 5, 1, 5, 1, 1, 18 ]
√92[ 9; 1, 1, 2, 4, 2, 1, 1, 18 ]
√93[ 9; 1, 1, 1, 4, 6, 4, 1, 1, 1, 18 ]
√94[ 9; 1, 2, 3, 1, 1, 5, 1, 8, 1, 5, 1, 1, 3, 2, 1, 18 ]
√95[ 9; 1, 2, 1, 18 ]
√96[ 9; 1, 3, 1, 18 ]
√97[ 9; 1, 5, 1, 1, 1, 1, 1, 1, 5, 1, 18 ]
√98[ 9; 1, 8, 1, 18 ]
√99[ 9; 1, 18 ]

Monday, 11 June 2018

Solving Frobenius Equations and Computing Frobenius Numbers

A Frobenius equation is an equation of the form:$$a_1x_n + \ldots + a_nx_n=m$$where \( a_1, \dots, a_n \) are positive integers, \( m \) is an integer and the coordinates \(x_1, \dots, x_n \) of solutions are required to be non-negative integers.

The Frobenius number of \( a_1, \ldots, a_n \) is the largest \( m \) for which the Frobenius equation \(a_1x_n + \ldots + a_nx_n=m \) has no solutions.

These definitions were taken from the Wolfram Language & System Documentation Center Tutorial. A Frobenius equation and number are both seemingly abstract concepts but there are practical, concrete applications. In the tutorial, the following example is given: in how many ways can a total of 42 cents be created using 1, 5, 10, and 25 cent coins? The problem can be expressed as a Frobenius equation:$$x_1+5x_2+10x_3+25x_4=42$$WolframAlpha solves this using the command: FrobeniusSolve[{1, 5, 10, 25}, 42] and the following output is produced:

{{2, 0, 4, 0}, {2, 1, 1, 1}, {2, 2, 3, 0}, {2, 3, 0, 1}, {2, 4, 2, 0}, {2, 6, 1, 0}, {2, 8, 0, 0}, {7, 0, 1, 1}, {7, 1, 3, 0}, {7, 2, 0, 1}, {7, 3, 2, 0}, {7, 5, 1, 0}, {7, 7, 0, 0}, {12, 0, 3, 0}, {12, 1, 0, 1}, {12, 2, 2, 0}, {12, 4, 1, 0}, {12, 6, 0, 0}, {17, 0, 0, 1}, {17, 1, 2, 0}, {17, 3, 1, 0}, {17, 5, 0, 0}, {22, 0, 2, 0}, {22, 2, 1, 0}, {22, 4, 0, 0}, {27, 1, 1, 0}, {27, 3, 0, 0}, {32, 0, 1, 0}, {32, 2, 0, 0}, {37, 1, 0, 0}, {42, 0, 0, 0}}

The Frobenius number is found using FrobeniusNumber[{1, 5, 10, 25}] which yields in this case -1. This is presumably WolframAlpha's way of saying that for this situation there is no such number because one of the building blocks is 1 and that can be used to build any positive integer. Let's look at another Frobenius equation:$$12x_1+16x_2+20x_3+27x_4=123$$Here FrobeniusSolve[{12, 16, 20, 27}, 123] yields:

{{0, 1, 4, 1}, {0, 6, 0, 1}, {1, 4, 1, 1}, {2, 2, 2, 1}, {3, 0, 3, 1}, {4, 3, 0, 1}, {5, 1, 1, 1}, {8, 0, 0, 1}}

The Frobenius number in this case (found using FrobeniusNumber[{12, 16, 20, 27}]) is 89. The actual output is {}. It can be noted that FrobeniusSolve[{12, 16, 20, 27},73]) also produces {} as output but 73<89 and so 73 is not the Frobenius number for the equation. What this means is that the Frobenius equation \(12x_1+16x_2+20x_3+27x_4=m \) always has a solution provided \(m>89\).

Here is a link to the MacTutor archive page about Ferdinand Georg Frobenius. I haven't yet found how to solve Frobenius equations or calculate a Frobenius number in SageMath. The only reference I've found is the following (link) which is clearly not the same:
We can compute the Frobenius coordinates and go back and forth: 
sage: Partition([7,3,1]).frobenius_coordinates()
([6, 1], [2, 0])
 
sage: Partition(frobenius_coordinates=([6,1],[2,0]))
[7, 3, 1]
 
sage: all(mu == Partition(frobenius_coordinates=mu.frobenius_coordinates())
....:     for n in range(30) for mu in Partitions(n))
True

Thursday, 19 April 2018

Sage Reference Manual

I first made a reference to SageMath in a previous blogpost on the 4th January 2017. At that time I installed it but, after tinkering with it a little, I forgot about it until recently. However, my interest has been rekindled and I've installed the latest version (8.1) on my laptop running macOS High Sierra 10.13.3. The online reference manual is located here but instead of a single PDF file, there is an assortment of 70+ PDF files. Because I'm travelling overseas shortly and may not always have Internet access, I wanted to download all the PDF files to have on my laptop for reference.

Fortunately, there is an extension for Chrome called Batch File Downloader that lets you download many links from a  website easily. Here is what the interface looks like:


As the name says, the extension created a batch file that allowed me to download all the *.pdf files in the online directory that it was pointed at. The total size of all files is a little over 80 MB with plotting.pdf (44.9 MB) and plot3d.pdf (20.8 MB) being the two largest. Most of the others are around 1 MB in size. 

Since SageMath uses Python, I also installed the Python  programming language on my laptop. However, my focus will be on getting more proficient at using SageMath. It's tedious to find in Python that most of the basic mathematical functions are missing. For example, if in SageMath one inputs factor(25217), then 151 * 167 is received as output. However, in Python the code must be created:

x, y = 25217, int(25217/2)
for i in range(2, y):
     if x % i == 0:
     print(i)

So the adventure begins. In the meantime, I've come across an interesting site created by a mathematics professor called Gregory V. Bard who writes:
The beauty of Sage is that it works through the internet. There is almost never any reason to do a local install of Sage on your laptop or home computer. This is good news, because it saves a lot of headaches and hassles (especially for students), that you would have to suffer if you were using Mathematica, Maple, Matlab, or Magma. The exception is if you have limited or no internet access, such as in rural areas. 
Internet access involves using the SageMathCell Server which as Professor Bard says is a competitor to WolframAlpha and up until recently was called Sage-Aleph. This is the interface:

Professor Bard has written a book called Sage for Undergraduates that is available as a zipped pdf file by clicking on the link. His site is up-to-date but is "preserving the look-and-feel of the World Wide Web as it was, in 1998".

Using SageMath, I'll be able to wean myself off WolframAlpha which, up until now, I've relied on completely for the factorisation of the number corresponding to my diurnal age and, if the factors are supportive, determining how this number can be expressed as a sum of two squares. Now, using SageMathCell or my own notebook, I can determine both.

Code that is created in SageMathCell can be shared via temporary or permanent links and also by a QR code which conveniently encodes the permalink. The permalink can be quite long and ugly because it encodes the entire block of code. Here is QR code that I generated:

If the QR code is scanned with a QR code reader, you will be taken to the SageMathCell Server where the following code should appear:



Sunday, 15 April 2018

Sum of Squares of Integers and Catalan Numbers

As I began reading a new book Catalan Numbers With Applications by Thomas Koshy, I hadn't progressed far before I came across the statement:$$ \sum_{k=1}^n k^2=\frac{n(n+1)(2n+1)}{6}$$At this point, I had to pause because the author had just assumed this result but I couldn't see how to prove it. I needed to do a little digging but before long I came across an interesting proof of the result on this site. The website starts slowly and works out firstly what the sum of the first n integers will be. Here is how it is worked out: $$ \begin{align} (k-1)^2&=k^2-2k+1\\ \text{Rearranging the terms as below:}\\k^2-(k-1)^2&=2k-1\\ \text{Now sum both sides:}\\ \sum_{k=1}^n (k^2-(k-1)^2)&=2 \sum_{k=1}^n k-\sum_{k=1}^n 1\\n^2&=2S_n-n\\S_n&=\frac{n^2+n}{2}\\&=\frac{n(n+1)}{2} \end{align} $$ After this the website goes on to tackle the sum of the squares of the first n integers as follows (using a similar approach): $$\begin{align} (k-1)^3&=k^3-3k^2+3k+1\\ \text{Rearrange the terms: }\\k^3-(k-1)^3&=3k^2-3k-1\\ \text{Summing both sides:}\\ \sum_{k=1}^n (k^3-(k-1)^3)&=3 \sum_{k=1}^n k^2-3 \sum_{k=1}^n k -\sum_{k=1}^n 1\\n^3&=3 \sum_{k=1}^n k^2 -3 \frac{n(n+1)}{2}-n \\ \sum_{k=1}^n k^2&=\frac{1}{3}n^3+\frac{1}{2}n^2+\frac{1}{6}n\\&=\frac{n(n+1)(2n+1)}{6} \end{align}$$ The website then goes on to establish a general result for the sum of integers raised to any power. The question is asked is there a formula for calculating: $$ 1^a+2^a+3^a+ \cdots + (n-1)^a + n^a=\sum_{k=1}^n k^a \text{ ?} $$Well there is, it's called Faulhaber's Formula and involves Bernoulli numbers but I won't go into that here.

For now, I can go on reading my book about the Catalan numbers. I first made a blog post about Catalan numbers back in 2015 on Tuesday the 29th September. This was the first time I'd really heard of them and I didn't delve deeply into them at all in that post. Hopefully I'll have more to say in later posts about these numbers.

The Catalan numbers are of the form: \( \dfrac{1}{n+1} \dbinom{2n}{n} \)

They can be calculated readily enough:
  • in WolframAlpha using catalannumber[n]
  • in SageMath using catalan_number(n)
Talking of SageMath, I've installed the latest version (8.1) on my Mac and am making a concerted effort to make more use of it. I first made a blog post about this free, open source software program in 2017 on the 4th of January. It really is quite impressive in its capabilities so hopefully I can become more adept at using it. Here's a screenshot from my SageMath notebook:


Lastly, the mathematician who lent his name to these numbers, Eugène Catalan, shouldn't be ignored. He was born on the 30th of May 1814 in Bruges, French Empire (now Belgium) and died on the 14th February 1894 in Liège, Belgium. A biography can be found at MacTutor History of Mathematics archive along with biographies of a great many other mathematicians and other interesting material. I first came across this archive in the early naughties and was fascinated to read about the lives of famous mathematicians who had lent their names to so many mathematical tools that I'd used in previous years. L'Hôpital's Rule was a case in point. Although widely used and a greatly useful mathematical tool, who knows anything about the impressively named Guillaume François Antoine Marquis de L'Hôpital who lent his name to the rule?

on Sunday, March 28th 2021
layout improved