MATLAB - grandezza di un vettore

La grandezza di un vettore v con elementi v1, v2, v3,…, vn, è data dall'equazione -

| v | = √ (v1 2 + v2 2 + v3 2 +… + vn 2 )

È necessario eseguire i seguenti passaggi per calcolare la grandezza di un vettore:

  • Prendi il prodotto del vettore con se stesso, usando array multiplication(. *). Questo produce un vettore sv, i cui elementi sono quadrati degli elementi del vettore v.

    sv = v. * v;

  • Usa la funzione sum per ottenere il file sum of squares of elements of vector v. This is also called the dot product of vector v.

    dp= sum(sv);

  • Use the sqrt function to get the square root of the sum which is also the magnitude of the vector v.

    mag = sqrt(s);

Example

Create a script file with the following code −

v = [1: 2: 20];
sv = v.* v;       %the vector with elements 
                  % as square of v's elements
dp = sum(sv);     % sum of squares -- the dot product
mag = sqrt(dp);   % magnitude
disp('Magnitude:'); 
disp(mag);

When you run the file, it displays the following result −

Magnitude:
36.469