MATLAB — Numerical Computing & Visualization
MATLAB — Numerical Computing & Visualization
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.
- Scalar — a single number, e.g.
x = 5; - Vector — a 1-D array, e.g.
v = [1 2 3 4 5];(row) orv = [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
| Operation | Syntax | Math |
|---|---|---|
| Matrix multiply | C = A * B | $C = AB$ |
| Element-wise multiply | C = A .* B | $c_{ij} = a_{ij}b_{ij}$ |
| Transpose | A' | $A^T$ |
| Inverse | inv(A) | $A^{-1}$ |
| Determinant | det(A) | $|A|$ |
| Eigenvalues | eig(A) | $\lambda$ such that $Av = \lambda v$ |
| Solve $Ax=b$ | x = A \ b | $x = A^{-1}b$ |
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.
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}$.
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.
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$.
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
A .* B mean?A \ b compute?* and .*?linspace(0,1,50) create?eig(A) return?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);