Control Tutorials for MATLAB and Simulink (2024)

Key MATLAB commands used in this tutorial are: tf , c2d , minreal , zpk , feedback , stairs , step

Related Tutorial Links

  • Digital Control Intro
  • Discrete-time Poles
  • Sampling Activity

Related External Links

Contents

  • Creating a sampled-date model of the plant
  • Root locus design

In this example, the controller will be designed applying a Root Locus method. A sample-data DC motor model can be obtained from conversion of an analog DC motor model, as we will describe. According to the DC Motor Position: System Modeling page, the continuos open-loop transfer function for DC motor's position in the Laplace domain is the following.

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

The structure of the control system has the form shown in the figure below.

Control Tutorials for MATLAB and Simulink (2)

Also from the main page, the design requirements for a 1-radian step reference are the following.

Creating a sampled-date model of the plant

The first step in the design of a digital control system is to generate a sampled-data model of the plant. Therefore, it is necessary to choose a frequency with which the continuous-time plant is sampled. In choosing a sampling period, it is desired that the sampling frequency be fast compared to the dynamics of the system in order that the sampled output of the system captures the system's full behavior, that is, so that significant inter-sample behavior isn't missed.

Let's create a continuous-time model of the plant. Create a new m-file and add the following MATLAB code (refer to the main problem for the details of getting these commands). Running the m-file within the MATLAB command window will generate the output shown below.

J = 3.2284E-6;b = 3.5077E-6;K = 0.0274;R = 4;L = 2.75E-6;s = tf('s');P_motor = K/(s*((J*s+b)*(L*s+R)+K^2));zpk(P_motor)
ans = 3.0862e+09 ------------------------ s (s+1.454e06) (s+59.23) Continuous-time zero/pole/gain model.

The use of the zpk command above transforms the transfer function into a form where the zeros, poles, and gain can be seen explicitly. Examining the poles of the plant (or its frequency response), it is clear that the pole at -1.45e06 contributes very little to the response of the plant. The gain crossover frequency of the plant is approximately 5 Hz. Therefore, choosing a sampling period of 0.001 seconds (frequency of 1000 Hz) is significantly faster than the dynamics of the plant. This sampling period is also fast compared to the speed that will be achieved by the resultant closed-loop system. A sample time of 0.001 seconds is specifically 1/100 of the required time constant and 1/40 of the required settling time.

In this case, we will convert the given transfer function from the continuous Laplace domain to the discrete z-domain. MATLAB can be used to achieve this conversion through the use of the c2d command. The c2d command requires three arguments: a system model, the sampling time (Ts), and the type of hold circuit. In this example, we will assume a zero-order hold (zoh) circuit. Refer to the Introduction: Digital Controller Design page for further details. Adding the following commands to your m-file and running in the MATLAB command window generates the sampled-data model shown below.

Ts = 0.001;dP_motor = c2d(P_motor, Ts, 'zoh');zpk(dP_motor)
ans = 0.0010389 (z+0.9831) (z+9.256e-07) ---------------------------------- z (z-1) (z-0.9425) Sample time: 0.001 secondsDiscrete-time zero/pole/gain model.

From the above, notice that there is a pole and zero very near to z = 0 that effectively cancel. This cancellation in the transfer function can be accomplished by applying the minreal command with a tolerance of 0.001. Cancellation of this pole and zero will reduce the order of our transfer function and will help to avoid numerical difficulties in MATLAB. Applying the minreal command, therefore, produces the following reduced order transfer function. Note the absence of the pole and zero near z = 0.

dP_motor = minreal(dP_motor,0.001);zpk(dP_motor)
ans = 0.0010389 (z+0.9831) -------------------- (z-1) (z-0.9425) Sample time: 0.001 secondsDiscrete-time zero/pole/gain model.

We would now like to analyze the closed-loop response of the system without any additional compensation. First, we have to close the loop of the transfer function by using the feedback command. After closing the loop, let's inspect the closed-loop step response with the zero-order hold. This can be accomplished by using the step and stairs commands. Since the step command is fed a discrete model, it will output a vector of discrete samples at the sample time Ts associated with the model (click here for further details). The syntax below specifies to simulate the step response for 0.5 seconds. The stairs command draws these discrete data points as a stairstep, just like what would be produced by a zero-order hold circuit. Add the following code at the end of your previous m-file and rerun it. You should generate a plot like the one shown below.

