Saturday, 5 November 2022

The Mediant

The mediant is such a simple concept and yet I can't recall ever having heard of the term before.$$ \text{Given two fractions } \frac{a}{b} \text{ and } \frac{c}{d} \text{ such that } \frac{a}{b}<\frac{c}{d} \text{ then}\\ \text{the mediant is defined as } \frac{a+b}{c+d} \text{ where } \frac{a}{b} < \frac{a+b}{c+d} < \frac{c}{d}$$There's a nice visual proof of this inequality to be found on this Mathematical Visual Proofs YouTube channel.

One interesting application of the mediant is a means of finding fractional approximations to irrational and transcendental numbers. I got the idea for this after watching this YouTube video titled Fraction-Finding with the Mediant Method and even though the author uses Python it was not clear what he was doing. However, I grasped what he was on about and wrote my own code which I'll insert below:


I normally just include a permalink but I thought it was about time that I went to the trouble of actually embedding the program. I've used \(e\) as an example but any number can be used as a target e.g. \( \pi \) or \( \sqrt{2} \). The program discards initially the whole number part of the number. So in the case of \(e\), this means discarding 2. What's left is then a decimal number between 0 and 1. We begin by starting with two fractions: one below the number and the other above. If we set \(a=0\) and \(b=1\) and \(c=1\) and \(d=1\) then we know that:$$ \frac{a}{b}=\frac{0}{1}=0 \text{ and } \frac{c}{d}=\frac{1}{1}=1$$are starting points that will always work. The algorithm then generates a new fraction (always 1/2 initially) that may be above or below the (infinite) decimal part of the number that we are trying to approximate. The algorithm ensures that another fraction is always chosen so that it is on the opposite side of the number on the number line. In this way the fractions get closer and closer to the number and the algorithm terminates when the approximation is within a bound that has been set initially.

In the case of \(e\) the following fractions are generated once the integer part has been added back in (so our initial 1/2 becomes 2+1/2 = 5/2 etc):

5/2, 8/3, 11/4, 19/7, 30/11, 49/18, 68/25, 87/32, 106/39, 193/71, 299/110, 492/181, 685/252, 878/323, 1071/394, 1264/465, 1457/536, 2721/1001, 4178/1537, 6899/2538, 9620/3539, 12341/4540, 15062/5541, 17783/6542, 20504/7543, 23225/8544, 25946/9545, 49171/18089, 75117/27634, 124288/45723, 173459/63812, 222630/81901, 271801/99990, 320972/118079, 370143/136168, 419314/154257, 468485/172346, 517656/190435, 566827/208524, 1084483/398959

I set the acceptable error to 0.000000000001 and once the fraction was below this the program stopped. The output that should be displayed above is:

Final approximating fraction for e is 1084483/398959
Decimal approximation is for e is 2.71828182845856
Actual decimal approximation for e is 2.71828182845905

No comments:

Post a Comment