How can I calculate ln(x) in matlab code?

1.890 views (last 30 days)
I am trying to find the syntax for ln(x) in matlab but nothing works. Anyone has an idea?
  11 Comments
Rik
Rik on 7 Dec 2022
I haven't done a thorough survey, but I would guesstimate that it is fairly common to see log used in mathematical computing as the natural log. To avoid this confusion, perhaps Matlab should not have a function called log at all, forcing you to specify the base.
While my high school taught log as base 10, my teacher did point out to us that many people would pronounce ln as log. Since powers of e tend to be very common (more so than 10, outside of orders of magnitude), this choice does have some merit.

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 30 Jun 2015

More Answers (2)

Walter Roberson
Walter Roberson on 18 Mar 2023
There seems to be some conception that "ln" is the "right" way to take natural logarithm, and that MATLAB is... deficient... because it uses log() instead.
I researched a number of different programming languges, and found only two so far (Ktolin and Rust) in which natural log was unequivically ln() and log() was incorrect for natural log. A third language, Maple accepts both ln() and log() for natural log. A few additional languages do not offer natural log, including two in which log() is log base 10.
I did not, in my research, find even one language in which natural log is ln() and log base 10 is log()
Languages that use log() for natural log and do not support ln() for natural log
  • Ada : Ada.Numerics.Generic_Elementary_Functions defines Log with one parameter is natural logarithm; if second parameter is given then that is the base. https://www.adaic.com/resources/add_content/standards/95lrm/ARM_HTML/RM-A-5-1.html
  • C : log() is natural log, log10() is base 10.
  • C++ : log() is natural log, log10() is base 10.
  • C# : Math.Log with one parameter is natural log, Math.Log with two parameters uses second parameter as base
  • Common Lisp : log() with a single parameter is natural logarithm, log() with two parameters uses second parameters as base
  • fortran : LOG is natural log, LOG10 is base 10
  • Go : math.Log is natural logarithm, math.Logb is logarithm base 2, math.Log10 is logarithm base 10
  • java : java.lang.Math.log() is natural logarithm, no other log functions, no two-parameter log
  • javascript : Math.log() is natural logarithm; Math.log10() is base 10. Math.log1p() is natural log of 1 more than the input
  • Lua : math.log is natural log, no other log functions, no two-parameter log
  • Mathematica : Log[] with one parameter is natural logarithm, Log[] with two parameters uses second parameter as base
  • MATLAB: log() is natural log, log2() is base 2, log10() is base 10, log1p() is natural log of 1 + x
  • php : log() with one parameter is natural log, log() with two parameters uses second parameter as base
  • python : math.log() with one parameter is natural log, math.log() with two parameters uses second parameter as base, math.log2() is base 2, math.log10() is base 10, math.log1p() is natural log of 1 + x
  • R : log() with one parameter is natural log, log() with two parameters uses second parameter as base, log2() is base 2, log10() is base 10, log1p() is natural log of 1 + x, logb is base 2 . Some of these are wrappers; not all are available in all editions of R; https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/log
  • Ruby : Math.log with one parameter is natural log, Math.log with two parameters uses second parameter as base, Math.log2() is base 2, Math.log10 is base 10
  • Swift : log() is natural log, log2() is base 2, log10() is base 10, log1p is natural log of 1+x
  • typescript : Math.log() is the natural logarithm, no other log functions, no two-parameter log
Languages which do things differently
  • Algol : LOG calculates log base 10
  • HCL : log() with one parameter is base 10, log() with two parameters is to the given base https://leap.hcldoc.com/help/topic/SSS28S_8.2.1/XFDL_Specification/i_xfdl_g_functions_log.html
  • Kotlin : ln() is natural log, log10() is base 10, log2() is base2, log() is log to given base
  • Maple: ln() is natural log, log2() is base 2, log10 is base 10, log(x) is also natural logarithm, log(x,b) is base b, log[b](x) is base b
  • Pascal : log2() is base 2, log10() is base 10, lnxp1() is natural log of x + 1, log() is log to given base
  • Rust : ln() is natural log, log2() is base 2, log10() is base 10, ln_1p() is natural log of x+1, log() is log to given base
  • Makefile -- does not support logarithm
  • Shell -- csh, ksh, sh, zsh do not support logarithms

MASIA
MASIA on 5 Jun 2025
  • log(x) → υπολογίζει ln(x) (φυσικός λογάριθμος, βάση e)
  • log10(x) → υπολογίζει λογάριθμο με βάση 10
  • log2(x) → λογάριθμος με βάση 2
  4 Comments
DGM
DGM on 7 Jun 2025
Edited: DGM on 7 Jun 2025
log(x) → calculates ln(x) (natural logarithm, base e)
log10(x) → calculate logarithm on base 10
log2(x) → logarithm on base 2
If I'm earnestly trying to communicate in a foreign-language (to me) forum, I prefer to post in my own native language and provide a machine translation. That way I maximize accessibility, while hopefully providing enough redundancy to account for any translation errors to which I might be blind. That said, I admit I'm not always so motivated. :)
Rik
Rik on 8 Jun 2025
Just to clarify: I didn't mean to suggest MASIA was doing anything wrong. I was merely pointing to the presumed difference in users on this forum who are able to read Greek vs English. It wouldn't surprise me if all Greeks visiting this page would be able to read English, while the reverse is definitely not the case.
@DGM That's a very good strategy (although I must admit I'm too lazy to do that for English and too stubborn to do that for French and Spanish).

Sign in to comment.

Categories

Find more on Exponents and Logarithms in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!