Control Tutorials for MATLAB and Simulink (2024)

In this tutorial we will introduce a simple, yet versatile, feedback compensator structure: the Proportional-Integral-Derivative (PID) controller. The PID controller is widely employed because it is very understandable and because it is quite effective. One attraction of the PID controller is that all engineers understand conceptually differentiation and integration, so they can implement the control system even without a deep understanding of control theory. Further, even though the compensator is simple, it is quite sophisticated in that it captures the history of the system (through integration) and anticipates the future behavior of the system (through differentiation). We will discuss the effect of each of the PID parameters on the dynamics of a closed-loop system and will demonstrate how to use a PID controller to improve a system's performance.

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

Related Tutorial Links

  • Circuit Control Activity
  • Temp Control Activity
  • Motor Control Activity

Related External Links

Contents

  • PID Overview
  • The Characteristics of the P, I, and D Terms
  • Example Problem
  • Open-Loop Step Response
  • Proportional Control
  • Proportional-Derivative Control
  • Proportional-Integral Control
  • Proportional-Integral-Derivative Control
  • General Tips for Designing a PID Controller
  • Automatic PID Tuning

PID Overview

In this tutorial, we will consider the following unity-feedback system:

Control Tutorials for MATLAB and Simulink (1)

The output of a PID controller, which is equal to the control input to the plant, is calculated in the time domain from the feedback error as follows:

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

First, let's take a look at how the PID controller works in a closed-loop system using the schematic shown above. The variable (Control Tutorials for MATLAB and Simulink (3)) represents the tracking error, the difference between the desired output (Control Tutorials for MATLAB and Simulink (4)) and the actual output (Control Tutorials for MATLAB and Simulink (5)). This error signal (Control Tutorials for MATLAB and Simulink (6)) is fed to the PID controller, and the controller computes both the derivative and the integral of this error signal with respect to time. The control signal (Control Tutorials for MATLAB and Simulink (7)) to the plant is equal to the proportional gain (Control Tutorials for MATLAB and Simulink (8)) times the magnitude of the error plus the integral gain (Control Tutorials for MATLAB and Simulink (9)) times the integral of the error plus the derivative gain (Control Tutorials for MATLAB and Simulink (10)) times the derivative of the error.

This control signal (Control Tutorials for MATLAB and Simulink (11)) is fed to the plant and the new output (Control Tutorials for MATLAB and Simulink (12)) is obtained. The new output (Control Tutorials for MATLAB and Simulink (13)) is then fed back and compared to the reference to find the new error signal (Control Tutorials for MATLAB and Simulink (14)). The controller takes this new error signal and computes an update of the control input. This process continues while the controller is in effect.

The transfer function of a PID controller is found by taking the Laplace transform of Equation (1).

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

where Control Tutorials for MATLAB and Simulink (16) = proportional gain, Control Tutorials for MATLAB and Simulink (17) = integral gain, and Control Tutorials for MATLAB and Simulink (18) = derivative gain.

We can define a PID controller in MATLAB using a transfer function model directly, for example:

Kp = 1;Ki = 1;Kd = 1;s = tf('s');C = Kp + Ki/s + Kd*s
C = s^2 + s + 1 ----------- s Continuous-time transfer function.

Alternatively, we may use MATLAB's pid object to generate an equivalent continuous-time controller as follows:

C = pid(Kp,Ki,Kd)
C = 1 Kp + Ki * --- + Kd * s s with Kp = 1, Ki = 1, Kd = 1 Continuous-time PID controller in parallel form.

Let's convert the pid object to a transfer function to verify that it yields the same result as above:

tf(C)
ans = s^2 + s + 1 ----------- s Continuous-time transfer function.

The Characteristics of the P, I, and D Terms

Increasing the proportional gain (Control Tutorials for MATLAB and Simulink (19)) has the effect of proportionally increasing the control signal for the same level of error. The fact that the controller will "push" harder for a given level of error tends to cause the closed-loop system to react more quickly, but also to overshoot more. Another effect of increasing Control Tutorials for MATLAB and Simulink (20) is that it tends to reduce, but not eliminate, the steady-state error.

The addition of a derivative term to the controller (Control Tutorials for MATLAB and Simulink (21)) adds the ability of the controller to "anticipate" error. With simple proportional control, if Control Tutorials for MATLAB and Simulink (22) is fixed, the only way that the control will increase is if the error increases. With derivative control, the control signal can become large if the error begins sloping upward, even while the magnitude of the error is still relatively small. This anticipation tends to add damping to the system, thereby decreasing overshoot. The addition of a derivative term, however, has no effect on the steady-state error.

