Control Tutorials for MATLAB and Simulink (2024)

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

Related Tutorial Links

  • Freq Resp Intro
  • Freq Resp Activity
  • Lead/Lag Freq Resp

Related External Links

Contents

  • Drawing the original Bode plot
  • Adding proportional gain
  • Plotting the closed-loop response
  • Adding a lag compensator

From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer function of the DC Motor are the following.

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

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

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

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

Control Tutorials for MATLAB and Simulink (4)

For the original problem setup and the derivation of the above equations, please refer to the DC Motor Speed: System Modeling page

For a 1-rad/sec step reference, the design criteria are the following.

  • Settling time less than 2 seconds
  • Overshoot less than 5%
  • Steady-state error less than 1%

Now let's design a controller using the methods introduced in the Introduction: Frequency Domain Methods for Controller Design page. Create a new m-file and type in the following commands.

J = 0.01;b = 0.1;K = 0.01;R = 1;L = 0.5;s = tf('s');P_motor = K/((J*s+b)*(L*s+R)+K^2);

Drawing the original Bode plot

The main idea of frequency-based design is to use the Bode plot of the open-loop transfer function to estimate the closed-loop response. Adding a controller to the system changes the open-loop Bode plot, thereby changing the closed-loop response. It is our goal to design the controller to shape the open-loop Bode plot in such a way that the closed-loop system behaves in a desired manner. Let's first draw the Bode plot for the original open-loop plant transfer function. Add the following code to the end of your m-file and run it in the MATLAB command window. You should generate the Bode plot shown below.

bode(P_motor)gridtitle('Bode Plot of the Original Plant')

Control Tutorials for MATLAB and Simulink (5)

Adding proportional gain

From the Bode plot above, it appears that the gain margin and phase margin of this system are currently infinite which indicates the system is robust and has minimal overshoot. The problem with this is that the phase margin is infinite because the magnitude plot is below 0 dB at all frequencies. This indicates that the system will have trouble tracking various reference signals without excessive error. Therefore, we would like to increase the gain of the system while still achieving enough phase margin.

A phase margin of 60 degrees is generally sufficient for stability margin. From the above Bode plot, this phase margin is achieved for a crossover frequency of approximately 10 rad/sec. The gain needed to raise the magnitude plot so that the gain crossover frequency occurs at 10 rad/sec appears to be approximately 40 dB. The exact phase and gain of the Bode plot at a given frequency can be determined by clicking on the graph at the corresponding frequency. The bode command, invoked with left-hand arguments, can also be used to provide the exact phase and magnitude at 10 rad/sec as shown below.

[mag,phase,w] = bode(P_motor,10)
mag = 0.0139phase = -123.6835w = 10

Therefore, the exact phase margin for a gain crossover frequency of 10 rad/sec is 180 - 123.7 = 56.3 degrees. Since the exact magnitude at this frequency is 20 log 0.0139 = -37.1 dB, 37.1 dB of gain must be added to the system. Otherwise stated, a proportional gain of 1/0.0139 = 72 will achieve an open-loop gain of 1 at 10 rad/sec. Add the following commands to your m-file to observe the effect of this proportional controller on the system. In this case, we use the margin command instead of the bode command in order to explicitly see the new gain and phase margins and crossover frequencies.

C = 72;margin(C*P_motor);

Control Tutorials for MATLAB and Simulink (6)

Plotting the closed-loop response

From the plot above we see that the resulting phase margin and gain crossover frequency are as we expected. Let's see what the closed-loop response look like. Add a % in front of the bode and margin commands to comment them out, then add the following code to the end of your m-file. Rerunning the m-file will produce the step response shown below where the annotations were added by right-clicking on the plot and choosing Characteristics from the resulting menu.

 sys_cl = feedback(C*P_motor,1); t = 0:0.01:10; step(sys_cl,t), grid title('Step Response with Proportional Gain = 72') 

Control Tutorials for MATLAB and Simulink (7)

Note that the settling time is fast enough, but the overshoot and the steady-state error are too high. The overshoot can be reduced by decreasing the gain in order to achieve a larger phase margin, but this would cause the steady-state error to become even larger. A lag compensator could be helpful here in that it can decrease the gain crossover frequency in order to increase the phase margin without decreasing the system's DC gain.

