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:
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.
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.