The addition of an integral term to the controller (Control Tutorials for MATLAB and Simulink (23)) tends to help reduce steady-state error. If there is a persistent, steady error, the integrator builds and builds, thereby increasing the control signal and driving the error down. A drawback of the integral term, however, is that it can make the system more sluggish (and oscillatory) since when the error signal changes sign, it may take a while for the integrator to "unwind."

The general effects of each controller parameter (Control Tutorials for MATLAB and Simulink (24), Control Tutorials for MATLAB and Simulink (25), Control Tutorials for MATLAB and Simulink (26)) on a closed-loop system are summarized in the table below. Note, these guidelines hold in many cases, but not all. If you truly want to know the effect of tuning the individual gains, you will have to do more analysis, or will have to perform testing on the actual system.

CL RESPONSE
RISE TIME
OVERSHOOT
SETTLING TIME
S-S ERROR
Kp
Decrease
Increase
Small Change
Decrease
Ki
Decrease
Increase
Increase
Decrease
Kd
Small Change
Decrease
Decrease
No Change

Example Problem

Suppose we have a simple mass-spring-damper system.

Control Tutorials for MATLAB and Simulink (27)

The governing equation of this system is

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

Taking the Laplace transform of the governing equation, we get

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

The transfer function between the input force Control Tutorials for MATLAB and Simulink (30) and the output displacement Control Tutorials for MATLAB and Simulink (31) then becomes

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

Let

 m = 1 kg b = 10 N s/m k = 20 N/m F = 1 N

Substituting these values into the above transfer function

(6)Control Tutorials for MATLAB and Simulink (33)

The goal of this problem is to show how each of the terms, Control Tutorials for MATLAB and Simulink (34), Control Tutorials for MATLAB and Simulink (35), and Control Tutorials for MATLAB and Simulink (36), contributes to obtaining the common goals of:

  • Fast rise time
  • Minimal overshoot
  • Zero steady-state error

Open-Loop Step Response

Let's first view the open-loop step response. Create a new m-file and run the following code:

s = tf('s');P = 1/(s^2 + 10*s + 20);step(P)

Control Tutorials for MATLAB and Simulink (37)

The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output to a unit step input. This corresponds to a steady-state error of 0.95, which is quite large. Furthermore, the rise time is about one second, and the settling time is about 1.5 seconds. Let's design a controller that will reduce the rise time, reduce the settling time, and eliminate the steady-state error.

Proportional Control

From the table shown above, we see that the proportional controller (Control Tutorials for MATLAB and Simulink (38)) reduces the rise time, increases the overshoot, and reduces the steady-state error.

The closed-loop transfer function of our unity-feedback system with a proportional controller is the following, where Control Tutorials for MATLAB and Simulink (39) is our output (equals Control Tutorials for MATLAB and Simulink (40)) and our reference Control Tutorials for MATLAB and Simulink (41) is the input:

(7)Control Tutorials for MATLAB and Simulink (42)

Let the proportional gain (Control Tutorials for MATLAB and Simulink (43)) equal 300 and change the m-file to the following:

Kp = 300;C = pid(Kp)T = feedback(C*P,1)t = 0:0.01:2;step(T,t)
C = Kp = 300 P-only controller.T = 300 ---------------- s^2 + 10 s + 320 Continuous-time transfer function.

Control Tutorials for MATLAB and Simulink (44)

The above plot shows that the proportional controller reduced both the rise time and the steady-state error, increased the overshoot, and decreased the settling time by a small amount.

Proportional-Derivative Control

Now, let's take a look at PD control. From the table shown above, we see that the addition of derivative control (Control Tutorials for MATLAB and Simulink (45)) tends to reduce both the overshoot and the settling time. The closed-loop transfer function of the given system with a PD controller is:

(8)Control Tutorials for MATLAB and Simulink (46)

Let Control Tutorials for MATLAB and Simulink (47) equal 300 as before and let Control Tutorials for MATLAB and Simulink (48) equal 10. Enter the following commands into an m-file and run it in the MATLAB command window.

