log
R2026bNatural logarithm
Syntax
Description
returns
the natural logarithm ln(x) of
each element in array Y = log(X)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 z
= u + i*w, the complex logarithm log(z) returns
log(abs(z)) + 1i*angle(z)
If you want negative and complex numbers to return error messages
rather than return complex results, use reallog instead.
Examples
Input Arguments
Input array, specified as a scalar, vector, matrix, multidimensional array, table, or timetable.
Data Types: single | double | table | timetable
Complex Number Support: Yes
Output Arguments
Logarithm values, returned as a scalar, vector, matrix, multidimensional array, table, or timetable.
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.
Extended Capabilities
The
log function fully supports tall arrays. For more
information, see Tall Arrays.
Usage notes and limitations:
When the input value
xis real, but the output should be complex, simulation ends with an error. To produce the complex result, make the input value complex by passing incomplex(x).
Usage notes and limitations:
When the input value
xis real, but the output should be complex, simulation ends with an error. To produce the complex result, make the input value complex by passing incomplex(x).
The log function supports HDL code generation with native
floating-point for single and double data types. For more
information, see Support for Functions in Math Function Block (HDL Coder).
The log
function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
The log function
supports GPU array input with these usage notes and limitations:
If the output of the function running on the GPU can be complex, then you must explicitly specify its input arguments as complex. For more information, see Work with Complex Numbers on a GPU (Parallel Computing Toolbox).
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
The
log function fully supports distributed arrays. For more
information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aThe log function shows improved performance when computing
natural logarithms in double precision. For example, this code computes the natural logarithms
of 100,000,000 real numbers within the interval of 0 to 1000. The code is about 2.8x faster than
in previous releases.
function t = timingLog x = rand(1,1e8)*1000; y = @() log(x); t = timeit(y); end
The approximate execution times are:
R2025a: 0.79 s
R2026a: 0.28 s
The code was timed on a Windows® 11, AMD EPYC™ 74F3 24-Core Processor @ 3.19 GHz test system by calling the
timingLog function.
This performance improvement arises from code changes that also result in slightly different round-off behavior in double precision, leading to more accurate results in general. For example, this code now returns a result that is more accurate to the 15th digit after the decimal point in long scientific notation.
format longE
Y = log(1.63340913276288)Y =
4.906693231856701e-01Previously, the same code returned this result.
format longE
Y = log(1.63340913276288)Y =
4.906693231856700e-01Note that MATLAB® converts a decimal number input, like 1.63340913276288, to the
nearest representable double-precision binary value, which might not be exactly equal to the
original decimal input. For this reason, although the log function is now
more accurate in double precision, the result might differ from a calculation performed using
the exact representation of the input.
The log function can calculate on all variables within a table or
timetable without indexing to access those variables. All variables must have data types
that support the calculation. For more information, see Direct Calculations on Tables and Timetables.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)