sys_cl = feedback(dP_motor,1);[x1,t] = step(sys_cl,.5);stairs(t,x1)xlabel('Time (seconds)')ylabel('Position (radians)')title('Stairstep Response: Original')grid

Control Tutorials for MATLAB and Simulink (3)

Root locus design

We will now employ a root locus design methodology to generate a digital controller for our system. The main idea of a root locus approach to design is to modify the closed-loop response by placing the closed-loop poles of the system at desired locations. Adding poles and zeros to the the original open-loop transfer function via the controller allows us to reshape the root locus in order to place the closed-loop poles at the desired locations. This approach can also be used for discrete-time models. We will use the Control System Designer graphical user interface (GUI) for designing our controller. Adding the command controlSystemDesigner('rlocus',dP_motor) to your m-file and running at the command line or by going to the Apps tab and clicking on the app icon under Control System Design and Analysis will open the Control System Designer .

One window will open initially named Control System Designer having the form shown in the figure below. This window shows the root locus as well as step response of the transfer function d_Pmotor passed through the controlSystemDesigner function. The root locus appears initially due to the addition of the string 'rlocus' to the function call. If the string 'rlocus' is omitted, the default initial window includes the open-loop Bode plot in addition to the root locus plot and step response.

Control Tutorials for MATLAB and Simulink (4)

Recall from our previous controller designs that we have added an integrator to our compensator in order to reject the effect of a constant disturbance in steady state. We can add integral control through the Control System Designer interface under the Compensator Editor window that can be opened by right-clicking on the root locus plot and then selecting Edit Compensator. Specifically, an integrator is added to our controller by right-clicking on the Dynamics section of the window and selecting Add Pole/Zero > Integrator from the resulting menu. The figure below illustrates what the Compensator Editor window should look like.

Control Tutorials for MATLAB and Simulink (5)

Integral control in the continuous-time domain is 1 / s. The Control System Designer uses a forward difference approximation for mapping from the s-plane to the z-plane as described by s = (z-1) / Ts, where Ts is the sample time being employed. Therefore, the addition of the integrator will add an additional pole at 1 on the root locus plot. See below for the resulting root locus plot where the red x denotes the location of the compensator pole at z = 1.

Control Tutorials for MATLAB and Simulink (6)

The default format for compensators in the Control System Designer is Time constant form. For the remainder of this problem, we will design the compensator in Zero/pole/gain form. This change can be made by choosing Preferences from the CONTROL SYSTEM tab of the Control System Designer window. The compensator parameterization can then be changed under the Options tab.

By default, the root locus plot includes the unit circle shown in the above figure by the dashed line. Recall that for a discrete-time system, closed-loop poles located within the unit circle indicate a stable closed-loop system. From the above, we can see that after adding the extra pole at 1, the root locus had three poles near 1. This caused the root locus to move to the right (outside of the unit circle) indicating that the closed-loop response will be more unstable.

Therefore, we will add a zero near 1, inside the unit circle, to cancel one of the poles and pull the root locus in. We will specifically add a zero at z = 0.95. In general, we must add at least as many poles as zeros for the controller to be causal. This zero can be added to our compensator in the same manner as the integrator was added above. The only difference is that you will choose Real Zero from the right-click menu and then must specify in the Location cell that the zero is to be placed at 0.95. The root locus plot should now appear as shown below.

Control Tutorials for MATLAB and Simulink (7)

The addition of the integrator has helped with the requirement on disturbance rejections, however, the above root locus indicates that the closed-loop system cannot be made robustly stable through further tuning of the loop gain. We also have not addressed our other design requirements. To help us in this regard, let's identify the region of the complex plane where the closed-loop poles must be located.

These regions can be added to our root locus by choosing Design Requirements from the right-click menu. Then choose new and set the overshoot to be less than 16%. This process can then be repeated to add the settling time requirement of less than 0.04 seconds. The resulting figure is shown below where the unshaded region identifies where the closed-loop poles must be located to meet our given transient requirements. These regions assume a canonical second-order system, which we do not have currently, but the regions can still help guide our design.

