Matlab

How to Find the Magnitude of a Vector in MATLAB?

MATLAB is an interactive programming environment used in scientific computing. It is often applied in a variety of technological domains where experimentation, data analysis, and, problem-solving are important. A significant portion of discipline-specific software is developed using MATLAB. This blog will discover how to calculate a vector’s magnitude in MATLAB.

How to Determine the Magnitude of a Vector in MATLAB?

There are two ways to calculate the magnitude of a vector in MATLAB.

Now we are going to discuss how to use these methods for finding the magnitude of a vector in MATLAB.

1: Using norm() Function

The built-in MATLAB function n = norm(v) returns the Euclidean norm of the vector v. The 2-norm, vector magnitude, and Euclidean length are other names for this function. For example:

v = [1 2 3 4 5 6 7 8];
norm(v)

2: Using Mathematical Algorithm

In this method, we can find the magnitude of a vector by following the given steps:

  • Calculate the vector’s product with itself by multiplying the arrays (.*). As a result, a vector sv is created, and its elements are squares of elements that are contained in vector v.
  • Calculate the addition of the squares of the elements of vector v utilizing the sum() function.
  • Get the square root of the sum using the sqrt() function, which is the magnitude of the vector v.

Example

Consider an example that calculates the magnitude of the given vector using the above algorithm.

v = [1 2 3 4 5 6 7 8];
sv = v.*v;
dot_product = sum(sv);
magnitude = sqrt(dot_product)

Conclusion

MATLAB includes two methods for finding the magnitude of a vector that is norm() function and a mathematical algorithm. The norm() is a built-in function in MATLAB that accepts the vector as an input and returns its magnitude. However, the mathematical algorithm uses mathematical steps to find the magnitude of a vector.

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.