Kp = 300;Kd = 10;C = pid(Kp,0,Kd)T = feedback(C*P,1)t = 0:0.01:2;step(T,t)
C = Kp + Kd * s with Kp = 300, Kd = 10 Continuous-time PD controller in parallel form.T = 10 s + 300 ---------------- s^2 + 20 s + 320 Continuous-time transfer function.

Control Tutorials for MATLAB and Simulink (49)

This plot shows that the addition of the derivative term reduced both the overshoot and the settling time, and had a negligible effect on the rise time and the steady-state error.

Proportional-Integral Control

Before proceeding to PID control, let's investigate PI control. From the table, we see that the addition of integral control (Control Tutorials for MATLAB and Simulink (50)) tends to decrease the rise time, increase both the overshoot and the settling time, and reduces the steady-state error. For the given system, the closed-loop transfer function with a PI controller is:

(9)Control Tutorials for MATLAB and Simulink (51)

Let's reduce Control Tutorials for MATLAB and Simulink (52) to 30, and let Control Tutorials for MATLAB and Simulink (53) equal 70. Create a new m-file and enter the following commands.

Kp = 30;Ki = 70;C = pid(Kp,Ki)T = feedback(C*P,1)t = 0:0.01:2;step(T,t)
C = 1 Kp + Ki * --- s with Kp = 30, Ki = 70 Continuous-time PI controller in parallel form.T = 30 s + 70 ------------------------ s^3 + 10 s^2 + 50 s + 70 Continuous-time transfer function.

Control Tutorials for MATLAB and Simulink (54)

Run this m-file in the MATLAB command window and you should generate the above plot. We have reduced the proportional gain (Control Tutorials for MATLAB and Simulink (55)) because the integral controller also reduces the rise time and increases the overshoot as the proportional controller does (double effect). The above response shows that the integral controller eliminated the steady-state error in this case.

Proportional-Integral-Derivative Control

Now, let's examine PID control. The closed-loop transfer function of the given system with a PID controller is:

(10)Control Tutorials for MATLAB and Simulink (56)

After several iterations of tuning, the gains Control Tutorials for MATLAB and Simulink (57) = 350, Control Tutorials for MATLAB and Simulink (58) = 300, and Control Tutorials for MATLAB and Simulink (59) = 50 provided the desired response. To confirm, enter the following commands to an m-file and run it in the command window. You should obtain the following step response.

Kp = 350;Ki = 300;Kd = 50;C = pid(Kp,Ki,Kd)T = feedback(C*P,1);t = 0:0.01:2;step(T,t)
C = 1 Kp + Ki * --- + Kd * s s with Kp = 350, Ki = 300, Kd = 50 Continuous-time PID controller in parallel form.

Control Tutorials for MATLAB and Simulink (60)

Now, we have designed a closed-loop system with no overshoot, fast rise time, and no steady-state error.

General Tips for Designing a PID Controller

When you are designing a PID controller for a given system, follow the steps shown below to obtain a desired response.

  1. Obtain an open-loop response and determine what needs to be improved
  2. Add a proportional control to improve the rise time
  3. Add a derivative control to reduce the overshoot
  4. Add an integral control to reduce the steady-state error
  5. Adjust each of the gains Control Tutorials for MATLAB and Simulink (61), Control Tutorials for MATLAB and Simulink (62), and Control Tutorials for MATLAB and Simulink (63) until you obtain a desired overall response. You can always refer to the table shown in this "PID Tutorial" page to find out which controller controls which characteristics.

Lastly, please keep in mind that you do not need to implement all three controllers (proportional, derivative, and integral) into a single system, if not necessary. For example, if a PI controller meets the given requirements (like the above example), then you don't need to implement a derivative controller on the system. Keep the controller as simple as possible.

An example of tuning a PI controller on an actual physical system can be found at the following link. This example also begins to illustrate some challenges of implementing control, including: control saturation, integrator wind-up, and noise amplification.

Automatic PID Tuning

MATLAB provides tools for automatically choosing optimal PID gains which makes the trial and error process described above unnecessary. You can access the tuning algorithm directly using pidtune or through a nice graphical user interface (GUI) using pidTuner.

The MATLAB automated tuning algorithm chooses PID gains to balance performance (response time, bandwidth) and robustness (stability margins). By default, the algorithm designs for a 60-degree phase margin.

Let's explore these automated tools by first generating a proportional controller for the mass-spring-damper system by entering the command shown below. In the shown syntax, P is the previously generated plant model, and 'p' specifies that the tuner employ a proportional controller.

 pidTuner(P,'p') 

