Ln in MATLAB
The ln()
function in MATLAB returns the natural logarithm of a number. The natural logarithm is the logarithm to the base of e, which is approximately 2.718281828459045.
The syntax for the ln()
function is as follows:
ln(x)
where x
is the number whose natural logarithm is to be calculated.
For example, the following code calculates the natural logarithm of 10:
x = 10; ln(x)
ans = 2.302585092994046
The ln()
function can also be used to calculate the natural logarithm of an array of numbers. In this case, the function returns an array of the natural logarithms of the corresponding elements in the input array.
For example, the following code calculates the natural logarithms of the numbers from 1 to 10:
x = 1:10; ln(x)
ans = 0.000000000000000 0.693147180559945 1.098612288668109 1.386294361119891 1.609437912434100 1.791759469228055 1.945910149055313 2.079441541679836 2.197224577336219
Questions related to Ln in MATLAB
- What is the difference between the
ln()
function and thelog()
function in MATLAB?
The log()
function in MATLAB returns the logarithm of a number to a specified base. The default base for the log()
function is 10.
For example, the following code calculates the logarithm of 10 to the base 2:
x = 10; log(x)
ans = 3.321928094887362
The ln()
function, on the other hand, always returns the natural logarithm of a number, regardless of the specified base.
- How can I calculate the natural logarithm of a negative number in MATLAB?
The ln()
function cannot calculate the natural logarithm of a negative number. If you try to do so, MATLAB will return an error message.
To calculate the natural logarithm of a negative number, you can use the reallog()
function. The reallog()
function returns the real part of the natural logarithm of a complex number.
For example, the following code calculates the natural logarithm of -1:
x = -1; reallog(x)
ans = -inf
- How can I calculate the natural logarithm of a complex number in MATLAB?
The ln()
function cannot calculate the natural logarithm of a complex number. To do so, you can use the log()
function.
The log()
function can be used to calculate the logarithm of a complex number to any specified base. For example, the following code calculates the natural logarithm of 2 + 3i:
x = 2 + 3i; log(x)
ans = 1.386294361119891 + 1.098612288668109i
The real part of the output of the log()
function is the natural logarithm of the real part of the input complex number, and the imaginary part of the output is the natural logarithm of the imaginary part of the input complex number.