How can I calculate ln(x) in matlab code?
Show older comments
I am trying to find the syntax for ln(x) in matlab but nothing works. Anyone has an idea?
11 Comments
Hadi Mohammadi
on 6 Sep 2020
for finding ln(x) you can use
log(x)/log(exp(1))
Rik
on 10 Feb 2021
Comment posted as flag by Nicolas Seemann-Ricard:
Wrong, log(x) computes ln(x). In the answer it is assumed log stands for log10
Rik
on 10 Feb 2021
You can calculate a log for any base with log(x)/log(base). Since log calculates the natural logarithm, log(exp(1)) is 1. So the comment is correct, it is just needlessly convoluted.
%this will also compute the natural log, may cause rounding errors on the order of eps(x)
log10(x)/log10(exp(1))
Baqer Alhabeeb
on 20 Jul 2022
The syntax of ln(x) in Matlab is log(x)
gunaseakar
on 2 Sep 2022
thank you
gunaseakar
on 2 Sep 2022
in matlab log is not giving log(x) value ,it is giving ln (x) value
Steven Lord
on 2 Sep 2022
Walter Roberson
on 2 Sep 2022
When you say "in matlab log is not giving log(x)", then what is the mathematical definition of "log(x)" that it might otherwise be returning ?
Daniel Joseph
on 7 Dec 2022
The usual notation from most mathematics textbooks is that log(x) returns values of log of base10, while ln(x) returns natural logarithm. I believe the same convention is followed on regular and scientific calculators. Matlab does it slightly differently, which can catch out users hoping to use log() and ln() convention.
I can understand from @gunaseaker point of view, log(x) should return log base 10 values and not base n. Steven Lord's @Steven Lord answer should have cleared the confusion, with the matlab convention defined.
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.
Tamba Siama KOMANO
on 17 Jan 2024
for me , is yes
Accepted Answer
More Answers (2)
Walter Roberson
on 18 Mar 2023
9 votes
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
on 5 Jun 2025
0 votes
- log(x) → υπολογίζει ln(x) (φυσικός λογάριθμος, βάση e)
- log10(x) → υπολογίζει λογάριθμο με βάση 10
- log2(x) → λογάριθμος με βάση 2
4 Comments
Rik
on 6 Jun 2025
Why did you post your answer in Greek? It looks like a valid answer, but only people who can read Greek will benefit. You might consider translating your answer to English to make sure more people understand your post.
Walter Roberson
on 6 Jun 2025
The rule is that people may always post in the language that they feel most comfortable with.
People who cannot happen to read that language are free to ignore the content, or run it through their favorite machine translator.
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
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).
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!