Dealing with logarithms is an essential part of technology design. A typical pocket calculator may include keys for very common log functions. Two very common varieties are the LOG and LN operations. The LOG operation can calculate a base-10 logarithm (log10). And LN is assigned to natural logarithms, which have Euler’s number (e ≃ 2.718) as the base.
These common log operations have very broad uses. But they are also restrictive, since it is sometimes necessary to use bases other than 10 and e.
Fortunately, there is a very simple identity that lets you use a base (b) of your choosing. Provided your base is a positive real number greater than 1, you should be set.
Objective
With a normal calculator, you can use the above method to work with any valid base of your choosing. In this tutorial, I will take this one step further by implementing this in the computational tool: Scilab.
The goal is for us to program a function within Scilab’s editor tool: SciNotes that will let us easily use arbitrary log bases in a single command.
The functions will take in the base (b) and the anti-logarithm (x) as input arguments, and then return the result as a return value that we will simply call “logarithm.” This function will basically be structured as:
arbLog(base , antiLog) = logarithm
Arbitrary Base in Scilab
Scilab is free and open-source software for numerical computation providing a powerful computing environment for engineering and scientific applications [1]. Its language and syntax are very similar to MATLAB, albeit not totally drop-in compatible.
It is released as open-source under the GPL License and is available to download for multiple operating systems free of charge. However, if you find it helpful, I recommend supporting the project in any way you can.
Link to Scilab official page: https://www.scilab.org/about
For this procedure, I will be using Scilab 6.0.2. for Windows 10.
Changing Log Base using the Scilab Editor
If you have loaded the Scilab program successfully, you will be greeted with a large console environment like the one pictured above. The console will have a welcome message, and the “–>” prompt.
We will start by creating a new .sci file that will hold the code for the log calculation. This code will contain our own custom function, which we will call “arbLog.” Start by launching the SciNotes editor…
edit arbLog
Next, we need to define the input arguments: base and anti-log. The arbLog function will also need a return value called logarithm, which will be the result of our arbitrary log calculation.
Now it is time to add instructions to perform the log base conversion. Just like the Octave language, “log” means natural log, as opposed to meaning log10 on a pocket calculator. In this example, I have used the base-10 log:
Now save and run your work on arbLog.sci by navigating to Execute –> Save and Execute near the top of the editor window. Alternatively, you can simply press F5.
Most likely, nothing apparent happened. To use arbLog, we need to navigate back to the command line interface that we saw when we first loaded Scilab.
Switch back to the main Scilab window. Once you are back in Scilab, you will probably see something in the command line such as “exec(‘C:\[path to your arbLog.sci file ]’).
Try to run the arbLog function with parameters as shown.
Figure 5: The Custom Function ‘arbLog’ Has Successfully Calculated a Base-2 Logarithm
Our custom arbLog() function can also serve as a replacement for the log and log10 function already included in Scilab. Comparing the results of our arbLog function to the built-in log functions is a good first step toward sanity checking that arbLog is working accurately.
Conclusions
I hope this helps you with your next project. If you are curious, I have also created a similar tutorial for the computational tool: GNU Octave, which you can find here on the Unboxing Tomorrow blog.
References
[1] | Scilab Team, “About | www.scilab.org,” ESI Group, [Online]. Available: https://www.scilab.org/about. [Accessed 26 Oct. 2019]. |
Important Notice: This article and its contents (the “Information”) belong to Unboxing-tomorrow.com and Voxidyne Media LLC. No license is granted for the use of it other than for information purposes. No license of any intellectual property rights is granted. The Information is subject to change without notice. The Information supplied is believed to be accurate, but Voxidyne Media LLC assumes no responsibility for its accuracy or completeness, any error in or omission from it or for any use made of it. Liability for loss or damage resulting from any reliance on the Information or use of it (including liability resulting from negligence or where Voxidyne Media LLC was aware of the possibility of such loss or damage arising) is excluded.