# Control Tutorials for MATLAB and Simulink (2023)

Key MATLAB commands used in this tutorial are: ss , order , det , ctrb , place , step

• State Space Intro
• LQR Animation 1
• LQR Animation 2

## Contents

• Designing the full state-feedback controller
• Disturbance response

From the main problem, the dynamic equations in state-space form are given below.

(1) (2) (Video) Control Design with MATLAB and Simulink

The above has the form of a standard set of state-space equations as described below.

(3) (4) For the original problem setup and the derivation of the above equations, please refer to the DC Motor Position: System Modeling page

With a 1-radian step reference, the design criteria are the following.

• Settling time less than 0.040 seconds
• Overshoot less than 16%
• No steady-state error, even in the presence of a step disturbance input

First create a new m-file and type in the following commands (refer to main problem for the details of getting these commands).

`J = 3.2284E-6;b = 3.5077E-6;K = 0.0274;R = 4;L = 2.75E-6;A = [0 1 0 0 -b/J K/J 0 -K/L -R/L];B = [0 ; 0 ; 1/L];C = [1 0 0];D = 0;motor_ss = ss(A,B,C,D);`

## Designing the full state-feedback controller

Since all of the state variables in our problem are very easy to measure (simply add an ammeter for current, a tachometer for speed, and a potentiometer for position), we can design a full-state feedback controller for the system without worrying about having to add an observer. The control law for a full-state feedback system has the form . The associated block diagram is given below. (Video) Control Tutorials for MATLAB and Simulink

Recall that the characteristic polynomial for this closed-loop system is the determinant of where is the Laplace variable. Since the matrices and are both 3x3 matrices, there should be 3 poles for the system. This fact can be verified with the MATLAB command order. If the given system is controllable, then by designing a full state-feedback controller we can move these three poles anywhere we'd like. Whether the given system is controllable or not can be determined by checking the rank of the controllability matrix . The MATLAB command ctrb constructs the controllability matrix given and . Additionally, the command rank determines the rank of a given matrix, though it can be numerically unreliable. Therefore, we will use the command det to calculate the determinant of the controllability matrix where a full rank matrix has a non-zero determinant. The following commands executed at the command line will verify the system's order and whether or not it is controllable.

`sys_order = order(motor_ss)determinant = det(ctrb(A,B))`
`sys_order = 3determinant = -3.4636e+24`

From the above, we know that our system is controllable since the determinant of the controllability matrix is not zero and hence we can place the system's closed-loop poles anywhere in the s-plane. We will first place the poles at -200, -100+100i and -100-100i. By ignoring the effect of the first pole (since it is faster than the other two poles), the dominant poles correspond to a second-order system with = 0.5 corresponding to 16% overshoot and = 100 which corresponds to a settling time of 0.040 seconds. Once we have determined the pole locations we desire, we can use the MATLAB commands place or acker to determine the controller gain matrix, , to achieve these poles. We will use the command place since it is numerically better conditioned than acker. However, if we wished to place a pole with multiplicity greater than the rank of the matrix , then we would have to use the command acker. Add the following code to the end of your m-file.

`p1 = -100+100i;p2 = -100-100i;p3 = -200;Kc = place(A,B,[p1, p2, p3])`
`Kc = 0.0013 -0.0274 -3.9989`

Referring back to the equations and schematic at the top of the page, we see that employing a state-feedback law , the state-space equations become the following.

(5) (6) We can generate the closed-loop response to a step reference by adding the following lines to the end of your m-file. Run your m-file in the command window and you should generate a plot like the one shown below.

`t = 0:0.001:0.05;sys_cl = ss(A-B*Kc,B,C,D);step(sys_cl,t)` Note that our given requirements are not met, specifically, the steady-state error is much too large. Before we address this, let's first look at the system's disturbance response.

## Disturbance response

In order to observe the system's disturbance response, we must provide the proper input to the system. In this case, a disturbance is physically a load torque that acts on the inertia of the motor. This load torque acts as an additive term in the second state equation (which gets divided by , as do all the other terms in this equation). We can simulate this simply by modifying our closed-loop input matrix, , to have a in the second row assuming that our current input is only the disturbance.

(Video) Getting Started with Simulink for Controls

`dist_cl = ss(A-B*Kc,[0; 1/J ; 0], C, D);step(dist_cl,t)` Notice that the error due to the step disturbance is non-zero. Therefore, this will also need to be compensated for.

