syms x y z
r = [x y z]
rmag???
rmag should equal (x^2 + y^2 + z^2)^0.5

4 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 5 Sep 2013
What is your question?
Philosophaie
Philosophaie le 5 Sep 2013
I am looking for the magnitude of the vector r. It should be equal to r = (x^2 + y^2 + z^2)^0.5.
Prashant C
Prashant C le 3 Juin 2015
use the function norm(r) or mag=sqrt(sum(r.*r))
Abdullraheem Diab
Abdullraheem Diab le 30 Juin 2019
Sqrt(sum(r.^2))

Connectez-vous pour commenter.

 Réponse acceptée

Shashank Prasanna
Shashank Prasanna le 5 Sep 2013
Modifié(e) : MathWorks Support Team le 22 Mai 2019

11 votes

This works perfectly fine on MATLAB R2013a:
>> syms x y z
r = [x y z];
norm(r)

2 commentaires

Shashank Prasanna
Shashank Prasanna le 5 Sep 2013
Modifié(e) : MathWorks Support Team le 22 Mai 2019
What version of MATLAB are you using? Can you confirm that you see the file when you run this:
>> which sym/norm
Bhuvana Krishnaraj
Bhuvana Krishnaraj le 3 Juin 2019
2015.a version >>which sym/nom C:\matlab\toolbox\symbolic\@!sym\norm.m

Connectez-vous pour commenter.

Plus de réponses (3)

Tariq Shajahan
Tariq Shajahan le 11 Mai 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 commentaires

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)
A = 5×5
17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
norm(A)
ans = 65
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))
ans = 
65
And norm is able to do the same thing for a symbolic array. So there should be no surprise here.
To compute the norm of each row or column of a numeric matrix use vecnorm instead of norm.
A = magic(5);
vecnorm(A, 2, 1) % default 2-norm in dimension 1
ans = 1×5
32.4808 33.2415 34.7131 33.2415 32.4808
vecnorm(A, 1, 2) % 1-norm in dimension 2
ans = 5×1
65 65 65 65 65

Connectez-vous pour commenter.

namal
namal le 23 Août 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);

Catégories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by