Vector Math
Beyond basic array arithmetic, MATLAB has several functions built specifically for arrays and vectors, such as averaging a set of sensor readings, finding the magnitude of a force vector, or computing the work done by a force over a displacement.
Functions on a Single Array
The following functions operate on a single array:
| Function | Description | MATLAB Help Center |
|---|---|---|
| flip | Reverse the elements in an array | flip |
| length | Length of largest array dimension | length |
| max | Largest value in array | max |
| mean | Mean or average value in array | mean |
| median | Median value of an array | median |
| min | Minimum value of an array | min |
| norm | Vector norm (magnitude) of array | norm |
| prod | Product of all values in array | prod |
| sort | Sort array in ascending order | sort |
| std | Standard deviation of array | std |
| sum | Total of all values in array | sum |
For a vector, norm computes the magnitude, $\sqrt{x^2 + y^2 + z^2 + …}$, which you need any time you care about the size of a force, velocity, or displacement rather than its individual components.
forces = [30, 45, -10, 22]; % N, readings from a load cell
disp('Mean force (N):')
disp(mean(forces))
resultant = [30, 40, 0]; % N
disp('Magnitude of the resultant force (N):')
disp(norm(resultant))
Mean force (N):
21.7500
Magnitude of the resultant force (N):
50
Functions on Multiple Arrays
These functions operate on two arrays at once:
| Function | Description | MATLAB Help Center |
|---|---|---|
| dot(a,b) | Dot product of two vectors | dot |
| cross(a,b) | Cross product of two vectors | cross |
The dot product of two vectors of the same length returns a single number, and is used throughout engineering to compute things like the work done by a force. The cross product is only defined for vectors of length 3; it returns a new vector perpendicular to both inputs, and is used to compute things like torque.
Example: Torque and Work
Question
A technician tightens a bolt using a wrench, then pushes a cart down a hallway.
For the wrench, the lever arm from the bolt to the technician’s hand is r = [0.15, 0, 0] m, and the applied force is F = [0, 40, 0] N.
Find the resulting torque vector.
For the cart, the applied force is force = [120, -40, 0] N, and the cart moves along displacement = [5, 2, 0] m.
Find the work done on the cart.
Solution
Torque is the cross product of the lever arm and the applied force, while work is the dot product of the force and the displacement.
% Torque from a wrench
r = [0.15, 0, 0]; % m, lever arm from bolt to hand
F = [0, 40, 0]; % N, applied force
torque = cross(r, F);
disp('Torque vector (N*m):')
disp(torque)
% Work done pushing a cart
force = [120, -40, 0]; % N
displacement = [5, 2, 0]; % m
work = dot(force, displacement);
disp('Work done (J):')
disp(work)
Torque vector (N*m):
0 0 6
Work done (J):
520
Reading Questions
- What does
normcompute for a vector, and what physical quantities is that useful for? - What is the difference between what
dotreturns and whatcrossreturns? - If you take the dot product of a force vector and a displacement vector, what physical quantity do you get?
- If you take the cross product of a lever arm vector and a force vector, what physical quantity do you get?
- Does
dotwork on vectors of any length, or only vectors of length 3? What aboutcross?
Practice Problem: Rotor Blade Load Survey
A helicopter’s main rotor blades don’t all carry exactly the same load. Small differences in manufacturing, wear, and adjustment mean each blade’s lift force is a little different, and technicians track that spread as part of routine maintenance.
In this practice problem, you’ll write a MATLAB script that summarizes a set of blade load readings, then uses the dot product to find the power delivered by the rotor during a climb.
Your Task
Write a script named rotor_loads.m that starts from the given blade load readings:
blade_loads = [4820, 5100, 4950, 5030]; % lbf, one reading per blade
Compute:
mean_load- the mean ofblade_loadsload_spread- the difference between the highest and lowest blade loadsload_std- the standard deviation ofblade_loads
Next, given the helicopter’s climb velocity and the rotor’s thrust vector:
v = [0, 0, 8]; % m/s, climb velocity
F = [150, -80, 45000]; % N, rotor thrust vector
climb_power- the power, in watts, delivered by the thrust vector along the climb direction (the dot product ofFandv)
Your variable names for the four answers above must match exactly (mean_load, load_spread, load_std, climb_power) so that the checker below can find them.
Checking Your Work
Download check_rotor_loads.m and save it in the same folder as your rotor_loads.m script.
Make sure that folder is your Current Folder in MATLAB, then run:
>> check_rotor_loads
The checker runs your script and reports whether each of the four values is correct. This is practice, not a graded assignment. If something doesn’t pass, use the feedback to find and fix the issue, then run the checker again.