From prior examples, we know that if we put an extra integrator in series with the plant it can remove the steady-state error due to a step reference. If the integrator comes before the injection of the disturbance, it will also cancel a step disturbance input in steady state. This changes our control structure so that it now resembles the block diagram shown in the following figure. We can model the addition of this integrator by augmenting our state equations with an extra state for the integral of the error which we will identify with the variable . This adds an extra state equation, where the derivative of this state is then just the error, where . This equation will be placed at the bottom of our matrices. The reference , therefore, now appears as an additional input to our system. The output of the system remains the same.

(7) (8) (9) (Video) Getting Started with Simulink, Part 1: How to Build and Simulate a Simple Simulink Model

These equations represent the dynamics of the system before the loop is closed. We will refer to the system matrices in this equation that are augmented with the additional integrator state as , , , and . The vector multiplying the reference input will be referred to as . We will refer to the state vector of the augmented system as . Note that the reference, , does not affect the states (except the integrator state) or the output of the plant. This is expected since there is no path from the reference to the plant input, , without implementing the state-feedback gain matrix .

In order to find the closed-loop equations, we have to look at how the input, , affects the plant. In this case, it affects the system in exactly the same manner as in the unaugmented equations except now . We can also rewrite this in terms of our augmented state as where . Substituting this into the equations above provides the following closed-loop equations.

(10) (11) In the above, the integral of the error will be fed back, and will result in the steady-state error being reduced to zero. Now we must redesign our controller to account for the augmented state vector. Since we need to place each pole of the system, we will place the pole associated with the additional integrator state at -300, which will be faster than the other poles.

Add the following lines to your m-file which reflect the closed-loop equations presented above. Note that since the closed-loop transition matrix depends on , it will be used in the place command rather than . Running your m-file will then produce the plot shown below.

`Aa = [0 1 0 0 0 -b/J K/J 0 0 -K/L -R/L 0 1 0 0 0];Ba = [0 ; 0 ; 1/L ; 0 ];Br = [0 ; 0 ; 0; -1];Ca = [1 0 0 0];Da = ;p4 = -300;Ka = place(Aa,Ba,[p1,p2,p3,p4]);t = 0:0.001:.05;sys_cl = ss(Aa-Ba*Ka,Br,Ca,Da);step(sys_cl,t)` To observe the disturbance response, we use a similar approach to that used without the integral action.

`dist_cl = ss(Aa-Ba*Ka,[0 ; 1/J ; 0; 0],Ca,Da);step(dist_cl,t)` (Video) MATLAB - Simulink Tutorial for Beginners | Udemy instructor, Dr. Ryan Ahmed

We can see that all of the design specifications are close to being met by this controller. The settle time may be a little large, but by placing the closed-loop poles a little farther to the left in the complex s-plane, this requirement can also be met.

Published with MATLAB® 9.2

## FAQs

### What is the MATLAB 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.

How is MATLAB used in control systems? ›

MATLAB and Simulink for Control Systems

Plant modeling using system identification or physical modeling tools. Prebuilt functions and interactive tools for analyzing overshoot, rise time, phase margin, gain margin, and other performance and stability characteristics in time and frequency domains.

What is Simulink in control system? ›

Simulink Control Design provides tools that let you compute simulation-based frequency responses without modifying your model.

How to use control system tuner in Simulink? ›

In the Simulink model window, under Control Systems in the Apps tab, select Control System Tuner. In Control System Tuner, on the Tuning tab, click Select Blocks. Use the Select tuned blocks dialog box to specify the blocks to tune. Click Add Blocks.

The Simulink approach is based on time based and multi rate system. SO that will be useful for HDL code generation. Whereas, MATLAB is for the mathematical based algorithm development and which will not consider the time while in simulation (independent of time). Simulink is graphical and more interactive to the user.

What is MPPT in MATLAB Simulink? ›

Maximum power point tracking (MPPT) is an algorithm implemented in photovoltaic (PV) inverters to continuously adjust the impedance seen by the solar array to keep the PV system operating at, or close to, the peak power point of the PV panel under varying conditions, like changing solar irradiance, temperature, and ...

What are five applications of using MATLAB? ›

Applications
• General Applications. Example models illustrating general applications.
• Automotive Applications. Model and simulate automotive systems using Simulink® and other MathWorks® products.
• Aerospace Applications. ...
• Industrial Automation Applications. ...
• Signal Processing. ...
• Control Design. ...
• Physical Modeling. ...
• Complex Logic.

