How to write log base e in MATLAB?

627 vues (au cours des 30 derniers jours)
Houssein Hachem
Houssein Hachem le 1 Oct 2019
Commenté : Walter Roberson le 31 Mai 2023
Screenshot (19).png
I have attached a picture of what i am trying to type in MATLAB. I get an error when i put loge(14-y), so im assuming im typing it wrong and MATLAB cannot understand what i am looking for. Any help would be great, Thank you
  3 commentaires
jinhu
jinhu le 28 Mai 2023
In matlab, the general log (without radix) is equivalent to natural logarithm (e). Generally, the legal radix in matlab is only 2 and 10
John D'Errico
John D'Errico le 28 Mai 2023
Modifié(e) : John D'Errico le 28 Mai 2023
@jinhu - I'm sorry, but that is a meaningless statement. There is no "legal" radix. If you are thinking 2 is a valid radix, since numbers are stored in binary form, you would be vaguely correct. But 10 would simply not apply, since MATLAB only uses a base of 10 to display the numbers. Nothing is stored as a decimal. Anyway, any log computation has essentially nothing at all to do with the way the numbers are stored internally anyway.
If you are talking about syntactic legality, there are THREE syntactically legal log bases: 2, 10, and e, since we have the functions log2, log10, and log in MATLAB. And, if I had to make a bet, I would seriously bet that the log2 and log10 functions merely encode the simple:
log(x,b) = log(x)/log(b)
They might special case certain values of x, so when x==10, you want log10(10) to be exactly 1.
log10(10) == 1
ans = logical
1

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 1 Oct 2019
The log function does exactly what you want.
log(14 - y)
If you want a base 10 log, you use log10. There is also a log2 function, which gives a base 2 log. Other bases are achieved using the simple relation
log(X)/log(b)
produces a log to the base b. You could write a function for it as:
logb = @(X,b) log(X)/log(b);
logb(9,3)
ans =
2
which is as expected.
Finally, there is the reallog function, which also does the natural log, but it produces an error when the log would have been a complex number.
log(-2)
ans =
0.693147180559945 + 3.14159265358979i
reallog(-2)
Error using reallog
Reallog produced complex result.
But for the more normal case, reallog does the same thing as log.
log(2)
ans =
0.693147180559945
reallog(2)
ans =
0.693147180559945
  12 commentaires
Paul
Paul le 29 Mai 2023
I never said "that log() is having to treat binary double precision exp(1) specially to get exactly double precision 1" and I'm not saying it now.
Walter Roberson
Walter Roberson le 31 Mai 2023
syms x real
G=simplify(taylor(log2(x), x, 1.5, 'order', 31),'steps',50)
G = 
double((subs(G,x,1) - log2(sym(1)))/eps)
ans = 0.5013
double((subs(G,x,2) - log2(sym(2)))/eps)
ans = -0.2565
So if we taylor log2(x) over the range [1 2) then the maximum error is less than eps in that range.
Now for any given binary double precision number, break the representation up into exponent and mantissa. Substitute the mantissa 0x3ff for the actual mantissa -- which is equivalent to scaling the number by a power of 2 until it is in the range [1, 2) . Use the taylor'd log2 formula; the result will be less than eps() from what it should be. The log2 of the resulting value will be between 0 and 1 (to within eps). Now add to that log2 the integer difference between the actual binary exponent and 0x3ff : the result will be the log2 of the original number. You can then multiply that log2 of the original number by log2(exp(1)) to get the natural log of the number.
We took advantage of range restriction and the fact that log2() of the exponent scaling factor adds an integer to get a finite number of steps (at most 31) to accurately calculate natural log of a normalized floating point number.
No "clever" algorithm is required.
This is almost certainly not how natural log is calculated. Except possibly for special cases such as NaN or inf or denormalized numbers, MATLAB is very likely just going to call into MKL or similar, such as https://www.intel.com/content/www/us/en/docs/onemkl/developer-reference-c/2023-1/v-ln.html
There is a paper about the IA-64 ("Itanium"); see https://www.cl.cam.ac.uk/~jrh13/papers/itj.pdf .... it does argument reduction and log2 much as I outlined, but there are apparently some additional optimizations.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Exponents and Logarithms dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by