lettura simple

Logarithms in Octaves

In this lesson I'll explain how to calculate logarithms on any base in Octave with some practical examples.

Natural logarithm

To calculate the natural logarithm use the function log(x)

For example, type log(9)

The result is 2.1972 because e2.1972=9

>> log(9)
ans = 2.1972

Now type log(e)

The result is 1 because e1=e

>> log(e)
ans = 1

Logarithm to base 10

To calculate the logarithm to base 10 use the function log10(x)

For example, type log10(9)

The result is 0.95424 because 100.95424=9

>> log10(9)
ans = 0.95424

Now type log10(10)

The result is 1 because 101=1

>> log10(10)
ans = 1

Logarithm to base 2

To calculate the logarithm to base 2 use the function log2(x)

For example, type log2(9)

The result is 3.1699 because 23.1699=9

>> log2(9)
ans = 3.1699

Now type log2(2)

The result is 1 because 21=1

>> log2(2)
ans = 1

The logarithm on other bases

To calculate the logarithm on a base other than 2, 10, and the natural logarithm (ln), you can use the change of base formula for logarithms.

$$ \log_A x = \frac{\log_B x}{\log_B A} $$

Where A is the arrival base while B is the departure base.

Note. In Octave you can use base 2, 10 or Nepero's number 'e' (natural logarithms) as a departure base (B) because the predefined functions already exist log2(), log10() and log().

I'll give you a practical example.

Calculates the logarithm of 16 over base 4 using the base change formula with decimal logarithms.

$$ \log_4 16 = \frac{\log_{10} 16}{\log_{10} 4} $$

So you can write log10(16)/log10(4) in Octave

>> log10(16)/log10(4)
ans = 2

The result is 2.

If you do a quick test 42 = 16. The result is correct.

Alternatively, you can also calculate the base 4 logarithm of 16 using natural logarithms (log) or base 2 logarithms (log2)

>> log(16)/log(4)
ans = 2
>> log2(16)/log2(4)
ans = 2

The result is the same

This way you can calculate logarithm to any base in Octave.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin