MATLAB Intro
MATLAB ("MATrix LABoratory") is a programming language and interactive environment built around one idea: the basic unit of data is an array of numbers. Where most languages make you write a loop to add two lists, MATLAB adds them with a single +. That makes it a favourite of engineers, scientists and students for numerical work: signal processing, control systems, image analysis, simulations, statistics and quick data exploration.
temps = [18.5 21.0 23.4 19.8 25.1]; % five daily temperatures
avg = mean(temps);
fprintf('Average temperature: %.2f degrees\n', avg);
fprintf('Warmest day: %.1f degrees\n', max(temps));Average temperature: 21.56 degrees
Warmest day: 25.1 degrees
No loop, no type declarations, no main function: you name some data, call functions on it, and print the result.
MATLAB and GNU Octave
MATLAB is a commercial product from MathWorks. GNU Octave is a free, open-source program that understands almost the same language. Code that sticks to the core language (what this course teaches) runs unchanged in both.
| MATLAB | GNU Octave | |
|---|---|---|
| Licence | Commercial (free licences for many universities) | Free and open source (GPL) |
| Language | The reference implementation | Highly compatible, plus a few extensions |
| Extras | Toolboxes (Simulink, Statistics, Image Processing, …), table, string, datetime |
Packages from Octave Forge; fewer high-level data types |
| Desktop | MATLAB Desktop / MATLAB Online | Octave GUI or command line |
Octave also accepts some syntax MATLAB does not, such as # comments, !=, x += 1 and endif. This course never uses those, so everything you learn here also works in MATLAB.
What you'll see on this site
Note: No MATLAB or Octave engine can run inside a web browser, so the playgrounds on this site are simulated: when you press Run you see the saved output. Every example was really executed with GNU Octave 11 when the course was built, so the output is exact, but it is Octave's output.
MATLAB prints values with a slightly different layout. When you leave off the semicolon, Octave shows a scalar on one line, while MATLAB adds blank lines and extra indentation:
x = 42
y = [1 2 3]x = 42
y =
1 2 3
In MATLAB the same two lines display as:
x =
42
y =
1 2 3
The values are identical; only spacing differs. Examples that print with disp and fprintf produce the same text in both programs, which is why most examples in this course use them.
Why learn MATLAB?
- Arrays are first-class. Vectors and matrices are built in, along with fast linear algebra.
- Huge function library. Statistics, polynomials, interpolation, integration, ODE solvers and plotting come out of the box.
- Interactive. Type a line, see the result; perfect for exploring data.
- Everywhere in engineering. Many university courses, labs and industries (automotive, aerospace, signal processing) use it daily.
Here is a taste of matrix work: solving a system of two linear equations, 2x + y = 5 and x - y = 1:
A = [2 1; 1 -1];
b = [5; 1];
solution = A \ b;
fprintf('x = %g, y = %g\n', solution(1), solution(2));x = 2, y = 1
How this course is organised
- Getting started: the command window, variables, output, numbers and types.
- Vectors and matrices: creating, indexing and combining arrays, and element-wise operations.
- Plotting: line plots, formatting, subplots and common chart types.
- Control flow:
if,switch, loops, and replacing loops with vectorised code. - Text and containers: char arrays, string functions, cell arrays, structures and maps.
- Functions: writing your own functions, handles, scope and error handling.
- Numerical computing: linear algebra, statistics, polynomials, interpolation, integration, ODEs and sets.
- Practice: exercises and a mini project.
Every lesson ends with a short quiz, and all quiz questions are collected on the course quiz page.
Test yourself
What does the name MATLAB stand for?
- Mathematical Laboratory
- MATrix LABoratory
- Machine Translation Lab
- Math Library
Which statement about GNU Octave is true?
- It cannot run MATLAB code at all
- It is a free program that runs most core MATLAB code unchanged
- It is a MATLAB toolbox
- It only runs in a web browser
Which of these is Octave-only syntax that this course avoids?
~=% commentx += 1end