Adding a lag compensator

Consider the following lag compensator:

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

This lag compensator has a DC gain of 1/0.01 = 100 which means it will increase the system's static position error constant by a factor of 100 and will reduce the steady-state error associated with the system's closed-loop step response. In fact, it allows us to reduce the proportional gain of 72 used earlier, while still meeting the requirement on steady-state error. We will employ a gain of 45. Furthermore, since the corner frequencies of the pole and zero are a decade or more below the current gain crossover frequency of 10 rad/sec, the phase lag contributed by the compensator shouldn't adversely affect performance much. A Bode plot of the lag compensator can be generated employing the following commands.

C = 45*(s + 1)/(s + 0.01);bode(C)gridtitle('Bode Plot of the Lag Compensator')

Control Tutorials for MATLAB and Simulink (9)

The resulting step response can then be observed by modifying the code in your m-file as follows.

 sys_cl = feedback(C*P_motor,1); t = 0:0.01:10; step(sys_cl,t), grid title('Step Response with Lag Compensator') 

Control Tutorials for MATLAB and Simulink (10)

Inspection of the above demonstrates that all of the given requirements are now met when the lag compensator described above is employed.


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 good for control systems? ›

As a control systems engineer, you can use MATLAB® and Simulink® at all stages of development, including plant modeling, controller design, deployment with automatic code generation, and system verification.

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 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.

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 Python better than 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.

Is MATLAB a valuable skill? ›

Many industries and professions benefit from implementing this language to complete tasks ranging from algorithm development to data visualization, application development, and more, as MATLAB makes solving technical computing problems easier and more efficient than with other programming languages.

What are the weaknesses of MATLAB? ›

General Limitations
  • MATLAB Online cannot interact with most other hardware, including instrument control. ...
  • Windows-specific components like COM are not supported.
  • xlsread and xlswrite will work in basic mode.
  • Files larger than 256 MB cannot be uploaded on MATLAB Online.

How much faster is C++ than MATLAB? ›

However, one of the journal reviewrs is saying that the scripts based on Fortran and C++ language are usually running faster than any other computational languages, and according to the reference [1] shown as follows, the MATLAB is usually 9 to 11 times slower than the best C++ executable.

Where can I practice MATLAB for free? ›

Practice your introductory MATLAB skills with these simple coding problems. These problems are based on the skills you learn in MATLAB Onramp, a free, two-hour tutorial on introductory MATLAB features and workflows.

How difficult is MATLAB to learn? ›

MATLAB language is the first (and often only) programming language for many engineers and scientists because the matrix math and array orientation of the language makes it easy to learn and apply to engineering and scientific problem-solving.

Is MATLAB real coding? ›

MATLAB is a high-level programming language designed for engineers and scientists that expresses matrix and array mathematics directly.

How do I find Solver in MATLAB? ›

Select solvers in the Solver pane of model configuration parameters. All solvers provided by MATLAB® and Simulink follow a similar naming convention: ode , followed by two or three numerals indicating the orders of the solver.

How do I see 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 I find something in MATLAB code? ›

In MATLAB® Online™, to search text in the Command Window, use the Ctrl+F keyboard shortcut to open the find and replace dialog box.

What is the Find command in MATLAB? ›

Description. k = find( X ) returns a vector containing the linear indices of each nonzero element in array X . If X is a vector, then find returns a vector with the same orientation as X . If X is a multidimensional array, then find returns a column vector of the linear indices of the result.

Top Articles
Latest Posts
Article information

Author: Fredrick Kertzmann

Last Updated:

Views: 5847

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Fredrick Kertzmann

Birthday: 2000-04-29

Address: Apt. 203 613 Huels Gateway, Ralphtown, LA 40204

Phone: +2135150832870

Job: Regional Design Producer

Hobby: Nordic skating, Lacemaking, Mountain biking, Rowing, Gardening, Water sports, role-playing games

Introduction: My name is Fredrick Kertzmann, I am a gleaming, encouraging, inexpensive, thankful, tender, quaint, precious person who loves writing and wants to share my knowledge and understanding with you.