Magnitude of a vector
Show older comments
syms x y z
r = [x y z]
rmag???
rmag should equal (x^2 + y^2 + z^2)^0.5
4 Comments
Azzi Abdelmalek
on 5 Sep 2013
What is your question?
Philosophaie
on 5 Sep 2013
Prashant C
on 3 Jun 2015
use the function norm(r) or mag=sqrt(sum(r.*r))
Abdullraheem Diab
on 30 Jun 2019
Sqrt(sum(r.^2))
Accepted Answer
More Answers (3)
Azzi Abdelmalek
on 5 Sep 2013
m=sqrt(x^2+y^2+z^2)
Tariq Shajahan
on 11 May 2015
2 votes
if 'r' is a vector. norm(r), gives the magnitude only if the vector has values. If r is an array of vectors, then the norm does not return the magnitude, rather the norm!!
2 Comments
If r is an array of vectors, what would you expect? How does MATLAB know, for example, that you want to compute the norm of each row of an array, as opposed to a matrix norm? In fact, when MATLAB is given a double precision array, and you use norm, it computes the MATRIX norm.
A = magic(5)
norm(A)
There is no reason to expect it should instead compute the norm of each row, or each column. That would be wrong.
norm(sym(A))
And norm is able to do the same thing for a symbolic array. So there should be no surprise here.
A = magic(5);
vecnorm(A, 2, 1) % default 2-norm in dimension 1
vecnorm(A, 1, 2) % 1-norm in dimension 2
namal
on 23 Aug 2024
0 votes
% Define the vector N = [-3, 7, -5]; % Calculate the magnitude of the vector magnitude = norm(N); % Calculate the unit vector unit_vector = N / magnitude; % Display the unit vector disp('Unit vector in the direction of N:'); disp(unit_vector);
Categories
Find more on Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!