Skip to main content
v2026.11,858 entries · CC-BY 4.0

ln in MATLAB: Use log() for the Natural Logarithm

In MATLAB, ln(x) is log(x). Learn log10, log2, log1p, reallog and why negative inputs return complex values.

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.

  1. Open the Command Window or a script.
  2. Type Y = log(X), replacing X with a scalar, vector, matrix or array.
  3. Run it. The result Y has the same size as X, 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 and log(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)) == x can 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about ln in MATLAB: Use log() for the Natural Logarithm

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →