Quantcast

Documentation Center

  • Trial Software
  • Product Updates

log

Natural logarithm

Syntax

Description

example

Y = log(X) returns the natural logarithm of each element in array X. The function accepts both real and complex inputs. For real values of X in the interval (0, Inf), log returns real values in the interval (-Inf ,Inf). For complex and negative real values of X, log returns complex values.

Examples

expand all

Calculate Scalar Natural Logarithm Values

Examine several common values of the natural logarithm function.

Calculate the natural logarithm of 1.

log(1)
ans =

     0

The result is 0, which is the x-intercept of the function.

Calculate the natural logarithm of Euler's number, e.

log(exp(1))
ans =

     1

The result is 1 because log is the inverse of exp.

Calculate the natural logarithm of -1.

log(-1)
ans =

   0.0000 + 3.1416i

The result is equal to .

Calculate the natural logarithm of 0.

log(0)
ans =

  -Inf

The result is -Inf because log is a monotonically increasing function over the real domain .

Plot Real-Valued Natural Logarithm

Define the domain.

X = (0.25:0.25:5)';

Calculate the natural logarithm of the vector, X.

Y = log(X)
Y =

   -1.3863
   -0.6931
   -0.2877
         0
    0.2231
    0.4055
    0.5596
    0.6931
    0.8109
    0.9163
    1.0116
    1.0986
    1.1787
    1.2528
    1.3218
    1.3863
    1.4469
    1.5041
    1.5581
    1.6094

The result is a vector of natural logarithm values.

Plot the function values.

plot(X,Y,'LineWidth',1.5);
grid on;
xlabel('X'); ylabel('Y');
title('Real-Valued Natural Logarithm');

The real-valued natural logarithm maps values in the domain $(0,\infty)$ to the range $(-\infty,\infty)$ .

Plot Branches of Complex Logarithm

Define a grid of values for the (X,Y) domain.

[X,Y] = meshgrid(-4:0.25:4,-4:0.25:4);

Calculate the complex logarithm on the grid.

Z = log(X+1i*Y);

Make a surface plot of the imaginary portion of the function.

surf(X,Y,imag(Z))
grid on; hold on;
title('Principal Branch of Im[$\log(X+iY)$]','Interpreter','latex')
xlabel('X'); ylabel('Y'); zlabel('Z');
view(44,42)

On the complex plane, the natural logarithm is a multivalued function that winds around the origin.

To obtain a different branch of the function, add $2\pi i$ to the Z values.

z2 = Z + 2*pi*1i;
surf(X,Y,imag(z2))
title('Two Branches of the Complex Logarithm')
view(45,22)

In this plot, the branches are stacked on top of each other and meet along the negative real axis.

Input Arguments

expand all

X — Input arrayscalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Data Types: single | double
Complex Number Support: Yes

More About

expand all

Algorithms

For complex or negative inputs, log computes the complex logarithm log(abs(z)) + 1i*atan2(y,x).

See Also

| | | | | | | |

Was this topic helpful?