Aim
To get familiar with the MATLAB desktop, its basic functions and programming environment, and to learn how to build and simulate models with the Simulink toolbox.
What this experiment shows
MATLAB (MATrix LABoratory) treats every quantity as a matrix, so a whole signal can be created, changed and plotted in a single line. Simulink gives a graphical, block-diagram view of the same ideas: signals flow along lines between blocks, and the solver steps through time.
Learning objectives
- Name and use the main parts of the MATLAB IDE: Command Window, Editor, Workspace, Current Folder, Command History and Figure window.
- Create vectors with the colon operator,
linspace,zerosandones, and tell matrix operators apart from element-wise operators. - Plot continuous-style (
plot) and discrete (stem) signals with labels and titles. - Build a Simulink model from Source β Math β Sink blocks, set block parameters and run a simulation.
Apparatus / environment
| Item | Real lab | In this virtual lab |
|---|---|---|
| Software | MATLAB R2018b or later with Simulink | Built-in mini-MATLAB console (a subset of commands) |
| Modelling | Simulink Library Browser | Interactive block builder with a live Scope |
| Hardware | PC, 4 GB RAM or more | Any modern browser |
1. The MATLAB desktop (IDE)
Click any panel in the diagram below to see what it does.
2. Variables, vectors and operators
Every MATLAB variable is an array. A scalar is 1Γ1 and a row vector is 1ΓN. Signals are stored as vectors of samples.
| Function | Purpose | Example |
|---|---|---|
plot(x,y) | Continuous line plot | plot(t,sin(t)) |
stem(n,x) | Discrete-time (lollipop) plot | stem(n,x) |
subplot(m,n,k) | Split a figure into an mΓn grid | subplot(2,1,1) |
xlabel / ylabel / title | Label axes and the figure | title('x[n]') |
length, size, sum, max | Array information | length(n) |
sin, cos, exp, abs | Element-wise maths | exp(-2*t) |
clc / clear / who | Clear screen / clear variables / list variables | clear all |
3. Simulink
Simulink is a graphical environment for modelling dynamic systems. A model is made of blocks from libraries (Sources, Math Operations, Continuous, Discrete, Sinks) joined by signal lines. A solver (for example ode45 or fixed-step) moves time forward from start time to stop time and works out every block's output at each step.
simulink in the Command Window β Blank Model β open the Library Browser β drag blocks in β draw lines between ports β set Stop time β press Run.Part A β MATLAB console
- Open the Simulator tab. Type
a = [1 2 3]in the console and press Enter. Watch the Workspace panel. - Make a time vector:
t = 0:0.01:1;(the semicolon hides the output). - Make a signal:
x = sin(2*pi*3*t);then plot it:plot(t,x). - Try
a*aand thena.*a. Write down the error and the result in the Observations tab. - Use
stem(0:10, (0:10).^2)and compare it withplot. - Use
who,length(t),sum(a),max(x), thenclearandclc.
Part B β Simulink
- In the Simulink builder, click the Source block and choose Sine Wave. Set amplitude 2 and frequency 1 Hz.
- Click Gain and set K = 1.5. Turn on the Sum block and add a Step as the second input.
- Set the stop time to 5 s and press βΆ Run. Watch the Scope.
- Read the peak value from the Scope readout and check it against theory: peak = KΒ·A + step value.
- Press Record in the Observations tab after each run.
sin.m). End lines with ; for long vectors, and save before running.Pre-lab diagnostic
Check your prerequisite knowledge. Wrong answers give a hint, and you can try again.
A. Mini-MATLAB console
Workspace
| Name | Value | Size | Class |
|---|
Command History
Supported: numbers, [ ], a:s:b, + β * / ^ .* ./ .^, comparisons, indexing x(3), and the functions sin cos tan exp log sqrt abs zeros ones linspace length numel sum max min mean cumsum floor round ceil rand plot stem sound soundsc title xlabel ylabel hold grid disp who whos clc clear.
B. Simulink model builder
Block Parameters
Observation table β Simulink runs
Press Record after each run. Then type the peak value you read off the Scope cursor into "Observed peak"; the % error is worked out for you.
Command log β MATLAB console
Analysis & notes
Post-lab quiz
Self-evaluation
exp1_basics.m
%% Experiment 1: MATLAB basics clc; clear all; close all; a = [1 2 3]; b = [4; 5; 6]; % row and column vectors M = a * b % inner product (1x3 * 3x1) = 32 E = a .* a % element-wise = [1 4 9] t = 0:0.001:1; % time vector x = 2*sin(2*pi*5*t); subplot(2,1,1); plot(t,x); grid on; xlabel('t (s)'); ylabel('x(t)'); title('Continuous sine'); n = 0:20; xn = 2*sin(2*pi*0.05*n); subplot(2,1,2); stem(n,xn,'filled'); grid on; xlabel('n'); title('Discrete sine'); %% Simulink from script % open_system('model1'); sim('model1', 'StopTime', '5');