how to represent ln in matlab?
How to plot lnx ?
How can i use ln (natural logarithem) function in matlab ?
casting - How do take natural log of a double in MATLAB? - Stack Overflow
Videos
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)
This is a one liner in addition to the for-loop answer provided by @farbiondriven
For 0<x<1 :
sumLn = @(x, n)(sum(((-1).^(0:n-1)).*((x-1).^(1:n))./(1:n)));
sumLn(0.5,10)
ans =
-0.6931
>> log(0.5)
ans =
-0.6931
For x>0.5 :
sumLn = @(x, n)(sum( ((x-1)/x).^(1:n) ./ (1:n) ));
sumLn(2,10)
ans =
0.6931
log(2) =
0.6931
Note: The variable x in this formula is bounded as mentioned in this link.
Try this:
clear
clc
n = input('Enter number of iterations (n): ' );
x = input('enter value of x with abs value < 1 (x): ');
y = zeros(1,n+1);
y(1)=0;
for i = 1:n
y(i+1)= y(i) + ((-1)^(i+1)*(x-1)^i/i);
end
txt = sprintf('The output is: %f', y(n+1))
I need to a plot the function, f(x) =sin(x)ln|x|, ranging from values -5<=x<=5.
I have written out so far
x = -5:5;
y = log(abs(x)) * sin(x)
plot(x,y)
However I keep receiving an error saying that there are incorrect dimensions for matrix mulitplication
Have I input the function in incorrectly?