Control Tutorials for MATLAB and Simulink (2024)

Related Tutorial Links

  • Simulink Control Intro
  • Simulink/MATLAB Interaction
  • Control Activity

Related External Links

Contents

  • Extracting a linear model into MATLAB
  • Implementing PI control
  • Closed-loop response

In the Cruise Control: Simulink Modeling page we created a Simulink model of the cruise control system. You can recreate the model or download it by right-clicking here and selecting Save link as. In this section, we will show how to implement a feedback controller in Simulink to meet the performance specifications for the system.

Extracting a linear model into MATLAB

A linear model of the system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. This is done through the use of In1 and Out1 blocks and the MATLAB function linmod.

  • Replace the Step Block and Scope Block with an In1 and an Out1 block, respectively (these blocks can be found in the Ports & Subsystems library). This defines the input and output of the system for the extraction process.

Control Tutorials for MATLAB and Simulink (1)

Save your file as "ccmodel.slx" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:

m = 1000;b = 50;u = 500;[A,B,C,D] = linmod('ccmodel')cruise_ss = ss(A,B,C,D);
A = -0.0500B = 1.0000e-03C = 1D = 0

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. We will multiply the numerator by 500 to simulate a step input of 500 N. Enter the following command in MATLAB.

step(u*cruise_ss)

Control Tutorials for MATLAB and Simulink (2)

Implementing PI control

In the Cruise Control: PID Control page a PI controller was designed with Control Tutorials for MATLAB and Simulink (3) and Control Tutorials for MATLAB and Simulink (4) to give the desired response. We will implement this in Simulink by first containing the open-loop system from earlier in this page in a Subsystem block.

  • Create a new model window.
  • Drag a Subsystem block from the Ports & Subsystems library into your new model window.

Control Tutorials for MATLAB and Simulink (5)

  • Double-click on this block. You will see a blank window representing the contents of the subsystem (which is currently empty).
  • Open your previously saved model of the cruise control system, ccmodel.slx.
  • Select Select All from the Edit menu (or Ctrl-A), and select Copy from the Edit menu (or Ctrl-C).
  • Select the blank subsystem window from your new model and select Paste from the Edit menu (or Ctrl-V). You should see your original system in this new subsystem window. Close this window.
  • You should now see input and output terminals on the Subsystem block. Name this block "plant model".

Control Tutorials for MATLAB and Simulink (6)

Now, we will build a PI controller around the plant model. First, we will feed back the plant output.

  • Draw a line extending from the plant output.
  • Insert a Sum block and assign "+-" to it's inputs.
  • Tap a line of the output line and draw it to the negative input of the Sum block.

Control Tutorials for MATLAB and Simulink (7)

The output of the Sum block will provide the error signal. From this, we will generate proportional and integral components.

  • Insert an Integrator block after the Sum block and connect them with a line.
  • Insert and connect a Gain block after the Integrator block to provide the integral gain.
  • Label this Integrator "Ki" and assign it a value of "Ki".
  • Insert a new Gain block and connect it with a line tapped off the output of the Sum block.
  • Label this gain "Kp" and assign it a value of "Kp".

Control Tutorials for MATLAB and Simulink (8)

Now we will add the proportional and integral components and apply the sum to the plant.

  • Insert a Sum block between the Ki block and the plant model and connect the outputs of the two Gain blocks to the Sum block inputs.
  • Connect the Sum block output to the input of the plant block.

Control Tutorials for MATLAB and Simulink (9)

Finally, we will apply a step input and view the output with a Scope block.

  • Attach a Step block to the free input of the feedback Sum block.
  • Attach a Scope block to the plant output.
  • Double-click the Step block and set the Step Time to "0" and the Final Value to "u". This allows the input magnitude to be changed outside of Simulink.

Control Tutorials for MATLAB and Simulink (10)

You can download our version of the closed-loop system model by right-clicking here and selecting Save link as.

In this example, we constructed a PI controller from fundamental blocks. As an alternative, we could have used a Transfer Function block (from the Continuous library) to implement this in one step, as shown below.

Control Tutorials for MATLAB and Simulink (11)

You can download this model by right-clicking here and selecting Save link as.

Closed-loop response

To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "10" in the Stop Time field. The design requirements included a rise time of less than 5 sec, so we simulate for 10 seconds to view the output. The physical parameters must now be set. Run the following commands at the MATLAB prompt:

m = 1000;b = 50;r = 10;Kp = 800;Ki = 40;

Run the simulation (hit Ctrl-T or select Run from the Simulation menu). When the simulation is finished, you should see the following output.

Control Tutorials for MATLAB and Simulink (12)


