Course materials · Robot Basic Lab

MATLAB Array Operations, Functions, and Plots

English translation

Calculate distance and velocity from coordinate changes, and check the results using functions and plots.

Before practice

01 Do dx^2 and dx.^2 perform the same calculation?

Description

^ is matrix power; .^ is elementwise power.

02 Is calculating height change alone enough to obtain velocity?

Description

Divide the height change by the time interval to obtain vertical velocity.

Execution conditions

Use MATLAB or MATLAB Online. The basic activities run in MATLAB alone and do not require a drone connection.

Practice

Preview practice mlx scriptDownload practice mlx script

1. Distinguish elementwise calculations from matrix multiplication

Starting conditions

Create dx=[0.30.40]\mathrm{dx}=\begin{bmatrix}0.3&0.4&0\end{bmatrix} and dy=[0.400.2]\mathrm{dy}=\begin{bmatrix}0.4&0&0.2\end{bmatrix} as column vectors.

Procedure

  1. Calculate segment lengths using sqrt and .^, and compare with hypot.
  2. Compare the sizes and meanings of dx.*dy and dx*dy'.

Results to retain

Items to record and points to check
Operations
Result size
Units/meaning

Points to check

  • Did you keep power and matrix multiplication operators distinct?
  • Did you distinguish units of length from units of squared length?

2. Calculate vertical velocity using timestamp intervals

Starting conditions

t=[0124]t=\begin{bmatrix}0&1&2&4\end{bmatrix} s, height=[0,.4,.9,.7] m.

Procedure

  1. Compute diff(height) and diff(t), then divide elementwise to calculate average vertical velocity for the three intervals.
  2. Plot height at measurement times and vertical velocity at the midpoint of each interval. Label axes and units on each plot.
  3. For the final interval 22–44 s, calculate values using the actual time difference and assuming 1 s. In two rows, record the height change, time difference used, and calculated vertical velocity, and explain the difference.

Results to retain

Items to record and points to check
Interval [s]
Height change [m]
Velocity [m/s]

Points to check

  • Did you account for diff returning one fewer element than the original array?
  • Did you use the irregular measurement intervals as given?

3. Reuse a calculation rule as a function

Starting conditions

Apply segmentLength=@(dx,dy) hypot(dx,dy) to new coordinate changes as well.

Procedure

  1. Pass two vectors of equal length to the function.
  2. Check the resulting size when one input is a row vector and the other is a column vector.
  3. Check that inputs have the same shape, or standardize their shapes.

Results to retain

Items to record and points to check
Input size
Output size
Processing rule

Points to check

  • Did implicit expansion produce a larger matrix than expected?
  • Did fixing the shape preserve element correspondence?

Official examples and data