MATLAB/Simulink

MATLAB/Simulink

How MATLAB's matrix-based language and Simulink's block diagrams are used for numerical computing, data analysis, and modeling and simulating dynamic systems in engineering and science.

What it is

MATLAB (MATrix LABoratory) is a commercial programming and numeric computing environment from MathWorks, built around matrices and arrays. Created by Cleve Moler in the late 1970s, it combines a high-level language, an interactive desktop, plotting tools and toolboxes for signal processing, control, statistics, optimization, machine learning, image processing and more. Simulink is its companion graphical environment: instead of writing equations as code, the engineer builds a model by connecting blocks in a diagram and simulates how the system behaves over time. Both are widely used in engineering, physics, finance, aerospace, automotive and academic teaching.

It answers questions such as: How do I analyze measured data and plot it quickly? How do I solve a system of equations or differential equations numerically? How does a controller respond to a disturbance? How can a design be tested in simulation before building hardware, and then converted to code for an embedded processor? MATLAB handles computation and analysis, and Simulink handles system-level simulation.

Fundamentals

The MATLAB language: matrices first

The basic data type is the matrix (a scalar is a 1 × 1 matrix). Create one with A = [1 2; 3 4], a vector with x = 0:0.1:10 (start:step:end) or linspace(0, 1, 101). Operations are vectorized: y = sin(x) acts on each element, A*B is matrix multiplication and A.*B is element-wise. Linear algebra is built in: A\b solves A x = b, and eig, svd, inv and det are available. Indexing starts at 1 with parentheses, A(2,3), and A(:,1) selects a column. Other data types are structures, cell arrays, tables and categorical arrays, and strings. Control flow uses if, for, while, switch and end; vectorizing instead of looping is usually faster. Functions are saved in .m files or defined as anonymous functions, f = @(x) x.^2 + 1.

Scripts, the desktop and toolboxes

Work is organized into scripts (sequences of commands) and functions (reusable, with their own workspace), plus Live Scripts (.mlx) that combine code, output and formatted text. The desktop shows the Command Window, Workspace (variables), Current Folder and Editor, with a debugger (breakpoints) and the Profiler for finding slow code. Toolboxes add specialized functions, such as Signal Processing, Control System, Statistics and Machine Learning, Optimization, Curve Fitting, Image Processing, Deep Learning and Parallel Computing; they are licensed separately. File input/output uses readtable, readmatrix, load/save (.mat files) and instrument connection through the Data Acquisition and Instrument Control toolboxes. Plotting uses plot, scatter, surf, histogram and subplot or tiledlayout, with labels and export by exportgraphics.

Numerical methods and ODEs

Standard tools include fzero and fsolve for nonlinear equations, fminsearch and fmincon for optimization, polyfit, interp1 and lsqcurvefit for fitting, integral for quadrature and fft for spectra. Ordinary differential equations dx/dt = f(t, x) are solved with ode45 (a Runge–Kutta 4/5 solver for non-stiff problems), and ode15s for stiff ones: [t, x] = ode45(@(t,x) -2*x + sin(t), [0 10], 1). Partial differential equations use the PDE Toolbox, and symbolic math (syms, solve, diff, int) is in the Symbolic Math Toolbox. Accuracy is controlled by tolerances (odeset('RelTol',1e-6)), and results should be checked against analytic solutions and refined time steps.

Simulink: block-diagram simulation

A Simulink model is a diagram of blocks (sources, math operations, integrators, transfer functions, gains, scopes, lookup tables) connected by signal lines. A first-order system dx/dt = −a x + u is built from an integrator with feedback through a gain. Continuous-time models are solved by solvers: variable-step (ode45, ode23t, ode15s for stiff systems) or fixed-step (needed for real-time and code generation). Subsystems keep large models organized; Stateflow adds state machines and flowcharts for logic; Simscape models physical systems (electrical, mechanical, hydraulic, thermal) directly with physical connections rather than signal flow; the Control System Toolbox linearizes models for stability and frequency-response analysis; and Simulink Coder and Embedded Coder generate C/C++ code for microcontrollers, with HDL Coder producing FPGA code. Test and verification tools check models against requirements, and the same model supports model-in-the-loop, software-in-the-loop and hardware-in-the-loop testing (see hardware-in-the-loop testing). Models can also be run from scripts (sim('model')) for parameter sweeps and Monte Carlo analyses.

Alternatives and practicalities

Free alternatives include GNU Octave (largely MATLAB-compatible), Python with NumPy/SciPy, Julia (see Julia) and Scilab with Xcos; MATLAB can also call Python and C/C++ and be called from them. Licensing is paid but common in universities. Benefits are documentation, integrated tooling and strong support in industry; limits are cost, proprietary lock-in of .m/.slx files, memory held in RAM and slower general-purpose programming than in some languages.

Typical applications and limits

Typical uses include controller design and tuning, signal and image processing, system identification, power-electronics and vehicle models, antenna and communication simulations, and teaching numerical methods. Results depend on correct models: a simulation is only as good as its assumptions and parameters, so validate against measurements.

Basic operation

  1. Open the desktop and set a folder. Choose a working directory and create a script or Live Script.
  2. Load or create data. Read files with readtable/readmatrix or generate vectors.
  3. Compute. Apply vectorized operations, toolbox functions or an ODE solver.
  4. Plot results. Draw graphs, label axes with units and export figures.
  5. Build a Simulink model. Place blocks, connect signals, and set parameters in the workspace.
  6. Configure and run the simulation. Choose the solver, step size and stop time, then run and view scopes.
  7. Verify. Compare results with theory or measurements, and refine tolerances.
  8. Collect the output. You obtain figures, data files and simulation results; controller tuning and code generation are covered in the video.

Good practice

  • Vectorize and preallocate. Avoid growing arrays inside loops.
  • Use scripts and functions. Do not rely on typing into the command line.
  • Pick solvers deliberately. Use stiff solvers for stiff problems and tighten tolerances to check convergence.
  • Organize Simulink models. Use subsystems, named signals and units.
  • Validate models. Compare with measurements before trusting predictions.
  • Test before real hardware. Simulated controllers should be tested with safety limits and fault cases before connecting motors, high power or people.

Key terms

  • Matrix / array — MATLAB’s basic data structure.
  • Toolbox — an add-on library of functions for a specific field.
  • Live Script — a notebook combining code, output and text.
  • ode45 — a general-purpose solver for non-stiff ordinary differential equations.
  • Block diagram — a model built from connected function blocks.
  • Solver — the numerical method that advances a simulation through time.
  • Simscape — a Simulink library for physical-network modeling.

Video coming soon

About five minutes on MATLAB/Simulink: how it works in detail, how the data is acquired, and how it is analyzed.

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping