How do take natural log of a double in MATLAB? - 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'

The natural log in MATLAB is simply log(x). You're mixing the two:
log in MATLAB
log in MuPAD
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)

Related

Encounter with a strange Mask initialization Command

So in one of the mask in the model i came across an initialization command that i don't understand.
The logic is for an SR Flip Flop. That latches the output.
It has 1 parameter that takes in the initial state for output 1(Output is o and o(bar)).
Which has the following properties:
Name: MaskParam1
Value: 0
Type: Edit
Evaluate: True
Tunable: True
The command is written in the Initialization tab of the Mask Editor.
ini = (#1~=0);
The first thing that came to my mind was anonymous function handle, that returns value of the parameter. In this case would be 0.
So as (0 ~=0) = 0hence the initial output of SR will be 0.
When i type the following in matlab:
ini = (#1~=0);
I get an error of unexpected matlab expression.
So I tried another thing. Considering as the Parameter is refering to the masked parameter so if we take a variable:
x = 0;
% The default value for the mask parameter
ini = (#x~=0);
This will give an error that
Error: "x" was previously used as a variable, conflicting with its use here as the name of a function or command.
So my question is ini
ini = (#1~=0)
a function or a variable??enter code here

matlab - suppress error message backtrace

I try to check if input argument is of specific type and throw error message like:
function test(input)
if ~ischar(input)
error('%s is invalid input type.', class(input));
end
end
But Matlab shows error message with backtrace-information:
>> test(1)
Error using test (line 3)
double is invalid input type.
How can I turn off the line Error using test (line 3)?
I'm looking for something similar to off backtrace with warning: warning off backtrace;.
I'm not sure you can. The closest I got was by defining my own error structure:
testerr.message = 'test';
testerr.identifier = '';
testerr.stack.file = '';
testerr.stack.name = 'Test Thing';
testerr.stack.line = 1;
error(testerr)
Which returns:
Error using Test Thing
test
As long as you keep the file field blank it will not display the line specified in the stack.
One potential workaround could be a combination of fprintf and return, courtesy of Undocumented MATLAB:
function test(input)
if ~ischar(input)
fprintf(2, '%s is invalid input type.\n', class(input));
return
end
end
Depending on where this check resides in your real function you might need to get creative with how it exits, since return only kicks you back to the invoking function. Probably have it output a True/False flag?

fzero: Too many output arguments

To learn how to work with fzero() I tried this code:
function equation(x)
k=(96-x)/6;
end
and then:
x0=4;
x=fzero('equation',x0)
The error is:
??? Error using ==> fzero at 307
FZERO cannot continue because user supplied function_handle ==> equation
failed with the error below.
Too many output arguments.
fzerois expecting a return value from your equation so (internally) it is trying to assign something to the output of that function. If I try
result = equation(42);
I get the same error message
Error using equation
Too many output arguments.
Just change your function signature to
function [k] = equation(x)
to indicate that k is the output of this function.
Try to supply the function argument as handle:
x = fzero(#equation,x0)

Error running matlab code after compiling

It looks like this has been asked many times, but none of the past posts seem to solve my question. All those had to do with matrix/vector while my code does not have any of these, just simple variables. It takes three variables as arguments. It works perfectly fine within the Matlab environment. I only got the error when I compiled it with mcc -m Normal.m and tried to run with the executable like this "./Normal 1 5 0.5". The complete error message is:
Error using /
Matrix dimensions must agree.
Error in Normal (line 4)
MATLAB:dimagree
It is complaining about line 4: N=2/dt, what is wrong with this?
Here is the code:
function val=Normal(l1,l2,dt)
const=(l2/l1-1);
N=2/dt;
S=1.0/sqrt(l2/l1);
Z(1)=S;
for i=2:N
t= -1+(i-1)*dt;
Z(i)=1.0/sqrt(const*t*t+1);
S=S+2*Z(i);
end
Z(21)=1.0/(l2/l1);
S=S+1.0/sqrt(l2/l1);
val=dt*S/2;
end
But dt is not a scalar when passed into the standalone through the command ./Normal 1 5 0.5. It is a character array with 3 elements ('0', '.','5')!
When passing numerical arguments to a standalone, they are passed as strings. Thus, inside the function, you need to convert '0.5' into a double, and similarly for l1 and l2:
dt = str2num(dt);
l1 = str2num(l1);
l2 = str2num(l2);
Note that you can use isdeployed to determine at runtime if the function is a standalone:
if isdeployed, dt = str2num(dt); end
And you might need to display the result:
if isdeployed, disp(val); end
Result:
>> system('Normal 1 5 0.5');
1.4307
>> Normal(1,5,0.5) % .m function for comparison
ans =
1.4307

Matlab: how to call a function

This is my function
function [mean,stdev] = stat(x)
n = length(x);
mean = sum(x)/n;
stdev = sqrt(sum((x-mean).^2/n));
and I called
[mean stdev] = stat([12.7 45.4 98.9 26.6 53/1])
??? Undefined function or method 'stat' for input arguments of type 'double'.
I also tried
mean,stdev = stat([12.7 45.4 98.9 26.6 53/1])
??? Input argument "x" is undefined.
Error in ==> mean at 30
y = sum(x,dim)/size(x,dim);
Both of them are wrong and I cannot figure out why.
Could you please help me =] Thanks a lot
Your function looks fine to me, therefore I assume that your Matlab "Current Directory" is not the same directory where your function lives.
Another reason might be that the file in which this function exists does not have the same as this function. In order for Matlab to know that this function exists, it has to live in a separate file called stat.m (note how file name is the same as the function name).