Help with Matlab and Simulink
Help with Matlab
Example of a Matlab macro to plot a figure over a range of indices or time: plot_range.m
Example of a Matlab macro to plot a figure with adjustable font sizes and lines widths: plot_sine.m
Properties of an existing figure can also be adjusted by clicking on File/Preferences/Figure Copy Template and Apply to Figure.
Vectors and matrices:
x=[0;0] is 2x1 (column)
x=[0 0] is 1x2 (row)
x' is the transpose (row <-> column)
x=ones(n,m) is an nxm matrix of ones
x=zeros(n,m) is an nxm matrix of zeros
x=eye(n) is an nxn identity matrix
size(x) gives the dimensions of x
who gives the variables in Matlab's workspace
x(end) gives the last value of the vector x
Save time by using vector operations:
x.*y computes the product of two vectors element-by-element
x./y divides two vectors element-by-element
x'*y computes the scalar product of two vectors
x.^2 squares all the elements of x
Tricky complex vector operations:
x' returns the complex conjugate transpose of x
x.' or transpose(x) returns the transpose
conj(x) returns the complex conjugate of x
Common mistakes:
- a/b*c computes (a/b)*c, not a/(b*c)
- use deg in Matlab cos/sin functions instead of rad
- redefining standard constants (i, j, pi). Write 1i in code to reduce the risk
Help with Simulink - Configuration parameters
Find the configuration parameters (settings of the simulation code):
Older version: Simulation/Model configuration/Commonly used parameters
Later version: Modeling/Model settings/Model settings
When time samples are too distant:
Configuration parameters => Solver/Solver details (or Additional Options in the old version)
Set Max step size to 1e-3, i.e, a 1 ms time interval (good to plot a 60 Hz signal)
To adjust the simulation precision:
Configuration parameters => Solver, Relative Tolerance to 1e-6
When the simulation stalls:
Simulate for a time shorter than the stall time, then look at the responses for an explanation
In Configuration parameters/Solver, change from Variable-Step to Fixed-Step and use ode1
In Solver details, set Fixed-step size to 1e-4 or less
My preference to name output vectors:
Configuration parameters > Data Import/Export, Save to Workspace, change Dataset to Array
Uncheck yout, logsout, dsmout (use a block "To Workspace" to store variables in the workspace)
Change the name of the time variable from tout to t
Help with Simulink - Other tips
Dimension of vectors:
It can help to initialize variables with their size (eg x=[0;0]),
especially to avoid an error message saying that variables are not
defined in all execution paths (usually in if/then statements)
In some blocks, the dimension of vectors can be set through the properties.
A dimension of -1 (or inherited) means that Simulink will infer the dimensions from other variables
Beware that copy/paste of ports also copies their dimensions.
Algebraic loop message:
occurs when y is a function of x and x is a function of y
it's ok if there is a dynamic system in the loop, but not if the relationship is instantaneous (algebraic)
look for the source of the issue, as it may reveal a problem in the simulation
if the problem remains, insert a delay block (z^-1) with a small Ts (1e-4 or less) in the loop
Common mistakes:
- divide by a variable that may be zero (e.g., initially)
- ignore warnings in compilation, as they often reveal a problem
Debugging:
- code the algorithm step-by-step and test at every stage
for example, first test a control algorithm without limits, then test with them
first test the inner loop, then the outer loop
- store intermediate variables of the code and check that they correspond to expectations
This web page is maintained by Professor
Marc Bodson,
of the
Department of Electrical and Computer
Engineering
at the University of Utah.
Please see the University of
Utah Web disclaimer.
For questions or comments, please send an e-mail to marc.bodson@utah.edu.
Last updated: May 29, 2024.