Control Tutorials for MATLAB and Simulink (8)

From the above figure, we can see that the two dominant branches of our root locus do not pass through the required region. Therefore, we need to add poles and/or zeros to our compensator to bend the root locus into the desired region. Let's first try cancelling the zero at approximately -0.98 by adding a pole nearby. This can be done in the same manner described above for the integrator and the real zero. It is desirable to cancel this zero since it will add overshoot to the step response. The addition of this pole will result in two of the branches of the root locus going off to the right and outside of the unit circle.

We will attempt to pull these branches in by placing two additional zeros near the desired closed-loop pole locations. These zeros can be added via the Compensator Editor window as done above. In this case, however, we will place the zeros using a graphical tuning approach. Specifically, select a single o from the ROOT LOCUS EDITOR tab at the top of the Control System Designer window. Then click the resulting "pointer" on the real axis to place the zero. Repeat this process to add a second zero. The effect of moving these two zeros can then be seen by moving the pointer over the corresponding o on the root locus and clicking on it. While holding down the mouse button, you can then move the zero along the real axis to visualize the effect of different locations. Through trial and error we arrive at a location of 0.8 for the two zeros. In order to make the controller causal, we need to add an additional pole. Using a graphical approach again, we can add a real pole by choosing an x from the ROOT LOCUS EDITOR tab at the top of the Control System Designer window. Through trial and eror we arrive at a pole location of 0.6. The resulting root locus is shown below.

Control Tutorials for MATLAB and Simulink (9)

Next we must choose a loop gain in order to move the closed-loop pole locations along the root locus to the desired location. Since our closed-loop system is not canonical second order, we will have to employ some trial and error to identify the exact pole locations. In order to assist us in this regard, we can open a plot for the closed-loop step response so that we can observe the effect of the gain changes on the actual step response, without having to rely on a second-order idealization.

We can check the closed-loop step response for the system with this new gain by moving to the IOTransfer_r2y:step tab. If you have accidentally closed it then you can still open it.From the Control System Designer window, click on the New Plot menu and under Create New Plot, choose New Step, a new window titled New Step to plot will appear. Then from Select Responses to Plot menu choose IOTransfer_r2y. Then click on Plot. The closed-loop step response will then appear in the figure.

Control Tutorials for MATLAB and Simulink (10)

Control Tutorials for MATLAB and Simulink (11)

The resulting step response plot with loop gain of 1 appears to meet the settling time requirement, but not the overshoot requirement. In order to be certain, we can define the allowed step response shape by choosing Design Requirements from the right-click menu of the step response plot and choosing the overshoot to be 16% and the settling time to be 0.040 seconds. Note that you will need to choose a rise time of less than 0.040 seconds, even though we have no such requirement.

We will now modify the loop gain to meet the overshoot requirement. Using a graphical tuning approach, grab one of the pink boxes on the root locus plot by clicking on it, then drag the box along the locus. Each box represents the location of a closed-loop pole and dragging the box along the locus changes the loop gain to the value that places that closed-loop pole at that specified location. Through trial and error, a loop gain of 800 reduces the overshoot of the associated step response to approxomately 10% while achieving a settling time of approximately 0.02 seconds. Furthermore, the steady-state error goes to zero when there is no disturbance present. The resulting step response plot is shown below.

Control Tutorials for MATLAB and Simulink (12)

The only thing now left to verify is the system's disturbance rejection properties. A plot of the system's response to a step disturbance can be generated from the Control System Designer window. Instead, we will generate the disturbance response plot from the MATLAB command line. Before we do that, however, we must first export the compensator we have designed in the Control System Designer to the workspace. This is accomplished by clicking on Export button of CONTROL SYSTEM tab of the Control System Designer window. You then select Compensator C and click on the Export button. Once you have done this, then enter the following instructions in the command window to produce the response plot shown below.

dist_cl = feedback(dP_motor,C); [x2,t] = step(dist_cl,.25); stairs(t,x2) xlabel('Time (seconds)') ylabel('Position (rad)') title('Stairstep Disturbance Response with Compensation') grid 

Control Tutorials for MATLAB and Simulink (13)

