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.

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

Worked example: Solve and verify

Given: A and b as in the code

Find: x and residual.

Answer: x = [1; 2; 3]

Worked example: Element-wise stress

Given: F = [10 20 30] kN, A = [100 200 150] mm²

Find: σ.

Answer: σ = [100 100 200] MPa

Worked example: Root finding with a handle

Given: g = @(x) x.^3 - 2*x - 5

Find: Root near 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 on

Outputs 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

Applications

Key takeaways

Going further