Session 1. Getting started with MATLAB/ Simulink

Lecture notes for Advanced modeling and Control

Author
Published

July 8, 2023

Modified

July 21, 2025

Table 1: Useful functions
General functions:
help/ helpwin Help for functions in Command Window. Use helpwin for MATLAB file help displayed in a window
cd Change current working directory.
demo Launch the demo (introduction).
dir/ what List folder. Use what to list MATLAB-specific files in directory.
load Load data from MAT-file into workspace.
lookfor Search for keyword in reference page text.
print Print or save a figure or model.
quit Quit MATLAB session.
save Save workspace variables to file.
who/ whos List current variables. Use whos for long form version of the list
Calculation functions:
conv Convolution and polynomial multiplication.
size, length Size of an array, length of a vector
Plotting functions:
axis Control axis scaling and appearance.
grid Add grid to plot
hold Hold a figure to add more plots (curves)
legend Add legend to plot
plot Linear plot
text (gtext) Add text (graphical control) to plot
title Add title to plot
xlabel, ylabel Add axis labels to plot
Partial fraction and transfer functions:
poly Construct a polynomial from its roots
residue Partial-fraction expansion
roots Find the roots to a polynomial
tf2zp Transfer function to zero-pole form conversion
zp2tf Zero-pole form to transfer function conversion
tf Create a transfer function object
get Listthe objectproperties
pole Find the poles of a transfer function
zpk Create a transfer function in pole-zero-gain form

The code demonstrating plotting functions is available in Matlab file/ mlx file.

The code demonstrating partial fractions and transfer functions is available in Matlab file/ mlx file.

Activities

  1. Explore MATLAB user interface

  2. Define a vector x = [1 2 3 4 5 6 7 8 9 10].

    What are different ways you can define x? What happens when you put ; at the end?

  3. Convert vector x into a column vector.

  4. Create vector y = [0, 0.1, 0.2, ...., 2.0]

  5. Create a 3 x 3 matrix.

  6. Print the size of the matrix and lengths of vectors defined so far.

  7. Define 3 polynomials

    (1)p1(s)=s25s+4

    (2)p2(s)=s2+4

    (3)p3(s)=s25s

  8. Calculate p1(s)p2(s)

  9. Perform some mathematical computations on the vectors, matrices, and polynomials defined so far.


%% 2. Define a vector x in different ways
x1 = [1 2 3 4 5 6 7 8 9 10];      % explicit entry
x2 = 1:10;                        % using colon operator
x3 = linspace(1, 10, 10);         % using linspace
x = x1;                           % we'll use x1 as the main vector

% Semicolon at end suppresses output to console
% Without ;, MATLAB prints the result
x2                                % prints output
x3;                               % suppresses output

%% 3. Convert vector x into a column vector
x_col = x(:);

% Convert column vector to a row vector
% Transpose operator `'`
x_row = x_col';

% `reshape` function
x_row = reshape(x_col, 1, []);
 
%% 4. Create vector y = [0, 0.1, ..., 2.0]
y = 0:0.1:2.0;

%% 5. Create a 3 x 3 matrix
A = [1 2 3; 4 5 6; 7 8 9];

%% 6. Print size of matrix and lengths of vectors
disp('Size of matrix A:')
disp(size(A))

disp('Length of vector x:')
disp(length(x))

disp('Length of vector y:')
disp(length(y))

%% 7. Define 3 polynomials
% p1(s) = s^2 - 5s + 4
% p2(s) = s^2 + 0s + 4
% p3(s) = s^2 - 5s

p1 = [1 -5 4];
p2 = [1 0 4];
p3 = [1 -5 0];

%% 8. Calculate p1(s) * p2(s)
p12 = conv(p1, p2);

disp('Product polynomial p1 * p2:')
disp(p12)

%% 9. Perform some computations
sum_xy = x + y(1:length(x));           % add two vectors (element-wise)
scaled_A = 2 * A;                      % scale matrix A
polyval_p3 = polyval(p3, x);           % evaluate p3 at each x

disp('Sum of vectors x and y:')
disp(sum_xy)

disp('Matrix A scaled by 2:')
disp(scaled_A)