Examination of the above shows that for a step disturbance, the steady-state error will still go to zero. Employing the property of superposition, it is also apparent that the total response will have overshoot less than 16% and will reach steady-state well within the 40 millisecond requirement.


Published with MATLAB® 9.2

Control Tutorials for MATLAB and Simulink (2024)

FAQs

How can I improve my MATLAB skills? ›

You ARE interested in improving your skills AT MATLAB.
  1. Read the tutorials. They seem reasonable enough.
  2. Find a project that interests you, and try to solve small problems in that area. If there is no project that interests you, then why are you bothering to learn MATLAB? ...
  3. Learn to use vectors. ...
  4. START WRITING CODE!
Feb 13, 2015

Can I learn MATLAB on my own? ›

MATLAB's official website provides comprehensive resources, including documentation, tutorials, and examples. The MATLAB documentation covers all aspects of the language and its various toolboxes. It's an excellent starting point for learning MATLAB from scratch.

How to design a control system in Simulink? ›

The details of each step in the design process are covered in later chapters.
  1. Defining a control design workflow.
  2. Linearizing a model.
  3. Finding system characteristics.
  4. Setting controller requirements.
  5. Tuning controllers.
  6. Testing controllers.

Is MATLAB Onramp free? ›

MATLAB Onramp is a free two-hour, self-paced, interactive course that allows new users to learn how to use MATLAB effectively. With MATLAB Onramp users gain confidence, become comfortable with the MATLAB environment, and gain the basic skills needed to use MATLAB.

Is MATLAB harder than Python? ›

The OOP in MATLAB is more advanced and complex, which to some can be more confusing. That being said, MATLAB is generally a more advanced language while Python is more of a beginner's language. Therefore, just because MATLAB may be more complex and confusing at first, with practice, it will become easier to grasp.

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 it better to learn Python or MATLAB? ›

So which language is better, Python or Matlab? In most cases, Python will be the better choice. The language is far more comprehensive, easier to learn and free. Matlab can be a better choice if you require the services of Simulink.

How difficult is MATLAB to learn? ›

MATLAB® creates an environment where issues and resolutions are expressed in familiar mathematical notation. MATLAB® is not hard to learn if you go for any professional course. It is ideal for engineering graduates and IT professionals willing to develop MATLAB® skills in their related fields.

How many days does it take 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 run C code in Simulink? ›

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.

What is Simulink control design? ›

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.

What are the two methods of control system design? ›

Open-loop and closed-loop control

Fundamentally, there are two types of control loop: open-loop control (feedforward), and closed-loop control (feedback). In open-loop control, the control action from the controller is independent of the "process output" (or "controlled process variable").

Where can I practice MATLAB for free? ›

Cody is a free community game where you solve MATLAB coding problems. It is a fun way to challenge your skills and learn MATLAB. Coding problems cover all skill levels, from beginner to advanced.

Is MATLAB certification worth it? ›

MATLAB certification establishes a standard of excellence that demonstrates your MATLAB proficiency to customers, industry peers, and your employer. Certification creates valuable transferable skills, sets individuals apart in the job market, and can help accelerate professional growth.

Can a student get MATLAB for free? ›

More than 2,200 schools have a Campus-Wide License, which provides unlimited use of MATLAB and Simulink to all students, faculty, staff, and researchers, on and off campus, on any device. To gain access to these tools, create a MathWorks Account using your university email address.

How long does it take to get good at 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.

What skills do you need to be a MATLAB developer? ›

To get started, one should know a basic object-oriented programming language and be aware of Matlab's syntax. Have a good knowledge of the workings of Simulink. One should also possess a good understanding of advanced mathematics.

Is MATLAB a valuable skill? ›

Beyond engineering, you can pursue several careers where MATLAB is a valuable skill. Research scientists, data scientists, data analysts, and machine learning professionals all benefit from having MATLAB as part of their repertoire. Along with the range of careers, MATLAB is making an impact across many industries.

How to improve MATLAB figure quality? ›

It is possible to increase the resolution of the plots obtained in MATLAB. On the Plot window ---- Click on <Edit> --> <Figure properties> --> <Export setup> --> <Rendering>. Then change the resolution to 300 or 600dpi. Then Click on Export and save as .

References

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 5494

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.