MATLAB & Octave for Engineers
MATLAB (and the free, largely compatible GNU Octave) is built around matrices and remains the industry standard in control, signal processing and many labs. Learn the syntax for matrices, element-wise operators, the backslash solver, scripts and functions, plotting, polynomial fitting, numerical integration, ODE solvers and how MATLAB idioms map to Python.
MATLAB stands for MATrix LABoratory: every variable is a matrix, and operations are written almost like textbook linear algebra. A\b solves a linear system; eig(A) gives eigenvalues; ode45 integrates differential equations; toolboxes add control design, signal processing and optimisation.
Octave runs most MATLAB scripts unchanged and is free — ideal for students. The concepts transfer directly to NumPy.
An analogy
MATLAB is a well-equipped workshop where every tool is labelled and within reach; Python is a larger hardware store where you pick the tools (libraries) you need.
Everything is a matrix
A scalar is 1×1, a row vector 1×n, a column vector n×1. Spaces or commas separate columns, semicolons separate rows. Indexing starts at 1 (not 0 as in Python): A(2,3) is row 2, column 3; A(:,1) is the first column; end refers to the last index.
Element-wise versus matrix operations
The most common MATLAB bug: x^2 tries a matrix power, x.^2 squares each element. Use .*, ./, .^ whenever you mean 'per element' — e.g. computing stress σ = F./A for arrays of forces and areas.
- A*B: matrix product (inner dimensions must match)
- A.*B: same-size element products
- A': transpose (complex-conjugate); A.' plain transpose
Scripts, functions and handles
Scripts run top to bottom in the workspace; functions (function y = f(x) … end) have their own variables and can live at the end of a script file. Anonymous handles like g = @(x) x.^2 - 2 pass functions into solvers: fzero(g, 1), integral(g, 0, 1), fminsearch, ode45.
Toolboxes and the Python mapping
Control System Toolbox (tf, step, bode, rlocus), Signal Processing (filter, fft, designfilt), Optimization (fmincon, linprog) and Simulink (block-diagram simulation) are MATLAB's strengths. Python equivalents: python-control, scipy.signal, scipy.optimize. Stiff ODEs need ode15s (MATLAB) or solve_ivp(method='BDF') (Python).
Formulas
- Matrix entry:
A = [4 -1 0; -1 4 -1; 0 -1 4] - Ranges:
t = 0:0.1:1; x = linspace(0, pi, 101) - Solve / eigen:
x = A\b; [V, D] = eig(A) - Element-wise vs matrix:
A*B (matrix product) vs A.*B (entry by entry) - ODE solver:
[t, y] = ode45(@(t,y) [y(2); -4*y(1)], [0 5], [1; 0]) - Polynomials:
p = polyfit(x, y, 1); roots([1 -6 11 -6])
Worked example: Solve and verify
Given: A and b as in the code
Find: x and residual.
- x = A\b
- norm(A*x − b) ≈ 1e−15
Answer: x = [1; 2; 3]
Worked example: Element-wise stress
Given: F = [10 20 30] kN, A = [100 200 150] mm²
Find: σ.
- sigma = F*1e3 ./ (A*1e-6) / 1e6
Answer: σ = [100 100 200] MPa
Worked example: Root finding with a handle
Given: g = @(x) x.^3 - 2*x - 5
Find: Root near 2.
- fzero(g, 2)
Answer: x ≈ 2.0946
Core MATLAB/Octave operations
% Linear algebra
A = [4 -1 0; -1 4 -1; 0 -1 4];
b = [2; 4; 10];
x = A \ b % x = [1; 2; 3]
lambda = eig(A) % [2.5858; 4.0000; 5.4142]
% Numerical integration and polynomials
xs = linspace(0, pi, 101);
I = trapz(xs, sin(xs)) % I = 1.9998 (exact 2)
r = roots([1 -6 11 -6]) % r = [3; 2; 1]
% Least-squares straight line through test data
t = 0:4;
y = [1.1 2.9 5.2 7.1 8.8];
p = polyfit(t, y, 1) % p = [1.9600 1.1000] -> y = 1.96 t + 1.10
% ODE: y'' + 4y = 0, y(0) = 1, y'(0) = 0 (exact y = cos 2t)
f = @(t, Y) [Y(2); -4*Y(1)];
[tt, Y] = ode45(f, [0 5], [1; 0]);
plot(tt, Y(:,1), tt, cos(2*tt), '--'); legend('ode45', 'exact'); grid onOutputs shown as comments were cross-checked with the equivalent NumPy calls (np.linalg.solve, eigvalsh, trapezoid, roots, polyfit). Note the column vector b uses semicolons, and trapz with 101 points has error ≈ 1.6×10⁻⁴.
Common mistakes
- Wrong: x^2 on a vector. — Right: x.^2 for element-wise.
- Wrong: Indexing from 0. — Right: MATLAB indices start at 1.
- Wrong: inv(A)*b. — Right: A\b.
Applications
- Control engineering: Controller design, Bode/root-locus with Control Toolbox.
- Signal processing: Filter design and spectral analysis.
- Lab automation: Instrument data acquisition and analysis.
- Simulink: Block-diagram system simulation and code generation.
Key takeaways
- Everything is a matrix; indices start at 1.
- Use dot operators for element-wise maths.
- A\b, eig, ode45, polyfit are the workhorses.
- Concepts transfer directly to NumPy/SciPy.
Going further
- Vectorised code: Avoid loops; preallocate arrays when loops are necessary.
- Live Scripts: Notebook-style documents mixing code and output.
- Code generation: MATLAB Coder / Simulink produce C for embedded targets.