Session 1. Getting started with MATLAB/ Simulink
Lecture notes for Advanced modeling and Control
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
Explore MATLAB user interface
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?
Convert vector
x
into a column vector.Create vector
y = [0, 0.1, 0.2, ...., 2.0]
Create a 3 x 3 matrix.
Print the size of the matrix and lengths of vectors defined so far.
Define 3 polynomials
Calculate
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)
Solve Ax = b
A = [ 4 -2 -10; 2 10 -12; -4 -6 16];
b = [-10; 32; -16];
Check the solution
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.
Consider data:
x = [ 0 1 2 4 6 10];
y = [ 1 7 23 109 307 1231];
Fit a third-order polynomial. Plot the results
Explore MATLAB plotting capabilities
Create a MATLAB script, save, and load it to plot data in item 13.
Find roots of polynomial defined by
p = [1 5 4]
Search for a function to find roots of a nonlinear equation.
Find polynomial for the roots (-4, -1)
For the following transfer functions find partial fractions.
% $$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) [
Have fun with
zp2tf
,tf2zp
, andtf
commandsResponse of first order system: Compute and plot step response of following first order systems
%% 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;
Response of second order system: Compute and plot step response of following second order system. Show effect of
on response. ; ;
- Solve differential equations using Simulink
- An object falling under gravity
Compare the result with analytical solution
- Systems of ODEs
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}
}