How can I calculate ln(x) in matlab code?
3,861 views (last 30 days)
Show older comments
Fotis
on 30 Jun 2015
Commented: Tamba Siama KOMANO
on 17 Jan 2024
I am trying to find the syntax for ln(x) in matlab but nothing works. Anyone has an idea?
11 Comments
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.
Accepted Answer
More Answers (1)
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
0 Comments
See Also
Categories
Find more on Exponents and Logarithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!