What are three applications of MATLAB? ›

Millions of engineers and scientists worldwide use MATLAB for a range of applications, in industry and academia, including deep learning and machine learning, signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology.

What is PID control in MATLAB? ›

PID control respectively stands for proportional, integral and derivative control, and is the most commonly used control technique in industry.

What is Simulink in simple words? ›

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.

### What is Simulink a tool for? ›

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

What is Simulink good for? ›

Simulink, which is created by MathWorks, is one of the most dynamic and resourceful applications. It is basically a simulation platform that incorporates MATLAB and a model design system. It features a fantastic environment for programming, simulation, and modelling.

How do you control a switch in Simulink? ›

To toggle between inputs, double-click the block. You control the signal flow by setting the switch before you start the simulation or by changing the switch while the simulation is executing. The Manual Switch block retains its current state when you save the model.

How to control a stepper motor with Simulink? ›

The model provides two controller options: one to control position and one to control speed. To change the controller type, right-click on the Controller block, select Variant->Override using-> and select Position or Speed.

Simulink isn't the way to go for small projects with a low budget: a MATLAB license is rather expensive: like the additional packages that may be required for testing on dedicated hardware. It is suited for big projects with a large number of developers working on them.

What is the alternative for MATLAB Simulink? ›

Top 10 Alternatives & Competitors to Simulink
• Scilab. (63)4.5 out of 5.
• GNU Octave. (55)4.2 out of 5.
• NI Multisim. (41)4.3 out of 5.
• Simcenter Amesim. (36)4.4 out of 5.
• COMSOL Multiphysics (formerly FEMLAB) (35)4.3 out of 5.
• Fusion 360. (407)4.5 out of 5.
• Ansys Fluent. (95)4.5 out of 5.
• PSIM. (26)4.7 out of 5.

What is the Python equivalent of MATLAB Simulink? ›

BMS is designed as a lightweight, fully scriptable, open-source equivalent to simulink in python.

What is the difference between Stateflow and Simulink in MATLAB? ›

In most cases, Stateflow is less efficient with regards to RAM. Therefore, Simulink has an advantage in computations that use simple formulas. In addition, Simulink is more advantageous for situations where state variables are operated with simple flip-flops and the Relay block.

Which MPPT algorithm is best? ›

In summary the best algorithms are those designed using the SASV-MPPT approach and Lyapunov de- sign method considering that the PV system can move from one characteristic to another. The proposed al- gorithms are the most efficient despite using low fre- quency commutation. They are the faster converging.

How to use breaker in MATLAB Simulink? ›

When the Breaker block is set in external control mode, a Simulink input appears on the block icon. The control signal connected to the Simulink input must be either 0 , which opens the breaker, or any positive value, which closes the breaker.

### What is the disadvantage of MATLAB? ›

MATLAB is interpreted language and hence it takes more time to execute than other compiled languages such as C, C++. ➨It is expensive than regular C or Fortran compiler. Individuals find it expensive to purchase. ➨It requires fast computer with sufficient amount of memory.

Is MATLAB used in NASA? ›

The Space Launch System (SLS) rocket is designed to carry humans into deep space. Using MATLAB® and Simulink® for simulation and validation, the complex mission management logic is designed to ensure that the SLS can correctly respond to nominal and off-nominal events.

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

Which engineer uses MATLAB? ›

Mechanical engineers of Design and manufacturing field use MATLAB and Simulink heavily. You would be surprised to know that MATLAB also forms the based for different CAD software as well as designing software just like SOLIDWORKS.

What are the 3 most important windows in MATLAB? ›

There are several important windows that you use in Matlab: the Command Window, the Command History, the Current Directory, and the Editor. To see these three windows at once (or suppress them later), click on the Desktop in the Matlab tool bar.

How to create 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 write PI controller in Matlab? ›

C = pid( Kp ) creates a continuous-time proportional (P) controller. C = pid( Kp , Ki ) creates a proportional and integral (PI) controller. C = pid( Kp , Ki , Kd ) creates a proportional, integral, and derivative (PID) controller.

What is PID vs MPC controller? ›

In comparison with the PID controller, the MPC controller's one notable distinction is that it is capable of managing diverse constraints. Furthermore, the MPC controller demands a process model, while the PID controller does not.

NASA Marshall Engineers have developed an ADCS Simulink simulation to be used as a component for the flight software of a satellite.

What is the best way to learn MATLAB? ›

