How to write log base e in MATLAB?

620 ビュー (過去 30 日間)
Houssein Hachem
Houssein Hachem 2019 年 10 月 1 日
コメント済み: Walter Roberson 2023 年 5 月 31 日
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 件のコメント
jinhu
jinhu 2023 年 5 月 28 日
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 2023 年 5 月 28 日
編集済み: John D'Errico 2023 年 5 月 28 日
@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

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2019 年 10 月 1 日
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 件のコメント
Paul
Paul 2023 年 5 月 29 日
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 2023 年 5 月 31 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeExponents and Logarithms についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by