The pidTuner GUI window, like that shown below, should appear.

Control Tutorials for MATLAB and Simulink (64)

Notice that the step response shown is slower than the proportional controller we designed by hand. Now click on the Show Parameters button on the top right. As expected, the proportional gain, Control Tutorials for MATLAB and Simulink (65), is smaller than the one we employed, Control Tutorials for MATLAB and Simulink (66) = 94.86 < 300.

We can now interactively tune the controller parameters and immediately see the resulting response in the GUI window. Try dragging the Response Time slider to the right to 0.14 s, as shown in the figure below. This causes the response to indeed speed up, and we can see Control Tutorials for MATLAB and Simulink (67) is now closer to the manually chosen value. We can also see other performance and robustness parameters for the system. Note that before we adjusted the slider, the target phase margin was 60 degrees. This is the default for the pidTuner and generally provides a good balance between robustness and performance.

Control Tutorials for MATLAB and Simulink (68)

Now let's try designing a PID controller for our system. By specifying the previously designed or (baseline) controller, C, as the second parameter, pidTuner will design another PID controller (instead of P or PI) and will compare the response of the system with the automated controller with that of the baseline.

 pidTuner(P,C) 

We see in the output window that the automated controller responds slower and exhibits more overshoot than the baseline. Now choose the Domain: Frequency option from the toolstrip, which reveals frequency domain tuning parameters.

Control Tutorials for MATLAB and Simulink (69)

Now type in 32 rad/s for Bandwidth and 90 deg for Phase Margin, to generate a controller similar in performance to the baseline. Keep in mind that a higher closed-loop bandwidth results in a faster rise time, and a larger phase margin reduces the overshoot and improves the system stability.

Finally, we note that we can generate the same controller using the command line tool pidtune instead of the pidTuner GUI employing the following syntax.

opts = pidtuneOptions('CrossoverFrequency',32,'PhaseMargin',90);[C, info] = pidtune(P, 'pid', opts)
C = 1 Kp + Ki * --- + Kd * s s with Kp = 320, Ki = 796, Kd = 32.2 Continuous-time PID controller in parallel form.info = struct with fields: Stable: 1 CrossoverFrequency: 32 PhaseMargin: 90


Published with MATLAB® 9.2

Control Tutorials for MATLAB and Simulink (2024)

FAQs

How do I find answers in MATLAB? ›

To view all of your solutions, go to a Problem page and click View my solutions. You can view your solutions in a list or in the Solution Map. If using the list view, you can review the display by selecting a Sort by option.

Is MATLAB Simulink hard to learn? ›

MATLAB is designed for the way you think and the work you do, so learning is accessible whether you are a novice or an expert. The Help Center is always available to guide you with robust documentation, community answers, and how-to videos. Additionally, online interactive training is a great way to get started.

How much time is required to learn MATLAB? ›

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 pass structure to Simulink? ›

To connect the structure input or output in a MATLAB function with Simulink, you must define a Simulink. Bus object in the base workspace. Then use this bus object as signal datra type for the signals which are to be connected to Matlab function.

How do you get a long answer in MATLAB? ›

To format the way numbers display, do one of the following:
  1. On the Home tab, in the Environment section, click Preferences. Select MATLAB > Command Window, and then choose a Numeric format option.
  2. Use the format function, for example: format short format short e format long.

How do I find Solver in MATLAB? ›

Description. S = solve( eqn , var ) solves the equation eqn for the variable var . If you do not specify var , the symvar function determines the variable to solve for. For example, solve(x + 1 == 2, x) solves the equation x + 1 = 2 for x.

Is MATLAB harder than Python? ›

The Difference in Technical Computing:

They are both used for the same type of work, such as numerical analysis, data visualization, and scientific computation. When it comes to syntax and readability, Python is often easier to read and understand than MATLAB.

What is the salary of MATLAB Simulink? ›

Average Annual Salary by Experience

Matlab Developer salary in India with less than 1 year of experience to 5 years ranges from ₹ 2.0 Lakhs to ₹ 9.4 Lakhs with an average annual salary of ₹ 5.6 Lakhs based on 342 latest salaries.

Is MATLAB enough for a job? ›

Conclusion. The industry has some familiar buzz that learning MATLAB will not be a good opportunity for a better career. But this is not fully true. Yes, it is an acceptable reason that salary or company structure will never be able to touch available popular jobs on other programming technologies.

Is MATLAB in high demand? ›