Best MATLAB Courses
1. Become a Good Matlab Programmer in 30 Days (Udemy)
2. Master MATLAB Through Guided Problem Solving (Udemy)
3. Introduction to Programming with MATLAB (Coursera)
5. MATLAB Master Class: Go from Beginner to Expert in MATLAB (Udemy)
6. MATLAB and Octave for Beginners (edX)
Mar 6, 2023

### What coding language is Simulink? ›

Simulink is a MATLAB-based graphical programming environment for modeling, simulating and analyzing multidomain dynamical systems. Its primary interface is a graphical block diagramming tool and a customizable set of block libraries.

To meet aggressive technology goals on a strict budget and timeline, the Tesla Motors design team relied on Simulink and MATLAB to model the entire vehicle and its major subsystems.

MATLAB and SIMULINK are used worldwide in industries as well as research institutes for developing up-to-date or technologically advanced solutions to many engineering, science and research problems.

You can set up your system to use Python with Simulink and use the MATLAB Function block or the MATLAB System block to integrate Python code with Simulink.

Do electrical engineers use Simulink? ›

Develop, simulate, and test electronics systems and devices

Engineers use MATLAB® and Simulink® product families to design and simulate signal and image processing systems and control systems by capturing algorithms and system models. Using MATLAB and Simulink you can: Analyze signals and explore algorithms.

Is Simulink used in industry? ›

Industrial Automation and Machinery engineers use Model-Based Design in MATLAB and Simulink to: Design and test machine controls and supervisory logic. Run automatic tests on equipment functions. Design artificial intelligence (AI) algorithms for predictive maintenance and operations optimization.

Where is Simulink used in industry? ›

Simulink® enables industrial equipment makers to create executable specifications in the form of models that provide clear design direction to diverse engineering groups. These example models illustrate industrial automation applications.

What is the use of Simulink control design toolbox? ›

Simulink Control Design provides tools that let you compute simulation-based frequency responses without modifying your model. You can deploy prebuilt control algorithms for online frequency response estimation, PID tuning, and adaptive control on hardware for control design and analysis.

What is Simulink Model-Based Design? ›

Model-Based Design allows you to: Use a common design environment across project teams. Link designs directly to requirements. Identify and correct errors continuously by integrating testing with design.

What is meant by MATLAB 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.

### What is control flow structure 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 ).

What is PID controller in Simulink? ›

PID control respectively stands for proportional, integral and derivative control, and is the most commonly used control technique in industry.

What type of programming language is Simulink? ›

Yes Simulink is a graphical programming language because, the source code itself is graphical in nature, though text can be included.

Which is better MATLAB or Simulink? ›

Simulink has Graphical User Interface (GUI) whereas Matlab is just code. Simulink's GUI lends it more intuitiveness. Simulink supports hardware communication. You can write a PID controller in Simulink and download it to Arduino.

Is MATLAB different from MATLAB Simulink? ›

Simulink is a graphical programming environment that allows you to create and simulate dynamic systems using blocks and connections. MATLAB is a numerical computing language that enables you to perform calculations, data analysis, and scripting.

What are the three basic control structures in flowchart? ›

The Three Basic Control Structures. Our programs are made up of the three basic constructures of: sequence, selection, and repetition.

What are the three types of control structures explain? ›

There are 3 main control structures in programming: Sequence, Selection and Repetition. Sequential control follows a top to bottom flow in executing a program, such that step 1 is first perform, followed by step 2, all the way until the last step is performed.

What are the three basic control flow structures in programming? ›

Flow of control through any given function is implemented with three basic types of control structures:
• Sequential: default mode. ...
• Selection: used for decisions, branching -- choosing between 2 or more alternative paths. ...
• Repetition: used for looping, i.e. repeating a piece of code multiple times in a row.

## Videos

1. Control System Design with MATLAB and Simulink
(MATLAB)
2. Control System Modeling with MATLAB & Simulink
(Akhilesh Kumar)
3. Getting Started with Simulink, Part 2: How to Add a Controller and Plant to the Simulink Model
(MATLAB)
4. Intro to Control - MP.1 Feedback Control in Matlab Simulink
(katkimshow)
5. Multivariable (MIMO) Control Fundamentals: MATLAB & Simulink Tutorial
(VDEngineering)
6. What Is Simulink Control Design?
(MATLAB)
Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated: 28/10/2023

Views: 6271

Rating: 4.1 / 5 (72 voted)

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376