How do I calculate a logarithm of a variable base?

조회 수: 156 (최근 30일)
Tzahi Shukrun
Tzahi Shukrun 2021년 10월 2일
댓글: Tzahi Shukrun 2021년 10월 2일
Hi!
I tried to calculate a log of a varying base - no success. I saw that calculating log of the base of 10 or e is possible.
for an example: after typing log (a,b) - I recieve an ERROR message.
I'd like to use "a" as a varying number and change it using a loop until I'll get a good result. "a" can be for an example - 1.05, 1.003....
Here it is:
>> a=1.005;
>> log(a,2)
Error using log
Too many input arguments.
Can you please guide me how doing it?

답변 (3개)

John D'Errico
John D'Errico 2021년 10월 2일
편집: John D'Errico 2021년 10월 2일
Just write your own function. Save it on your search path. Now you have it.
[log(10),logb(10)] % natural log
ans = 1×2
2.3026 2.3026
[log10(5),logb(5,10)] % log(10) to base 5
ans = 1×2
0.6990 0.6990
[log2(3),logb(3,2)] % log(2) to base 3
ans = 1×2
1.5850 1.5850
logb(16,[2 3 4 5 16]) % log(16) to the bases [2,3,4,5,16]
ans = 1×5
4.0000 2.5237 2.0000 1.7227 1.0000
Simple enough.
function L = logb(X,Base)
% log(X) to the base Base
% If Base is not supplied, then the natural log is presumed.
if (nargin < 2) || isempty(Base)
% default case is the natural log
L = log(X);
else
% test for an invalid base
if any(Base<=0) || any(Base == 1)
error('Base must be > 0, and ~= 1')
end
L = log(X)./log(Base);
end
end

DGM
DGM 2021년 10월 2일
Nowhere in the synopsis for log() does it say that it accepts two arguments.
For base 2, you can just use log2()
More generally, just remember the properties of logarithms and use either log(), log10(), or log2() to compute the arbitrary base-n logarithm.

Star Strider
Star Strider 2021년 10월 2일
The general approach is:
So:
a = 64
a = 64
log(a)/log(2)
ans = 6
2^(log(a)/log(2))
ans = 64
See the Wikipedia article on List of logarithmic identities for an extended discussion.
.
  댓글 수: 1
Tzahi Shukrun
Tzahi Shukrun 2021년 10월 2일
First, thank you all for your answers. I wrote it and calculated it that way. Now Im sure that matlab dont have a direct command for this action
I did it using excel and wondered about this possibility using matlab.
Again, thank you very much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by