Reading — step 1 of 5
Learn
~2 min readVectorization, ODEs, Optimization
Octave's killer use case is numerical methods. Most classical algorithms ship as one-liners.
Root finding
%% fzero — find a root of f(x) = 0
f = @(x) x^2 - 4;
x = fzero(f, 1) %% start near x=1, find x=2
fzero works for any continuous function. Newton-style under the hood.
Optimization
%% fminbnd — minimize over a bounded interval
f = @(x) (x - 3).^2 + 1;
x_min = fminbnd(f, 0, 10) %% finds x = 3
%% fminunc — unconstrained minimization (multidimensional)
f = @(v) v(1)^2 + v(2)^2;
x_min = fminunc(f, [10, 10]) %% finds [0, 0]
%% fminsearch — Nelder-Mead simplex (no derivative needed)
Numerical integration
%% quad — adaptive quadrature
f = @(x) sin(x);
q = quad(f, 0, pi) %% ≈ 2.0
%% quadgk — adaptive Gauss-Kronrod (more accurate)
%% trapz, simpson — fixed-grid integration
Differential equations
%% Solve dy/dt = -y, y(0) = 1
f = @(t, y) -y;
[t, y] = ode45(f, [0, 5], 1);
printf("%.4f\n", y(end)); %% e^-5 ≈ 0.0067
ode45 is the Runge-Kutta 4(5) integrator. Other solvers: ode23, ode23s (stiff), ode15s.
Polynomial fitting
x = 0:10;
y = 2*x + 3 + randn(1, 11); %% noisy linear
p = polyfit(x, y, 1) %% [slope, intercept]
y_fit = polyval(p, x); %% evaluate the polynomial
Interpolation
x = [0, 1, 2, 3, 4];
y = [1, 2, 4, 7, 11];
x_query = 2.5;
y_interp = interp1(x, y, x_query, "spline") %% spline interpolation
Methods: "linear", "spline", "pchip", "nearest".
FFT
fs = 1000; %% sample rate
t = 0:1/fs:1;
signal = sin(2*pi*50*t) + sin(2*pi*120*t);
Y = fft(signal);
P = abs(Y) / length(signal);
f = (0:length(signal)-1) * fs / length(signal);
%% peaks at 50 Hz and 120 Hz
Why Octave for numerical work
- One-liner access to LAPACK, BLAS, FFTW under the hood
- Compatible MATLAB syntax — algorithms in textbooks copy/paste
- Free, scriptable, embeddable
- Statistics, signal, image packages add specialized algorithms
For research-grade numerical code, Octave is competitive with MATLAB and beats most general-purpose languages by far in expressiveness.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…