- Matlab教程
- MATLAB - 主页
- MATLAB - 概述
- MATLAB - 环境设置
- MATLAB - 语法
- MATLAB - 变量
- MATLAB - 命令
- MATLAB - M 文件
- MATLAB - 数据类型
- MATLAB - 运算符
- MATLAB - 决策
- MATLAB - 循环
- MATLAB - 向量
- MATLAB - 矩阵
- MATLAB - 数组
- MATLAB - 冒号表示法
- MATLAB - 数字
- MATLAB - 字符串
- MATLAB - 函数
- MATLAB - 数据导入
- MATLAB - 数据输出
- MATLAB 高级版
- MATLAB - 绘图
- MATLAB - 图形
- MATLAB - 代数
- MATLAB - 微积分
- MATLAB - 微分
- MATLAB - 集成
- MATLAB - 多项式
- MATLAB - 变换
- MATLAB - GNU Octave
- MATLAB - Simulink
- MATLAB 有用资源
- MATLAB - 快速指南
- MATLAB - 有用的资源
- MATLAB - 讨论
MATLAB - Magnitude of a Vector
Magnitude of a vector v with elements v1, v2, v3, …, vn, is given by the equation −
|v| = √(v12 + v22 + v32 + … + vn2)
You need to take the following steps to calculate the magnitude of a vector −
Take the product of the vector with itself, using array multiplication (.*). This produces a vector sv, whose elements are squares of the elements of vector v.
sv = v.*v;
Use the sum function to get the 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
matlab_vectors.htm