Training Data Visualization MATLAB — Numerical Computing & Visualization
5 / 10

MATLAB — Numerical Computing & Visualization

24 min Data Visualization

MATLAB — Numerical Computing & Visualization

What Is MATLAB?

MATLAB (MATrix LABoratory) is a proprietary numerical computing environment developed by MathWorks. It is built around matrix and array operations, making it a natural fit for linear algebra, signal processing, control systems, and data visualization.

Key Variables & Concepts
  • Scalar — a single number, e.g. x = 5;
  • Vector — a 1-D array, e.g. v = [1 2 3 4 5]; (row) or v = [1;2;3]; (column)
  • Matrix — a 2-D array, e.g. A = [1 2; 3 4]; creates $A = \begin{bmatrix}1&2\\3&4\end{bmatrix}$
  • Workspace — the session's variable memory (view with whos)
  • Semicolon (;) — suppresses console output
Essential Operations
OperationSyntaxMath
Matrix multiplyC = A * B$C = AB$
Element-wise multiplyC = A .* B$c_{ij} = a_{ij}b_{ij}$
TransposeA'$A^T$
Inverseinv(A)$A^{-1}$
Determinantdet(A)$|A|$
Eigenvalueseig(A)$\lambda$ such that $Av = \lambda v$
Solve $Ax=b$x = A \ b$x = A^{-1}b$
Plotting Commands
  • plot(x, y) — 2-D line plot. x: independent variable vector, y: dependent variable vector.
  • bar(x, y) — bar chart.
  • histogram(data, nbins) — histogram. data: vector of values; nbins: number of bins.
  • scatter(x, y) — scatter plot.
  • surf(X, Y, Z) — 3-D surface. X, Y: mesh grid; Z: height values.
  • contour(X, Y, Z) — contour (level curves).
  • xlabel('text'), ylabel('text'), title('text') — axis labels and title.
  • legend('series1','series2') — add a legend.
The Colon Operator

a:step:b creates a vector from $a$ to $b$ with increment step.
Example: t = 0:0.01:2*pi; creates 629 evenly spaced points from $0$ to $2\pi$.

linspace(a, b, n) creates exactly $n$ points: $a, a+\Delta, \ldots, b$ where $\Delta = \frac{b-a}{n-1}$.

Example 1 — Plot a Sine Wave

Plot $y = \sin(x)$ from $0$ to $2\pi$.

x = linspace(0, 2*pi, 200);

y = sin(x);

plot(x, y, 'b-', 'LineWidth', 2);

xlabel('x'); ylabel('sin(x)'); title('Sine Wave');

Variables: x — 200-point vector from $0$ to $2\pi$; y — $\sin$ evaluated at each point; 'b-' — blue solid line.

Example 2 — Solve a Linear System

Solve $2x + 3y = 8$, $x - y = 1$ in MATLAB.

A = [2 3; 1 -1]; b = [8; 1];

x = A \ b;

Result: x = [2.2; 1.2], meaning $x = 2.2,\; y = 1.2$.

Example 3 — 3-D Surface

Plot $z = \sin(\sqrt{x^2+y^2})$ over $[-5,5]\times[-5,5]$.

[X,Y] = meshgrid(-5:0.2:5, -5:0.2:5);

Z = sin(sqrt(X.^2 + Y.^2));

surf(X, Y, Z); colormap jet; colorbar;

Variables: X, Y — 2-D grids from meshgrid; Z — height at each $(x,y)$; .^2 — element-wise square.

Practice Problems

1. Create a row vector of odd numbers from 1 to 19.
2. What does the dot (.) in A .* B mean?
3. Plot $y = e^{-x}\cos(2\pi x)$ for $0 \le x \le 4$. Which commands?
4. What does A \ b compute?
5. How do you add a title and axis labels to a plot?
6. What is the difference between * and .*?
7. How many points does linspace(0,1,50) create?
8. Create a $3\times3$ identity matrix.
9. What command creates a 3-D surface plot?
10. How do you suppress output in MATLAB?
11. What does eig(A) return?
12. Name two ways to create a vector from 0 to 10.
Show Answer Key

1. v = 1:2:19;

2. Element-wise operation (operates on corresponding elements)

3. x = linspace(0,4,500); y = exp(-x).*cos(2*pi*x); plot(x,y);

4. Solves the system $Ax = b$ (left division, equivalent to $A^{-1}b$)

5. title('text'); xlabel('text'); ylabel('text');

6. * is matrix multiplication; .* is element-wise multiplication

7. 50

8. I = eye(3);

9. surf(X,Y,Z) or mesh(X,Y,Z)

10. End the statement with a semicolon (;)

11. Eigenvalues (and eigenvectors if called as [V,D]=eig(A))

12. v = 0:10; or v = linspace(0,10,11);