Accelerating the pace of engineering and science

# log

Natural logarithm

## Description

example

Y = log(X) returns the natural logarithm ln(x) of each element in array X.

The log function's domain includes negative and complex numbers, which can lead to unexpected results if used unintentionally. For negative and complex numbers, log returns the complex logarithm

$\mathrm{log}|z|+i\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{atan2}\left(y,x\right)\text{\hspace{0.17em}}.$

Use reallog instead if you want negative and complex numbers to return error messages rather than return complex results.

## Examples

expand all

### Natural Logarithm of Negative Numbers

Show that the natural logarithm of -1 is .

log(-1)
ans =

0.0000 + 3.1416i

### Real-Valued Natural Logarithm

Create a vector of real numbers in the interval [0.25,5].

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



### Complex-Valued Natural Logarithm

Create two Cartesian grids for X and Y.

[X,Y] = meshgrid(0:0.5:1.5,-2:0.5:2);


Calculate the complex natural logarithm on the grid. Use 1i for improved speed and robustness with complex arithmetic.

Z = log(X + 1i*Y)

Z =

0.6931 - 1.5708i   0.7235 - 1.3258i   0.8047 - 1.1071i   0.9163 - 0.9273i
0.4055 - 1.5708i   0.4581 - 1.2490i   0.5893 - 0.9828i   0.7520 - 0.7854i
0.0000 - 1.5708i   0.1116 - 1.1071i   0.3466 - 0.7854i   0.5893 - 0.5880i
-0.6931 - 1.5708i  -0.3466 - 0.7854i   0.1116 - 0.4636i   0.4581 - 0.3218i
-Inf + 0.0000i  -0.6931 + 0.0000i   0.0000 + 0.0000i   0.4055 + 0.0000i
-0.6931 + 1.5708i  -0.3466 + 0.7854i   0.1116 + 0.4636i   0.4581 + 0.3218i
0.0000 + 1.5708i   0.1116 + 1.1071i   0.3466 + 0.7854i   0.5893 + 0.5880i
0.4055 + 1.5708i   0.4581 + 1.2490i   0.5893 + 0.9828i   0.7520 + 0.7854i
0.6931 + 1.5708i   0.7235 + 1.3258i   0.8047 + 1.1071i   0.9163 + 0.9273i



## 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

## Output Arguments

expand all

### Y — Logarithm valuesscalar | vector | matrix | multidimensional array

Logarithm values, returned as a scalar, vector, matrix, or multidimensional array.

For positive real values of X in the interval (0, Inf), Y is in the interval (-Inf,Inf). For complex and negative real values of X, Y is complex. The data type of Y is the same as that of X.