disp('p3(x) evaluated at x:')
disp(polyval_p3)
  1. Solve Ax = b

    A = [ 4 -2 -10; 2 10 -12; -4 -6 16];

    b = [-10; 32; -16];

  2. Check the solution

  3. Calculate eigenvalues and eigenvectors.

A = [ 4  -2 -10;
      2  10 -12;
     -4  -6  16];

b = [-10; 32; -16];

x = A \ b;

%% Compute eigenvalues and eigenvectors
[ev, l] = eig(A)

% ev is a matrix whose columns are the corresponding eigenvectors
% l is a diagonal matrix with eigenvalues of A on the diagonal

% verify : A*ev = l*ev

% If any eigenvalue has a positive real part, the system is unstable.
  1. Consider data:

    x = [ 0 1 2 4 6 10];

    y = [ 1 7 23 109 307 1231];

    Fit a third-order polynomial. Plot the results

  2. Explore MATLAB plotting capabilities

  3. Create a MATLAB script, save, and load it to plot data in item 13.

  1. Find roots of polynomial defined by p = [1 5 4]

  2. Search for a function to find roots of a nonlinear equation.

  3. Find polynomial for the roots (-4, -1)

  4. For the following transfer functions find partial fractions.

(4)G(s)=q(s)p(s)=2s2+5s+4

(5)G(s)==2s(s+1)(s+2)(s+3)

(6)G(s)==s3+4s+3s47s3+11s2+7s12


% $$G(s) = \frac{q(s)}{p(s)}=\frac{2}{s^2+5s+4}$${#eq-4}
num = 2;
den = [1 5 4];

[r, p, k] = residue(num, den)

% $$G(s) = =\frac{2}{s (s + 1) (s + 2) (s + 3)}$${#eq-5}
num = 2;

factors = {[1 0], [1 1], [1 2], [1 3]};

den = factors{1};
for k = 2:length(factors)
    den = conv(den, factors{k});
end

[r, p, k] = residue(num, den)



% $$G(s) = =\frac{s^3 + 4s + 3}{s^4 - 7s^3 + 11s^2 + 7s -12}$${#eq-6}

num3 = [1 0 4 3];
den3 = [1 -7 11 7 -12];

[r, p, k] = residue(num, den)
  1. Have fun with zp2tf, tf2zp, and tf commands

  2. Response of first order system: Compute and plot step response of following first order systems

    (7)y(s)=15s+1 (8)y(s)=5e10s2.5s+1


%% First order system
% System 1: y(s) = 1 / (5s + 1)
num1 = 1;
den1 = [5 1];
sys1 = tf(num1, den1);

% System 2: y(s) = (5 * e^{-10s}) / (2.5s + 1)
num2 = 5;
den2 = [2.5 1];
delay = 10;  % Delay in seconds
sys2 = tf(5, [2.5 1], 'InputDelay', 10);

% Plot step responses
figure;
step(sys1, 'b', sys2, 'r--', 0:0.1:100);
legend('sys1', 'sys2');
xlabel('Time (s)');
ylabel('Response');
title('Step Response of First-Order Systems');
grid on;
  1. Response of second order system: Compute and plot step response of following second order system. Show effect of ξ on response.

    (9)Gp(s)=Y(s)U(s)=Kpeθsτ2s2+2ξτs+1

    Kp=1; τ=1; θ=10

  1. Solve differential equations using Simulink
  1. An object falling under gravity

(10)d2ydt2=g

Compare the result with analytical solution y=gt2/2

  1. Systems of ODEs
  1. (11)d2ydt2+2dydt+5y=1

    (12)y˙(0)=y(0)=0

  2. (13)[x˙y˙]=[10]+[2510][xy]

Citation

BibTeX citation:
@online{utikar2023,
  author = {Utikar, Ranjeet},
  title = {Session 1. {Getting} Started with {MATLAB/} {Simulink}},
  date = {2023-07-08},
  url = {https://amc.smilelab.dev/content/notes/01-recap/in-class-activities.html},
  langid = {en}
}
For attribution, please cite this work as:
Utikar, Ranjeet. 2023. “Session 1. Getting Started with MATLAB/ Simulink.” July 8, 2023. https://amc.smilelab.dev/content/notes/01-recap/in-class-activities.html.