Showing posts with label Mathematica. Show all posts
Showing posts with label Mathematica. Show all posts

Friday, December 23, 2011

Automated Mathematica language module BBEdit or TextWrangler


I realize that a few people will find this useful, however those will appreciate it.

If you're a frequent user of Wolfram Mathematica like me, you've probably found yourselves in situations where you could not simply edit your code stored in notebooks (files with.nb extensions) with Mathematica itself. This could be due to various reasons, for example when you don't have Mathematica installed on your current machine or operating system. Luckily, most, if not all, text editors can open and edit notebooks. However, if you use a text editor that supports advanced features like syntax coloring, you will be disappointed when the whole Mathematica code will be in the same default color.
My text editor of choice on OS X is TextWrangler, developed by Bare Bones Software, Inc. In my opinion it captures the perfect balance between the simplicity of use and its many powerful functions. It is also free, in comparison to its bigger brother, BBEdit, which comes with even more features.
Both TextWrangler and BBEdit support syntax coloring for a number of programming languages out of the box, but Mathematica is not one of them. This is not a problem though, as you can easily create a so called language module, a property list (file extension .plist) file with syntax coloring specifications for your language of choice that the text editor will recognize.
Of course, the first thing I did when I found out that TextWrangler does not recognize the Mathematica code was go to google to search for the language module. The only useful result was an old blog post with the property list file made long ago in 2004 for Mathematica 5.0. Now, seven years later, Mathematica has more than doubled the number of its built-in functions, so I was not really satisfied with it. Getting the list of all built-in functions in Mathematica is very easy, so I used parts of that deprecated code to create my automatic generator of the property list file that will always be up to date. It's written in Mathematica and it's only required to be run once every time you install a new version of Mathematica. It supports both TextWrangler and BBEdit. I'm releasing property list files for both TextWrangler and BBEdit (built with Mathematica 8.0.4, in case you don't have access to Mathematica or you have an older version) and the notebook with the generator code under the Creative Commons Attribution-ShareAlike License.

Download the files below:

Saturday, March 26, 2011

Calibrating a scale


A digital scale is an instrument that should be found in any modern laboratory. They are very cheap and come in various capacities and accuracies, from 1000 g ± 0.1 g to 20 g ± 0.001 g. I have bought a 1000 g ± 0.1 g one a while ago and I think they are the most common. All of them should come pre-calibrated, but you can't know if the calibration was accurate for sure. And because usually you don't have a set of calibration weights at home, you don't know how to do it yourself. It's actually quite simple, providing you have some spare change lying around.
My scale has an option for calibration that demands putting on a scale of declared weight from 1000 g down to 50 g in decrements of 50 g. After some calculations I decided that 100 g weight in coins is the best choice. You will soon understand why.

Using this simple brute-force formula in Mathematica I calculated all possible solutions for various weights:
Reduce[e2*8.5 + e1*7.5 + c50*7.8 + c20*5.74 + c10*4.1 +
    c5*3.92 + c2*3.06 + c1*2.3 == 100 && e2 >= 0 && e1 >= 0 &&
  c50 >= 0 && c20 >= 0 && c10 >= 0 && c5 >= 0 && c2 >= 0 &&
  c1 >= 0, {e2, e1, c50, c20, c10, c5, c2, c1}, Integers]
total weightpossible solutionsmin. number of coinsmax. number of coinsmin. value of coinsmax. value of coins
50 g1156200.29 €11.00 €
100 g467412410.57 €22.00 €
150 g5285818620.85 €33.00 €
200 g31847624830.97 €44.00 €
Note that the solution with minimum coins always has the maximum value, and vice versa.

As you can see the number of solutions increases greatly with each weight. It roughly follows an exponential power function[1] shown on the next plot.
Log-plot of possible solutions vs. total weight.
I think the optimal number of coins is about 30. If you'd want to use a larger number of coins it becomes more difficult to place them on the scale in time (the calibration time is limited, about 3 seconds) without any them falling off because you accidentally bumped a pile with your sausage fingers. A calibration with too few coins would be less accurate because every coin weight varies slightly from the declared weight.

Here are all of the solutions for 100 g total weight. You can download it and import in Excel or Mathematica or some other program if you want to play with it.
The solution with fewest coins and maxium value is the following:
10 × 2 €
2 × 1 €
Totaling 12 coins worth 22 €.

The solution with most coins and minimum value is the following:
1 × 10 c
1 × 5 c
3 × 2 c
36 × 1 c
Totaling 41 coins worth just 0.57 €.

And now for some statistics:
A comparison of coin weights by their values.
A comparison of total coins used in all solutions by their values.
You can clearly see the connection between the above plots. The lighter the coin, the more it will be used. No surprises here.
Also interesting are the distributions of the solutions.
Distribution of solutions by total number of coins used. It almost follows the normal distribution, but there is an unexplained shift of the peak to the left of the calculated distribution.
Distribution of solutions by total value of the solution. This one follows a Chi-square distribution instead of the normal distribution. Note that I've rounded the values to 10 cents for a better representation. The plot with original values can be seen here.
And here is my digital scale, freshly calibrated.
The combination is 11 × 10 c; 6 × 5 c; 8 × 2 c; 3 × 1c - the only one possible with the change from my wallet.
I hope you will find this useful and will calibrate your scales if you haven't already. And remember, it's best to use new coins straight from a bank. Dirt, oxides and scratches on the coins will cause an inaccurate calibration, especially with a more sensitive scale.


[1] The name is my own invention for functions of the type y = a^x^b since I couldn't find any information of this type of functions on the internet.