Matlab careers are actually on the rise today. It's a very popular programming language. It can be used by a developer, engineer, programmer, scientist, etc. to collect and sort out data, and develop apps, software, and sites.

Can I learn MATLAB in 1 month? ›

If you want to become an expert in Matlab then you need to mention which part of madlab you want to learn and want expertise. If I generalize my answer highly, It may take at least 3 months to learn matlab and may take maximum 3 years to become an expert.

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.

How to generate code from Simulink? ›

To generate code, you must make the following changes: In the Modeling tab of the model toolstrip, click Model Settings. The Configuration Parameters dialog opens. Navigate to the Code Generation tab, select the Generate code only parameter, and click Apply.

How to check results in MATLAB? ›

View Results in Command Window

The Summary Report link provides access to the Model Advisor Command-Line Summary report. You can review additional results in the Command Window by calling the DisplayResults parameter when you run the Model Advisor.

How do you find the step response in MATLAB? ›

[ y , tOut ] = step( sys , tFinal ) computes the step response from t = 0 to the end time t = tFinal . [ y , tOut ] = step( sys , t ) returns the step response of a dynamic system model sys at the times specified in the vector t .

How do I find something in MATLAB code? ›

Search Using Find Dialog Box

The Find dialog box opens. The search begins at the current cursor position. MATLAB finds the text you specified and highlights it. MATLAB beeps when a search for Find Next reaches the end of the Command Window, or when a search for Find Previous reaches the top of the Command Window.

What is the ANS command in MATLAB? ›

ans is the variable created when an output is returned without a specified output argument. MATLAB® creates the ans variable and stores the output there. Changing or using the value of ans in a script or function is not recommended, as the value can change frequently. ans is specific to the current workspace.

Top Articles
Minecraft: The Return of Bundles Should Herald a Broader QoL Update
How to Play Split Screen in Fortnite
Bon plan – Le smartphone Motorola Edge 50 Fusion "4 étoiles" à 339,99 €
Hemispheres Dothan Al
Double Helicath Clan Boss
Levidia 2019
We Will Collide Someday Novel
How Much Food Should I Buy For Christmas? | Gousto Christmas
Recruitment Drive/Quick guide
Nazir Afzal on the BBC: ‘Powerful predators were allowed to behave terribly on an industrial level’
New Orleans Pelicans News, Scores, Status, Schedule - NBA
Q-global Web-based Administration, Scoring, and Reporting
Dr Paul Memorial Medical Center
Expendables 4 Showtimes Near Cinemark 14 Rockwall And Xd
Seafood Restaurants Open Late Near Me
2013 Chevy Sonic Freon Capacity
Knock At The Cabin Showtimes Near Fat Cats Mesa
Haverhill, MA Obituaries | Driscoll Funeral Home and Cremation Service
Myth or Fact: Massage Parlors and How They Play a Role in Trafficking | OUR Rescue
ZQuiet Review | My Wife and I Both Tried ZQuiet for Snoring
Wayne State Dean's List
Icl Urban Dictionary
Worlds Hardest Game Tyrone
Joy Ride 2023 Showtimes Near Amc Ward Parkway
Names of the dead: September 11, 2001
Natasha Tillotson
Kickflip Seeds
Ella Phipps Haughton
A 100% Honest Review of M. Gemi Shoes — The Laurie Loo
Modesto Personals Craigslist
Uw Madison Mechanical Engineering Flowchart
What is a W-8BEN Form and Why Does It Matter?
Great Clips Radio Road
Preventice Learnworlds
Nikki Porsche Girl Head
Keanu Reeves cements his place in action genre with ‘John Wick: Chapter 4’
Flixmate Chrome Extension
Charlotte North Carolina Craigslist Pets
Drugst0Recowgirl Leaks
Dyi Urban Dictionary
iPhone reconditionné
China Rose Plant Care: Water, Light, Nutrients | Greg App 🌱
Rg353M Vs Rg351Mp
Margie's Money Saver Hey Dudes
No Hard Feelings Showtimes Near Silvermoon Drive-In
Whats On Metv Now
Metrocast Channel Lineup
Online-Shopping bei Temu: Solltest du lieber die Finger davon lassen?
Was genau ist eine pillow princess?
Jili Game Cityjili
Closest Asian Supermarket
South Florida residents must earn more than $100,000 to avoid being 'rent burdened'
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5287

Rating: 4 / 5 (71 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.