Control Tutorials for MATLAB and Simulink (2025)

Key MATLAB commands used in this tutorial are: tf , feedback , step , rlocus

Related Tutorial Links

  • Intro to PID Control
  • Root Locus Activity

Related External Links

Contents

  • Adding a PID controller
  • Plotting the closed-loop response
  • Choosing the gains for the PID controller

From the main problem, the dynamic equations in transfer function form are the following:

(1)Control Tutorials for MATLAB and Simulink (1)

(2)Control Tutorials for MATLAB and Simulink (2)

where,

(3)Control Tutorials for MATLAB and Simulink (3)

and the system schematic is the following where F(s)G1(s) = G2(s).

Control Tutorials for MATLAB and Simulink (4)

For the original problem and the derivation of the above equations and schematic, please refer to the Suspension: System Modeling page.

We want to design a feedback controller so that when the road disturbance (W) is simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds and an overshoot less than 5%. For example, when the bus runs onto a 10-cm step, the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5 seconds.

The system model can be represented in MATLAB by creating a new m-file and entering the following commands (refer to the main problem for the details of getting those commands).

m1 = 2500;m2 = 320;k1 = 80000;k2 = 500000;b1 = 350;b2 = 15020;nump=[(m1+m2) b2 k2];denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2];G1=tf(nump,denp);num1=[-(m1*b2) -(m1*k2) 0 0];den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+(b2*k1) k1*k2];G2=tf(num1,den1);numf=num1;denf=nump;F=tf(numf,denf);

Adding a PID controller

Recall that the transfer function for a PID controller is:

(4)Control Tutorials for MATLAB and Simulink (5)

where Control Tutorials for MATLAB and Simulink (6) is the proportional gain, Control Tutorials for MATLAB and Simulink (7) is the integral gain, and Control Tutorials for MATLAB and Simulink (8) is the derivative gain. Let's assume that we will need all three of these gains in our controller. To begin, we might start with guessing a gain for each: Control Tutorials for MATLAB and Simulink (9)=208025, Control Tutorials for MATLAB and Simulink (10)=832100 and Control Tutorials for MATLAB and Simulink (11)=624075. This can be implemented into MATLAB by adding the following code into your m-file:

Kd = 208025;Kp = 832100;Ki = 624075;C = pid(Kp,Ki,Kd);

Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the road. From the schematic above we can find the transfer function from the road disturbance W to the output(X1-X2), and simulate:

sys_cl=F*feedback(G1,C);

Plotting the closed-loop response

Now we have created the closed-loop transfer function in MATLAB that will represent the plant, the disturbance, as well as the controller. Let's see what the closed-loop step response for this system looks like before we begin the control process. Keep in mind that we are going to use a 0.1-m step as our disturbance, to simulate this, all we need to do is to multiply sys_cl by 0.1. Add the following code to your m-file. You should see the response (X1-X2) to a step W like this:

t=0:0.05:5;step(0.1*sys_cl,t)title('Response to a 0.1-m Step under PID Control')

Control Tutorials for MATLAB and Simulink (12)

From the graph, the percent overshoot is 9mm, which is larger than the 5mm requirement, but the settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable output from the beginning, we start with choosing a pole and two zeros for PID controller. A pole of this controller must be at zero and one of the zeros has to be very close to the pole at the origin, at 1. The other zero, we will put further from the first zero, at 3, actually we can adjust the second-zero's position to get the system to fulfill the requirement. Add the following command in the m-file, so you can adjust the second-zero's location and choose the gain to have a rough idea what gain you should use for Control Tutorials for MATLAB and Simulink (13), Control Tutorials for MATLAB and Simulink (14), and Control Tutorials for MATLAB and Simulink (15).

z1=1;z2=3;p1=0;s = tf('s');C = ((s+z1)*(s+z2))/(s+p1);rlocus(C*G1)title('root locus with PID controller')

Control Tutorials for MATLAB and Simulink (16)

Add the code [k,poles]=rlocfind(C*G1) onto the end of your m-file to help you choose a specific loop gain. After running in the command window, go to the root locus plot and select a point near those indicated by the cross marks on the plot below.

Control Tutorials for MATLAB and Simulink (17)

After doing this, you should see the following output in the MATLAB command window.

Select a point in the graphics window selected_point = -4.0047 + 9.6694i k = 5.2714e+04 poles = 1.0e+02 * -2.1838 + 0.0000i -0.0415 + 0.0967i -0.0415 - 0.0967i -0.0670 + 0.0000i -0.0061 + 0.0000i 

Note that the values returned in your MATLAB command window may not be exactly the same, but should at least have the same order of magnitude.

We will explain the root locus method in more detail in the Suspension: Root Locus Controller Design page.

Choosing the gains for the PID controller

