How do take natural log of a double in MATLAB?

I am attempting to that the natural log of a number, I get the message:

tf2 = 60*ln(B1); Undefined function 'ln' for input arguments of type 'double'. 

So i try to cast the number as a float which the documentation claims it will accept but then i get the error message :

float(B1); Error using float (line 50) The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'double' 

So then i try to cast the double as a single and get the same error but it says :

f=single(B1); float(B1); Error using float (line 50) The input argument to float was not a supported type. The only recognized strings are 'single' and 'double'. The input type was 'single' 
4

2 Answers

The natural log in MATLAB is simply log(x). You're mixing the two:

The error message you get is because the function is not defined. You'll get the same error for this line:

bogus_function(1.23) ??? Undefined function or method 'bogus_function' for input arguments of type 'double'. 

I know it's an old question but as I didn't find a good answer when I was trying to do it so I will write my solution for others.

First there is no implemented function to do ln operation in matlab, but we can make it. just remember that the change formula for log base is

log b (X)= log a (X)/log a (B)

you can check this easily.

if you want to calculate log 2 (8) then what you need to do is to calculate log 10 (8)/log 10 (2) you can find that: log 2 (8) = log 10 (8)/log 10 (2) = 3 So easily if you want to calculate ln(x), all you need is to change the base to the e.

ln(x) = log 10 (x)/log 10 (e)

so, just write that code in matlab

my_ln= log 10 ( number ) / log 10 ( exp(1) ); 

you can also make it as a function and call it whenever you need it,

function [val] = ln_fun(number) val = log 10 (number)/ log 10 ( exp(1) ); end 

*remember the log general formula → log base (number)

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like