Matlab

How to Implement Log, ln, and e in MATLAB

MATLAB is a valuable programming language used by scientists and engineers to solve many complicated mathematical problems. Finding exponential and taking the logarithm of a scalar, vector, or multidirectional array are common problems faced by mathematicians in daily life. These problems can be easily solved in MATLAB using the built-in functions; log() and exp().

If you are not familiar with the working of log() and exp() functions, read this guide to learn how to implement these functions in MATLAB.

1: How to Implement log() in MATLAB?

The log() is MATLAB’s built-in function used for finding the natural logarithm of any scalar, vector, or multidirectional array. This function takes a variable having a value as an argument and returns the natural logarithm of that value.

Syntax

To implement the log() function in MATLAB, we follow the given syntax:

y = log(x)

Here,

The function y= log(x) returns the natural logarithm of the specified value or an array of values.

If we pass a negative number and a complex number as a parameter to the log() function, the natural logarithm of that value can be calculated using the given formula:

log(abs(z)) + 1i*angle(z)

where z is a negative number or complex number.

Examples

Follow the given examples to implement the log() function in MATLAB.

Example 1: Implementing the log() Function to Compute the Natural Logarithm of the Given Number

This example describes the implementation of the log() function in MATLAB for a given number.

x = 3.8;
y = log(x)

Example 2: Implementing log() Function to Compute the Natural Logarithm of the Given Vector

The given example demonstrates how to implement the log() function in MATLAB on a given vector.

x = [6 -9 4+7i];
y = log(x)

Note: ln is a common abbreviation used interchangeably with the log() function. So, in MATLAB, you can use the log() function to calculate the natural log, which is the same thing as the ln() function.

2: How to Implement e in MATLAB?

The number e is an Euler’s constant that can be implemented in MATLAB using the exp() function. The exp() is MATLAB’s built-in function that enables us to find the exponentials of any scalar, vector, or multidirectional array. This function accepts a variable containing a value as an argument and returns the exponential of that value.

Syntax

To implement the exp() function in MATLAB, follow the given syntax:

y = exp(x)

Here,

The function y= exp(x) calculates the exponential growth of the specified value or an array of values.

If x = a +ib, the exponential value of x can be computed by the given formula:

e^x= e^a(cos(b) + isin(b))

where x is the real number, whose exponential will be calculated, a is the real part of x, b is the imaginary part of x, and e is the Euler’s constant.

Examples

Follow the given examples to implement the e function in MATLAB.

Example 1: Implementing exp() Function to Compute the Exponential of the Given Number

The following code uses the exp() function in MATLAB to compute the exponential of a given number.

x = 3.8;
y = exp(x)

Example 2: Implementing exp() Function to Compute Exponential of the Given Vector

This example uses the exp() function in MATLAB to compute the exponential of a given vector.

x = [6 -9 4+7i];
y = exp(x)

Conclusion

Finding the natural logarithm and exponential value of a scalar, a vector, or a multidirectional array is a commonly used mathematical problem that can be easily solved in MATLAB using the built-in functions log() and exp(). These functions effectively solve the given mathematical problem. This tutorial has covered the basic guide of implementing log, ln, and e by providing a description of these functions, their syntaxes, and examples to help you learn how to implement them in your codes.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.