Written and maintained by CASRAI Editorial Board
Last updated
Short answer: MATLAB has no function called ln in its documented exponents-and-logarithms list. The natural logarithm ln(x) is log(x). Use log10 for base 10 and log2 for base 2. This guide is for researchers and analysts who know ln from mathematics or a textbook and need the exact MATLAB call, plus the one trap (negative inputs) that can silently change results.
This page is the quick answer for the ln lookup. For the longer walk-through, including why the logarithm is called natural, how log10 is used, and a fuller translation table, see Natural Logarithm in MATLAB: ln(x) Is log(x).
What it is used for in research
The natural logarithm ln(x) appears in log-transforming skewed data, fitting exponential and power-law models, log-likelihoods, and growth-rate calculations. In MATLAB all of these use the same function, log. See the related guide on exponential regression for a modelling context where logarithms are used.
Install or access
MATLAB is a MathWorks product; obtain it from MathWorks or through your institution’s licence. The log function is documented in the core MATLAB function reference, and its documentation page names no add-on toolbox. For options that need no MATLAB licence, see the table below and the overview of free statistical software.
How do I do ln in MATLAB?
Call log on a number or an array. Per the MathWorks documentation, Y = log(X) returns the natural logarithm ln(x) of each element in array X.
- Open the Command Window or a script.
- Type
Y = log(X), replacingXwith a scalar, vector, matrix or array. - Run it. The result
Yhas the same size asX, with ln applied element by element.
x = [1 2 10];y = log(x) % natural log of each elementz = exp(y) % exp undoes log: recovers [1 2 10] up to rounding
Is MATLAB log the same as ln? Are log() and ln() the same?
In MATLAB, yes: log is the natural logarithm. This differs from calculators and many textbooks, where “log” often means base 10. In MATLAB the base-10 logarithm is log10.
Which logarithm function do I need?
Function descriptions below are those given in the MathWorks “Exponents and Logarithms” list (read 2026-11-07).
| You want | MATLAB function | MathWorks description |
|---|---|---|
| ln(x) | log(x) |
Natural logarithm |
| Base 10 | log10(x) |
Common logarithm (base 10) |
| Base 2 | log2(x) |
Base 2 logarithm and floating-point number dissection |
| ln(1+x) for small x | log1p(x) |
Compute natural logarithm of 1+X accurately for small X |
| ln(x), error if the result would be complex | reallog(x) |
Natural logarithm for nonnegative real arrays |
| ex | exp(x) |
Exponential |
Logarithms in another base
There is no general-base function in that list. Use the change-of-base identity, logb(x) = ln(x) / ln(b) (a mathematical identity, not a MATLAB-specific feature):
b = 7;y = log(x) ./ log(b) % base-b logarithm of each element of x
For bases 2 and 10 prefer log2 and log10.
Output and how to read it
log(1)is 0 andlog(exp(1))is 1, as for any natural logarithm.- Output is element-wise and the same size as the input.
- Results for non-integer inputs are floating-point values, so identities such as
exp(log(x)) == xcan fail by rounding; compare with a tolerance.
Negative and complex inputs
This is the trap the shortest tutorials skip. The MathWorks page states that for complex and negative real values of X, Y is complex, and that this can lead to unexpected results if used unintentionally. For complex z, MATLAB defines log(z) = log(abs(z)) + 1i*angle(z). The documented example is log(-1), which gives 0.0000 + 3.1416i, that is iπ.
If your data should never be negative (concentrations, counts, intensities), use reallog instead: it is documented as the natural logarithm for nonnegative real arrays, and the MathWorks page recommends it to get an error instead of a complex result.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Undefined function or variable ln |
No function named ln in the documented function list |
Use log |
Output contains i or complex values |
Input has negative or complex elements | Check the data; use reallog to make this an error |
| Values look wrong against a calculator “log” | Calculator log is often base 10 | Use log10 for base 10 |
| Precision loss for tiny x in ln(1+x) | Computing log(1+x) directly |
Use log1p(x) |
Free alternatives
The natural logarithm is named differently across tools. Notes below come from each project’s own documentation (read 2026-11-07).
| Tool | Natural log | Documented behaviour |
|---|---|---|
| MATLAB | log(x) |
Complex output for negative or complex input; reallog for an error instead |
| GNU Octave | log(x) |
Computes ln(x) for each element; also log10, log2, log1p; reallog errors on complex results |
| Python NumPy | numpy.log(x) |
Element-wise natural logarithm; documented example gives -inf for 0; real input whose result cannot be real gives nan with an invalid flag; complex input uses the principal branch |
Note the contrast: for a negative real number MATLAB returns a complex value, while NumPy returns nan for real input. Other MATLAB guides on this site include matrices in MATLAB and the MATLAB equation solver.
FAQ
How do I do ln in MATLAB?
Use log(x). It returns the natural logarithm of each element of x.
How to use ln()?
MATLAB’s documented logarithm functions include no ln(); write log() wherever you would write ln in a formula, for example y = a*log(x) + b.
Is MATLAB log the same as ln?
Yes. MATLAB log is the natural logarithm, base e. Base 10 is log10.
Are log() and ln() the same?
In MATLAB, log() is ln. In many calculators and some textbooks, “log” means base 10, so convert carefully when moving a formula into code.
How do I take log base 10 or base 2?
Use log10(x) or log2(x). For any other base b use log(x)./log(b).
Sources
- MathWorks, MATLAB documentation, “log – Natural logarithm”: mathworks.com/help/matlab/ref/double.log.html (read 2026-11-07).
- MathWorks, “Exponents and Logarithms”: mathworks.com/help/matlab/exponents-and-logarithms.html (read 2026-11-07).
- GNU Octave manual, “Exponents and Logarithms”: docs.octave.org (read 2026-11-07).
- NumPy documentation, “numpy.log”: numpy.org (read 2026-11-07).







