Created by Dr. V. R. Ratnaparkhe
Signals & SystemsVirtual Laboratory
Experiment 01 Β· Signals & Systems Lab

MATLAB IDE & Simulink Fundamentals

Explore the MATLAB desktop, run commands in a live console, and build a first Simulink model.

MATLAB consoleSimulink builderWorkspaceCO 2

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, zeros and ones, 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

ItemReal labIn this virtual lab
SoftwareMATLAB R2018b or later with SimulinkBuilt-in mini-MATLAB console (a subset of commands)
ModellingSimulink Library BrowserInteractive block builder with a live Scope
HardwarePC, 4 GB RAM or moreAny modern browser

1. The MATLAB desktop (IDE)

Click any panel in the diagram below to see what it does.

HOME PLOTS APPS EDITOR β”‚ β–Ά Run New Script Open Simulink Current FolderπŸ“ lab πŸ“„ exp1.m πŸ“„ signals.m πŸ”· model1.slx Editor β€” exp1.m1 t = 0:0.01:1;2 x = sin(2*pi*5*t);3 plot(t, x); grid on4 title('5 Hz sine') Command Window>> a = [1 2 3]a = 1 2 3>> | Workspacea [1 2 3] 1Γ—3t 1Γ—101 doublex 1Γ—101 double Command Historyclca = [1 2 3]plot(t,x)
Click a panel…

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.

n = -5:5 % colon: start:step:end (step defaults to 1) t = linspace(0,1,100) % 100 equally spaced points z = zeros(1,10); o = ones(1,10); y = x .* h % element-wise product (x * h is the MATRIX product) p = x .^ 2 % element-wise power
FunctionPurposeExample
plot(x,y)Continuous line plotplot(t,sin(t))
stem(n,x)Discrete-time (lollipop) plotstem(n,x)
subplot(m,n,k)Split a figure into an mΓ—n gridsubplot(2,1,1)
xlabel / ylabel / titleLabel axes and the figuretitle('x[n]')
length, size, sum, maxArray informationlength(n)
sin, cos, exp, absElement-wise mathsexp(-2*t)
clc / clear / whoClear screen / clear variables / list variablesclear 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.

Sine WaveSources Gain KMath Ops ScopeSinks
Signal flow: Source β†’ Math β†’ Sink. The dashes show data moving each solver step.
Opening Simulink: type 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

  1. Open the Simulator tab. Type a = [1 2 3] in the console and press Enter. Watch the Workspace panel.
  2. Make a time vector: t = 0:0.01:1; (the semicolon hides the output).
  3. Make a signal: x = sin(2*pi*3*t); then plot it: plot(t,x).
  4. Try a*a and then a.*a. Write down the error and the result in the Observations tab.
  5. Use stem(0:10, (0:10).^2) and compare it with plot.
  6. Use who, length(t), sum(a), max(x), then clear and clc.

Part B β€” Simulink

  1. In the Simulink builder, click the Source block and choose Sine Wave. Set amplitude 2 and frequency 1 Hz.
  2. Click Gain and set K = 1.5. Turn on the Sum block and add a Step as the second input.
  3. Set the stop time to 5 s and press β–Ά Run. Watch the Scope.
  4. Read the peak value from the Scope readout and check it against theory: peak = KΒ·A + step value.
  5. Press Record in the Observations tab after each run.
Guidelines: save scripts with names that do not clash with built-in functions (do not name a file 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

Try an example:
>>

Workspace

NameValueSizeClass
Figure 1

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


Click a block to edit its parameters (like double-clicking in Simulink).
Scope
Sim time0 s
Maxβ€”
Minβ€”
Expected peakβ€”

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');