How to Change Log Base in GNU Octave

Changing Log Base in GNU Octave

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).  LN is assigned to natural logarithms, which have Euler’s number (e ≃ 2.718) as the base. The program I will use in this tutorial (GNU Octave) also includes a base-2 logarithm (log2) function called log2.

Figure 1: Parts of a Log Expression

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.

Log Change of Base Method

Objective

With a calculator, you can use the above equation to work with any valid base of your choosing.  In this tutorial, I will implement this identity using the computational math program: GNU Octave.

The goal is for us to program a function within GNU Octave 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 GNU Octave

GNU Octave is a high-level language primarily intended for numerical computations [1].  It’s available on multiple operations systems.  And it’s perhaps most notable (in the engineering community) for being mostly compatible with Matlab. 

It is free software under the GNU General Public License.  However, if you find it helpful, then I recommend supporting the project in any way you can.

Link to GNU Octave official page: https://www.gnu.org/software/octave/about.html

For this procedure, I will be using GNU Octave version 5.1.0 for Windows 10.

Figure 2: The GNU Octave Version 5.1.0 Welcome Screen

Changing Log Base using the Octave Editor

If you have loaded GNU Octave successfully, you will be greeted with a brief welcome message and the “>>” prompt in the large console area.

We will start by creating a new M file for our environment that will hold our code for the arbitrary logarithm.  To start, launch the editor by typing the following command…

edit arbLog.m

This file is new, so you will likely see a dialog message asking you to confirm before Octave will create the file.  Acknowledge, and you should see the editor window appear.

Figure 3: GNU Octave Editor with Empty Function “arbLog”

GNU Octave will pre-populated the arbLog.m file with a title block (shown in green text above).  Below the title block is the actual arbLog function, which we will be building.

By default, the arbLog function is pre-populated with a few function arguments.  The inputs: input1 and input2 are clearly labeled, so we will rename them accordingly.  The return value (retval) is going to carry the result of our calculation, so we will rename it to “logarithm.”

Figure 4: The Function arbLog with Renamed Arguments

Next, we need to add instruction for log base conversion.  Bear in mind that in the Octave language, “log” means natural log, as opposed to meaning log10 on a pocket calculator, and in general mathematics.  In this example, I have used the base-10 log:

Figure 5: The Completed arbLog Function

Now save and run your work on arbLog.m by navigating to Run à Save File and Run near the top of the editor window.  Alternatively, you can simply press F5.

Figure 6: The Save and Execute Function

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 Octave. 

At the very bottom of the Editor window, click the tab that says “Command Window.”

Figure 7: The Tab at the Bottom of the Editor Can Return the View Back to the Command Line Interface

Once you have clicked the Command Window tab, you will probably see an error has occurred in arbLog.  This is fine, and the error simply occurred because we did not pass any arguments to arbLog() when we saved and ran it.

Try to run the arbLog function with parameters as shown. 

Figure 8: 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 functions already included in Octave.  Comparing its results is a good way to sanity check the arbLog function we just wrote to make sure it is working accurately.

Figure 9: Testing the New arbLog Function

Conclusion

I hope this helps you with your next project.

References

[1] GNU Octave, “About,” GNU.org, [Online]. Available: https://www.gnu.org/software/octave/about.html. [Accessed 6 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.