# chapter 6

Use of MATLAB

# Section 9: matlab live scripts for an introductory control course

A number of live scripts are available to help you learn how to use MATLAB to support your understanding of modelling, dynamics and control. A typical live script has the window arranged into two halves as seen below:

The left hand side has the textual explanation and, in the lightly shaded blocks, the MATLAB code.

The right hand side shows the results from using the code.

The document may be split into sections for convenience so you can run a small part at a time.

You can edit the code and re-run sections using the ‘run section’ icon at the top.

The following video also gives some basic insight into how to use livescript files.

The resources/toolbox developed here are now most easily used from within MATLAB by downloading the control 101 toolbox; see section 6.11 for more details.

We list the files below in the order in which students may encounter and need them on a first course in control; later files often assume familiarity with concepts in the earlier ones. The files gives numerous example code snippets students can use to learn from, or indeed modify for their own examples. The files also include some text book like explanations of core engineering principles and assumptions.

For future convenience all files are being edited and moved to a google folder along with other files associated to the toolbox development. Some files require the symbolic toolbox and others the control toolbox.

It is always best to save the original working live script in a backup folder and then perform edits elsewhere on a duplicate file. This way you can always restore the original file if something goes wrong.

TIME DOMAIN METHODS

plotting_with_matlab.mlx Gives a rapid overview of the vector notation used to support line plots and the plot command.

ODEs_with_matlab.mlx Shows how MATLAB can be used to give both analytic and numerical solutions of ODEs. Includes examples of how to plot the solutions.

firstordermodels_in_matlab.mlx Shows how to create models in MATLAB to represent systems.

firstordermodels_responses_analysis_and_feedback.mlx A holistic storyline script which begins from modelling 1st order models in time constant form, identifying parameters from data and then moves on to analysis and design of proportional and PI feedback compensators.

secondordermodels_in_matlab.mlx Shows how to create models in MATLAB to represent systems

secondordermodels_and_under-damping.mlx Illustrates under-damped 2nd order behaviours and the impact of the damping ratio and natural frequency. Uses Laplace based models.

laplace_transforms_with_matlab.mlx Shows how to find the Laplace transform of a time domain signal and the inverse Laplace of a transform. Focuses on analytic solutions. Files below this largely focus on numerical solutions.

partial_fractions_with_matlab.mlx Partial fractions are a core part of inverse Laplace and understanding behaviours. This file shows how MATLAB can support such computations where required.

transferfunctions_and_poles.mlx Shows how to create a transfer function in MATLAB and also to find its poles and zeros which in turn characterise the expected behaviours.

transferfunctions_and_behaviours.mlx Shows how to create a transfer function in MATLAB and also to characterise and plot the expected behaviours.

step_responses_with_matlab.mlx Step responses are a cornerstone of system behaviours and supported by the MATLAB step.m command. This file illustrates several different ways step.m can be used.

closedloop_transferfunctions_with_feedback.mlx Introduces the feedback.m file for computing closed-loop transfer functions.

closed_vs_openloop_overlay.mlx Demonstrates code for overlaying the closed-loop responses with different choices of compensator so you can compare and contrast different designs. Allows 3 different compensators but uses transparent (inefficient) coding for simplicity so users can follow the core steps.

closed_loop_compare_multiple_compensators.mlx This file provides more efficient coding for comparing numerous compensators on the same system. Allows any number of compensators with minimal changes in the code required (just enter the required system definitions).

closedloop_offset_and_poles.mlx Focuses specifically on how to compute the closed-loop poles and offset (percentage).

PID_tuning_methods.mlx Introduces a number of popular rules for tuning PID compensators.

FREQUENCY RESPONSE METHODS

freq_response_with_matlab.mlx Introduces users to the concept of frequency response and how the gain and phase might be computed from both time domain responses and with complex algebra (i.e. bode.m). [Needs the file ds2nfu.m to support some internal illustrations.]

bode_asymptotes.mlx While modern computing means there is less need to sketch Bode diagrams by hand, some basic insight and understanding into the basic shapes is useful in analysis and design. This file produces asymptote plots to aid such insight.

proportional_design_with_bode.mlx A simple proportional design is often based on a phase margin criteria. This livescript explains and illustrates the key steps.

lag_design_with_bode.mlx This file explains the core steps behind a mechanistic lag design (PM and low freq. gain criteria) and illustrates with examples.

lead_design_with_bode.mlx This file explains the core steps behind a mechanistic lead design (PM and bandwidth criteria) and illustrates with examples.

lead_lag_design_with_bode.mlx This file explains the core steps behind a mechanistic lead-lag design (PM, low freq. gain and bandwidth criteria) and illustrates with examples. Also compares with a lag design and a lead design.

PID_Cont_Controller_Design_with_Pole_Cancellation.m This file explains the core steps behind a mechanistic PID design using a pole cancellation technique and exploiting frequency response methods.

lead_lag_design_with_criteria.m This file combines the insight from the above files and shows how mechanistic designs can be hard coded, simply, to produce the designs based solely on the criteria. This also enables efficient compare and contrast opportunities.

delays_and_bode.mlx Real systems are often subject to small delays, perhaps due to actuator placement or measurement issues. This file allows users to explore the impact of delays on expected closed-loop performance and indeed to consider how a system re-design of the compensator may be considered.

### THE FILES BELOW LINK TO DISCRETE AND ADVANCED SYSTEMS BUT ONLY DO SO AT A RELATIVELY SUPERFICIAL LEVEL AS THIS IS MORE LINKED TO AWARENESS RAISING THAN MEETING THE FORMAL TEACHING REQUIREMENTS NEEDED IN HIGHER LEVEL COURSES

time_series_models.mlx This file gives a simple introduction to the concept of a time series/discrete systems and how MATLAB can be used to support modelling and analysis with such tools.

time_series_models_and_recursion.mlx Often simulation of a time series model is best handled with a recursion (or loop in code). A simple example of suitable coding is given here.

discrete_models_and_usage.mlx More generally control engineers deploy z-transform models to handle discrete systems. This file gives a rapid introduction to the MATLAB tools which facilitate this and thus explains how users might undertake the corresponding analysis.

discretisation_and_bode.mlx Real systems are often implemented in a discrete fashion, albeit the design was done using a continuous design method. This file allows users to explore the impact of discretisation and the choice of sample rate on the efficacy of the original design.

discrete_models_and_discrete_PID_controller_design.mlx This file then focuses on how PID design can be performed in the discrete case using a pole cancellation technique.

Youla_continuous_controller_design.mlx Advanced approaches to compensator design to take more explicit account of sensitivity to uncertainty.