Published with MATLAB® 9.2

Control Tutorials for MATLAB and Simulink (2024)

FAQs

How to make a control system in MATLAB? ›

Build models that represent your control system using model objects. Collect MIMO data, estimate and compare models, and view corresponding model responses. Perform online parameter estimation for a time-varying ARX model at the MATLAB command line. Estimate multiple parameters of a model by iterated estimations.

How to learn MATLAB Simulink? ›

Start learning MATLAB and Simulink with free tutorials. Expand your knowledge through interactive courses, explore documentation and code examples, or watch how-to videos on product capabilities. Note: You must be on a desktop computer to take courses.

What is control model in Simulink? ›

Simulink Control Design lets you design and analyze control systems modeled in Simulink. You can automatically tune arbitrary SISO and MIMO control architectures, including PID controllers. PID autotuning can be deployed to embedded software for automatically computing PID gains in real time.

Why use Simulink over MATLAB? ›

You can also create custom blocks using MATLAB functions or other Simulink models. Simulink blocks provide a visual representation of your system, which can help you to verify its logic and behavior. On the other hand, MATLAB code requires you to write and edit text commands, which can be more complex and error-prone.

What is MATLAB control system toolbox? ›

Control System Toolbox™ provides algorithms and apps for systematically analyzing, designing, and tuning linear control systems. You can specify your system as a transfer function, state-space, zero-pole-gain, or frequency-response model.

How to design a controller in MATLAB? ›

To design a controller, first select the controller sample time and horizons, and specify any required constraints. For more information, see Choose Sample Time and Horizons and Specify Constraints. You can then adjust the controller weights to achieve your desired performance. See Tune Weights for more information.

Is MATLAB Simulink hard to learn? ›

Although Matlab is not considered to be a programming language, it really is easy to learn. When you write code on Matlab you actually don't care about declaring data types, allocating memories e.t.c like you do in other programming languages.

How long does it take to learn MATLAB Simulink? ›

If you're a novice programmer, you can expect it to take a little longer than if you were a more seasoned programmer. Someone who can afford to devote all their time to MATLAB can finish learning the language in two weeks. If you have a lot of other responsibilities, however, it will take you longer to complete.

How to run Simulink step by step? ›

In the Simulink Toolstrip, on the Simulation tab, click Step Forward to start a simulation of the model vdp . The simulation starts and pauses just after calculating the output values for the first simulation time and before stepping to the next simulation time.

What is MATLAB Simulink used for? ›

Simulink is a block diagram environment used to design systems with multidomain models, simulate before moving to hardware, and deploy without writing code.

How to connect two blocks in MATLAB Simulink? ›

You can connect most blocks using signal lines. Signal lines connect to ports on blocks. During simulation, signals travel from output ports to connected input ports. You can send the signal from the output port of one block to the input ports of multiple blocks by branching the signal line.

What is CMS in MATLAB? ›

A constant maturity swap (CMS) is a special kind of interest-rate swap, where one of its two legs is a floating "CMS leg" that resets periodically using the par swap rate at a constant tenor (for example, a 10 year swap rate).

Is Simulink separate from MATLAB? ›

Simulink is an add-on product for MATLAB that creates a graphical, interactive environment for modeling, simulating, and analyzing dynamic systems.

Is Simulink worth it? ›

Simulink is one of the most effective block diagram environment for modelling, simulation, and analysis of diverse systems. It is an intuitive tool that is very simple to understand.

What is the point of Simulink? ›

Simulink is the platform for Model-Based Design that supports system-level design, simulation, automatic code generation, and continuous test and verification of embedded systems. Key capabilities include: A graphical editor for modeling all components of a system.

How to create a system in MATLAB? ›

Create System Object Class
  1. In MATLAB, on the Editor tab, select New > System Object > Basic. A simple System object template opens.
  2. Subclass your object from matlab. System . Replace Untitled with AddOne in the first line of your file. classdef AddOne < matlab.System. ...
  3. Save the file and name it AddOne. m .

How to make a PID controller MATLAB? ›

Creation
  1. Create a model using the pid function.
  2. Use the pidtune function to tune PID controllers for a plant model. Specify a 1-DOF PID controller type in the type argument of the pidtune function to obtain a parallel-form PID controller. ...
  3. Interactively tune the PID controller for a plant model using:

What is control structure in MATLAB? ›

For-loops are examples of control structures: ways to specify the flow of control in a program. Let's illustrate what this is with a simple example: Imagine that you need to print the numbers 1, 2, and 3. In Matlab you could do this by typing disp(1) and hitting return; then disp(2) , and disp(3) .

References

Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5404

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.