Lumenary CS · Programming languages
MATLAB and Octave
The matrix language of science and engineering, taught through free GNU Octave, from a first script to linear algebra, ODEs, data analysis and a complete project.
MATLAB ("matrix laboratory") is a language and environment for numerical computing in which the basic value is a matrix. Engineers, scientists and students use it to solve linear systems, simulate differential equations, process signals and images, design control systems and analyse data, usually with a few lines of vectorised code where other languages would need loops. MATLAB is proprietary software from MathWorks, sold with many add-on toolboxes and the Simulink modelling environment. GNU Octave is a free, open-source program that runs most MATLAB code, and this course teaches the language through Octave, saying clearly wherever the two differ.
This course is for anyone who already knows what a variable, a loop and a function are: the Start level of Lumenary CS or equivalent experience in any language. A little algebra helps; the linear algebra and calculus that appear are explained as they come. It covers matrices and indexing, vectorised arithmetic and control flow, scripts and functions, text, cell arrays and structs, solving linear systems, numerical methods (roots, integration and ODEs with ode45), data analysis and plotting, the toolbox and Simulink ecosystem, alternatives such as Python with NumPy and Julia, and a complete small project.
To run the code, install GNU Octave from octave.org (Windows, macOS and Linux; it is free), or use MATLAB if you have a licence; MathWorks also offers MATLAB in the browser, MATLAB Online, with a MathWorks account. Every complete program in these lessons was run with GNU Octave 11 by Lumenary's test suite, and the output shown is what Octave printed; MATLAB prints the same values but lays out automatic displays slightly differently. The lessons were drafted with AI assistance and are awaiting review by a human computer scientist.
- Chloe · The Designer
“Why is the language built this way?” - Margaret · The Profiler
“What does it print, and what does it cost?” - Gideon · The Language Lawyer
“What exactly does the language guarantee?”
Foundations · 3 lessons
Foundations
Install Octave, run scripts, and learn the core of the language: matrices as the basic type, creating and indexing arrays, vectorised arithmetic and control flow.
- 1Getting started with MATLAB and OctaveWhere the matrix laboratory came from, how MATLAB and GNU Octave relate, and your first scripts. 45 min
- 2Matrices, arrays and indexingBuilding arrays with brackets, ranges and constructor functions, and reading and changing them with subscripts, linear indices and logical masks. 50 min
- 3Vectorised arithmetic and control flowElement-wise and matrix operators, implicit expansion, reductions along dimensions, floating point and integer types, and if, for, while and switch. 55 min
Practice · 3 lessons
Practice
Organise code into scripts and functions with handles and error handling, work with text, cell arrays and structs, and solve linear systems the way numerical analysts do.
- 4Scripts and functionsFunction files and local functions, multiple outputs and optional inputs, anonymous functions and handles, errors, and testing. 60 min
- 5Text, cell arrays and structsCharacter arrays and the functions that handle text, cell arrays for mixed data, structs and struct arrays for records, maps, and JSON. 55 min
- 6Linear algebra and linear systemsSolving systems with backslash and what it does underneath, conditioning, least squares, eigenvalues and sparse matrices. 60 min
Professional · 2 lessons
Professional
Find roots, integrate and solve differential equations with the built-in solvers, analyse and plot data, know where toolboxes, Simulink, NumPy and Julia fit, and build a complete project.
- 7Numerical methods: roots, integrals and ODEsFinding roots and minima, integrating functions and data, and solving differential equations with ode45 and the stiff solvers, with tolerances you understand. 65 min
- 8Data analysis, plotting and a complete projectReading, cleaning, summarising and modelling data, plotting it, where toolboxes, Simulink, Python and Julia fit, and a complete analysis from raw file to tested report. 70 min
Further reading and resources
- GNU OctaveThe official home of GNU Octave: downloads, news and community.
- GNU Octave documentationThe complete Octave manual, including every built-in function.
- MathWorks documentationThe official MATLAB, toolbox and Simulink documentation.
- Differences between Octave and MATLAB (Octave wiki)The Octave community's own list of where the two languages differ.
- MATLAB OnlineMATLAB in the browser, from MathWorks; requires a MathWorks account.
- Octave packagesThe index of community packages that extend Octave (signal, control, statistics and more).