Written and maintained by CASRAI Editorial Board
Last updated
MathWorks also publishes a “Fit Logarithmic Models” page in its Curve Fitting documentation for that last case.
Researchers and analysts who arrive from a calculator, Excel, R or Python usually search this phrase for one reason: MATLAB has no function called ln. The natural logarithm is log(x). This guide covers the call, the other logarithm functions, what happens at zero and below, and how to read the output.
How do you type ln in MATLAB?
You type log(x). MathWorks documents Y = log(X) as returning the natural logarithm ln(x) of each element in array X, and a MathWorks-hosted Answers reply states that MATLAB has no ln function: log computes the natural logarithm. Typing ln(5) raises an undefined-function error; log(5) returns the value.
What it is used for in research
Natural logarithms appear whenever data are skewed or multiplicative: log-transforming a response variable before a regression, computing log-likelihoods, converting growth rates, or linearising an exponential decay so a straight-line fit can be applied. MathWorks also publishes a “Fit Logarithmic Models” page in its Curve Fitting documentation for that last case. For the related setup tasks see generating evenly spaced vectors with linspace and solving equations in MATLAB.
Install or access
MATLAB is a MathWorks product, and log is a core function, so no toolbox is needed to call it. Access options and licensing are described on the MathWorks site (mathworks.com). The function is the same in the Command Window, scripts and live scripts.
Step-by-step: take the natural log in MATLAB
- Put your values in a variable, for example
x = [1 2 10]; - Call
y = log(x). The function is applied to each element and returns an array of the same size. - For a single number,
log(5)works the same way. - To check your understanding of the inverse,
exp(log(x))returnsx(to rounding), becauseexpis the inverse of the natural logarithm.
The input can be a scalar, vector, matrix, N-d array, table or timetable, according to the MathWorks log reference page.
What does the log() function do?
log returns the natural logarithm, the logarithm to base e, element by element. Applied to a matrix it transforms every entry independently; it does not compute a matrix logarithm. For that, MathWorks provides logm, a separate function documented as the matrix logarithm.
What makes ln “natural”?
The base is Euler’s number e (about 2.71828), the constant for which the exponential function ex has itself as its derivative. The natural logarithm is the inverse of that function, which is why it turns up in continuous growth, decay and likelihood calculations. In MATLAB you can obtain the constant with exp(1).
How to use log10 in MATLAB
log10(X) returns the base-10 (common) logarithm of each element. MathWorks gives log10(10) returning 1 as its example, and notes that log10(0) returns -Inf. For real input in the open interval (0, Inf) the result is real; negative or complex input gives a complex result. The same MathWorks Answers thread identifies log2 as the base-2 logarithm and log1p as the natural log of 1+x.
Output and how to read it
| Function | What it returns | Source |
|---|---|---|
log(x) |
Natural logarithm, ln(x), element-wise | MathWorks log reference |
log10(x) |
Base-10 logarithm, element-wise | MathWorks log10 reference |
log2(x) |
Base-2 logarithm | MathWorks Answers (staff reply) |
log1p(x) |
Natural logarithm of 1+x | MathWorks Answers (staff reply) |
reallog(x) |
Listed under log‘s alternatives for raising an error instead of returning a complex result |
MathWorks log reference |
logm(A) |
Matrix logarithm | MathWorks logm reference |
Because the output is element-wise, log([1 2 10]) gives a row vector with three values (0, about 0.6931, about 2.3026). To convert between bases, divide by the log of the target base: log(x)/log(10) equals log10(x) mathematically.
Troubleshooting
Undefined function ‘ln’
Replace ln with log. MATLAB’s convention differs from tools where log means base 10.
You get 0.0000 + 3.1416i instead of an error
For negative input, log returns a complex result: MathWorks shows log(-1) giving 0.0000 + 3.1416i. For complex z = u + iw the real part is the log of the magnitude and the imaginary part is the angle. MathWorks warns this can lead to unexpected results if used unintentionally. If a negative value signals bad data, call reallog, which errors instead, or filter your data first.
You get -Inf
A zero in the input produces -Inf (documented for log10(0)). Remove or offset zeros deliberately, and document the choice in your analysis, rather than letting -Inf flow into a fit.
Taking the log of an image or other array
log works on N-d arrays, but check the data type and zeros first, since image pixels commonly include 0.
Free alternatives and translation table
If you do not have a MATLAB licence, GNU Octave documents log(x) as computing the natural logarithm ln(x) for each element, and also provides log10, log2 and log1p. Octave’s documentation describes reallog as aborting with an error if any element would produce a complex result.
| Natural log in | Call |
|---|---|
| MATLAB | log(x) |
| GNU Octave | log(x) |
| Python (NumPy) | numpy.log(x); see NumPy log |
| R | log(x) (base e is the default) |
| Calculator or spreadsheet | the ln key or LN() |
Online calculators exist for quick checks, but for a reproducible analysis keep the call in a script so the transformation is recorded. Related MATLAB basics: matrices in MATLAB, integration in MATLAB and epsilon (eps) in MATLAB.
FAQ
How do you type ln in MATLAB?
Type log(x). There is no ln function.
How to use log10 in MATLAB?
Call log10(x); log10(10) returns 1.
What makes ln natural?
Its base is e, and it inverts the exponential function ex.
What does the log() function do?
It returns the natural logarithm of every element of its input, returning complex values for negative input.
Sources (official documentation)
- MathWorks,
logreference page: mathworks.com/help/matlab/ref/double.log.html (read 2026-11-07) - MathWorks,
log10reference page: mathworks.com/help/matlab/ref/double.log10.html (read 2026-11-07) - MathWorks Answers, “How can I calculate ln(x) in matlab code?”: mathworks.com/matlabcentral/answers/225896 (read 2026-11-07)
- GNU Octave manual, Exponents and Logarithms: docs.octave.org/latest/Exponents-and-Logarithms.html (read 2026-11-07)