Now that we have the closed-loop transfer function, controlling the system is simply a matter of tuning the Control Tutorials for MATLAB and Simulink (18), Control Tutorials for MATLAB and Simulink (19), and Control Tutorials for MATLAB and Simulink (20) gains. From the figure above, we can see that the system has larger damping than required, but the settling time is very short. This response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can be rectified by adjusting the Control Tutorials for MATLAB and Simulink (21), Control Tutorials for MATLAB and Simulink (22) and Control Tutorials for MATLAB and Simulink (23) gains to obtain a better response. Let's increase Control Tutorials for MATLAB and Simulink (24), Control Tutorials for MATLAB and Simulink (25), and Control Tutorials for MATLAB and Simulink (26) by a factor of 2 to see what will happen. Go back to your m-file and multiply Control Tutorials for MATLAB and Simulink (27), Control Tutorials for MATLAB and Simulink (28), Control Tutorials for MATLAB and Simulink (29) by 2 and then rerun the program, you should get the following plot.

Kd=2*Kd;Kp=2*Kp;Ki=2*Ki;C=pid(Kp,Ki,Kd);sys_cl=F*feedback(G1,C);step(0.1*sys_cl,t)title('Response to a 0.1-m Step w/ High-Gain PID')

Control Tutorials for MATLAB and Simulink (30)

To compare this graph with the graph of low-gain PID controller, you can change the axis:

axis([0 5 -.01 .01])

Control Tutorials for MATLAB and Simulink (31)

Now we see that the percent overshoot and settling time meet the requirements of the system. The percent overshoot is about 5% of the input's amplitude and settling time is 2 seconds which is less than the 5 second requirement.

For this problem, it turns out that the PID design method adequately controls the system. This can be seen by looking at the root locus plot. Such a task can be achieved by simply changing only the gains of a PID controller. Feel free to play around with all three of the parameters, Control Tutorials for MATLAB and Simulink (32), Control Tutorials for MATLAB and Simulink (33), and Control Tutorials for MATLAB and Simulink (34), as we suggested, but you will most likely get the response to have either a large percent overshoot or a long settling time.


Published with MATLAB® 9.2

Control Tutorials for MATLAB and Simulink (2025)

FAQs

How to use 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.

How to simulate a control system in MATLAB? ›

The first step is to define the system that you want to simulate. You need to specify the system parameters, such as the transfer function, the state-space model, the input and output variables, and the initial conditions. You can use MATLAB commands or graphical tools to define the system.

What is the control system toolbox in MATLAB? ›

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.

What are control structures in MATLAB? ›

Introduction to Matlab Control Structures
  • Most of the basic concepts we learned in C/C++ apply in Matlab. ...
  • Relational Operators - >,<,>=,<=,= =, ~ = ...
  • Logical Operators - &, |, ~ for AND, OR, NOT. ...
  • if - elseif - else - end. ...
  • while loops. ...
  • for loops. ...
  • for loops and while loops support break and continue.

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.

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.

How much time does it take to learn Simulink? ›

Learn MATLAB and SIMULINK in one week

Ideal for beginners from any field, it covers vectors, matrices, loops, and functions.

How to design a controller in MATLAB Simulink? ›

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.

What is MATLAB Simulink used for? ›

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 simulate a Simulink model from MATLAB? ›

Simulate a model interactively by clicking the Run button in the Simulink Toolstrip, or programmatically using functions like sim and set_param in the MATLAB Command Window or a MATLAB script. For information about running parallel and batch simulations, see Run Multiple Simulations.

How do you control output in MATLAB? ›

When paging is enabled, MATLAB displays output one page at a time.
  1. To advance to the next page of output, press the Space key.
  2. To advance to the next line of output, press the Return key.
  3. To stop displaying the current output, press the Q key. Do not use Ctrl+C to exit more , otherwise MATLAB can return an error.

What is control flow in MATLAB? ›

A control flow subsystem executes one or more times at the current time step when enabled by a control flow block. A control flow block implements control logic similar to that expressed by control flow statements of programming languages (e.g., if-then , while-do , switch , and for ).

How to make 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.

How to use source control in MATLAB? ›

Setup Source Control
  1. On the Home tab, click New > Project > From Git or New > Project > From SVN. ...
  2. If you know your repository location, paste it into the Repository path field. ...
  3. In the Sandbox field, select the working folder where you want to put the retrieved files for your new project. ...
  4. Click Retrieve.

How does a control system work? ›

A control system is a set of mechanical or electronic devices that regulates other devices or systems by way of control loops. Typically, control systems are computerized. Control systems are a central part of production and distribution in many industries. Automation technology plays a big role in these systems.

How to use AC function in MATLAB? ›

Call C Code from a Simulink Model
  1. Identify the source ( . ...
  2. Insert a MATLAB Function block into your model.
  3. In the MATLAB Function block, use the coder. ...
  4. Specify the C source and header files in the Simulation Target pane of the Configuration Parameters window. ...
  5. Test your Simulink model and ensure it functions correctly.

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Melvina Ondricka

Last Updated:

Views: 5281

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.