Control Tutorials for MATLAB and Simulink (2024)

Contents

  • Block parameters from MATLAB variables
  • Exchanging signals with MATLAB
  • Extracting models from Simulink into MATLAB

In this page we will examine three of the ways in which Simulink can interact with MATLAB.

  • Block parameters can be defined from MATLAB variables.
  • Signals can be exchanged between Simulink and MATLAB.
  • Entire systems can be extracted from Simulink into MATLAB.

Block parameters from MATLAB variables

Often a controller will be designed in MATLAB and verified within a Simulink model. Commonly, numerical parameters such as gains and controller transfer functions are entered into Simulink manually by entering the corresponding numbers into the block dialog boxes. Rather than enter numbers directly, it is also possible to use MATLAB variables in the Simulink block dialog boxes.

Consider, for example, the Simulink model from the Simulink Basics tutorial which is shown in the figure below. You can recreate this model yourself, or you can download it by right-clicking here and then selecting Save link as ....

Control Tutorials for MATLAB and Simulink (1)

In this case, the complete controller transfer function is the following.

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

Suppose this transfer function were generated by some computation in MATLAB. In this case, there would most likely be three variables, the numerator polynomial, the denominator polynomial, and the gain. Enter the following commands in MATLAB to define these variables.

K = 2.5;num = [1 2];den = [1 0];

These variables can now be used within blocks in the Simulink environment. In your Simulink model, double-click on the Gain block and enter "K" in the Gain field as shown below.

Control Tutorials for MATLAB and Simulink (3)

Now close this dialog box. Notice that the Gain block in the Simulink model now shows the variable K rather than a number.

Control Tutorials for MATLAB and Simulink (4)

Next, double-click on the PI Controller block. Enter "num" into the Numerator Coefficient field and "den" into the Denominator Coefficient field as shown below.

Control Tutorials for MATLAB and Simulink (5)

Close this dialog box. Notice now that the PI Controller block shows the variables num and den (as functions of s) rather than an explicit numerical transfer function.

Control Tutorials for MATLAB and Simulink (6)

You can then simulate the model with the MATLAB variable parameters. Select Run from the Simulation menu to run the simulation. Double-click on the Scope block to view its output and you should see the following.

Control Tutorials for MATLAB and Simulink (7)

Now, if any calculations are done in MATLAB to change any of the variables used in the Simulink model, the simulation will use the new values the next time it is run. To try this, change the gain K within MATLAB by entering the following at the command prompt.

K=5;

Start the simulation again, bring up the Scope window, and hit the autoscale button. You will see the following output which reflects the new, higher gain.

Control Tutorials for MATLAB and Simulink (8)

To download the Simulink model with MATLAB variable parameters, right-click here and then select Save link as ....

Exchanging signals with MATLAB

Sometimes, we would like to use the results of a Simulink simulation in the MATLAB command window for further calculations and plotting. Less often, we would like to generate signals in MATLAB which we can then use as inputs in a Simulink model. These tasks are accomplished through the use of the To Workspace block from the Sinks library and the From Workspace block found in the Sources library. We will only transfer signals from Simulink to MATLAB. Doing the reverse is a very similar process.

The To Workspace block saves a signal as a vector (or structure) in the MATLAB workspace. Open the model which you used previously in this tutorial or click here to download the model. Be sure that the variables K = 5, num = [1 2], and den = [1 0] are defined in the MATLAB workspace.

We would like to use both the output signal y and the control signal u for calculations in MATLAB. We will save these two variables as well as a time signal from our Simulink model. First, we need to generate an explicit time signal. Open the Sources library by double-clicking the Sources icon in the main Simulink Library Browser window. Drag the Clock block from the Sources window to the lower portion of your Simulink model as shown below.

Control Tutorials for MATLAB and Simulink (9)

Now, open the Sinks library and drag three instances of the To Workspace block into your Simulink window, arranged approximately as shown below.

Control Tutorials for MATLAB and Simulink (10)

Before connecting these blocks to the rest of your system, name the variables where the data is stored in the MATLAB workspace. The lower To Workspace block will output the time signal, hence we will store this data in the MATLAB variable t. Double-click on this block and enter "t" in the Variable Name field as shown below. Also change the entry in the Save format field from the default of Structure to Array.

Control Tutorials for MATLAB and Simulink (11)

Now close the dialog box. Notice that the lower To Workspace block displays a t.

Control Tutorials for MATLAB and Simulink (12)

The To Workspace block near the Plant block will output the control signal to a MATLAB variable we've named u. Again define the variable name for this block by entering "u" into the the Variable Name field and choose Array from the drop-down menu in the Save format field. The last To Workspace block will output the output signal to the MATLAB variable y by repeating the steps above. Also, for better clarity, change the labels (by clicking on the existing labels "To Workspace") of these blocks to "time", "control", and "output". Your model should appear as follows.

Control Tutorials for MATLAB and Simulink (13)

Now, you will connect these blocks to the rest of your system. Draw a line from the Clock block to the time (t) block. Tap a line off of the control signal (the line between the PI Controller block and the Plant block) and connect it to the control (u) block. Remember, to tap off an existing line, hold the Ctrl key while drawing the line. You can also tap off a line by right-clicking. Tap a line off the output signal line (the line which enters the Scope block) and connect it to the output (y) block. Your system should appear as follows.

Control Tutorials for MATLAB and Simulink (14)

Start the simulation (Run from the Simulation menu). You can still view the output in the Scope window.

Control Tutorials for MATLAB and Simulink (15)

You can now examine the variables saved to the MATLAB workspace. Plot u and y vs. t by entering the following commands.

 plot(t,u,t,y); 

Note that it is important to plot each of these variables against the time vector generated by Simulink. This is because the time between elements in the signal vectors may be unequal if a variable-step solver is employed. This is particularly the case near a discontinuity such as the step input (anywhere the signals are changing rapidly). Your plot of u (blue) and y (red) should appear as follows.

Control Tutorials for MATLAB and Simulink (16)

To download the model with outputs to MATLAB variables, right-click here and then select Save link as ....

Instead of employing To Workspace blocks, you can also output data to the MATLAB workspace using your Scope block. Specifically, if you double-click on your Scope and select the Configuration Properties in View tab, you will open the following dialog box. Under the Logging tab, you can select the box Log data to workspace. Here you can also define the Format of the data and the Variable name to which the data is saved. In this case, the data will be output as a matrix with the first column corresponding to simulation time, and subsequent columns corresponding to the signals being fed to the Scope block.

Control Tutorials for MATLAB and Simulink (17)

Furthermore, you do not always need to define a clock to generate the simulation time vector. By default, Simulink will ouput a vector tout to the MATLAB workspace that includes the simulation time. This vector, however, is limited to a size of 1000 elements.

Extracting models from Simulink into MATLAB

It is also sometimes useful to extract a simulation model from within Simulink into the MATLAB workspace. This is especially true for situations where your simulation model is complicated or nonlinear. This extraction can be accomplished with the MATLAB command linmod, or from within the Simulation model itself as we will demonstrate here.

We will employ the Simulink model with variables defined above (download by right-clicking here and then selecting Save link as ...). First, choose from the menus at the top of the model window Analysis > Control Design > Linear Analysis. This will cause the Linear Analysis Tool window to open. In order to perform our extraction/linearization, we need to identify the inputs and outputs for the model and the operating point that we wish to perform the linearization about. We will extract the closed-loop model from the reference r to the output y. Right-click on the signal representing the reference r in the Simulink model. Then choose Linear Analysis Points > Open-loop Input from the resulting menu. Similarly, right-click on the output signal of the model (y) and select Linear Analysis Points > Output Measurement from the resulting menu. The resulting input and output should now be identified on your model by arrow symbols as shown in the figure below.

Control Tutorials for MATLAB and Simulink (18)

Next we need to identify the operating point to be linearized about. Since our model is already linear, any choice will produce the same output. Therefore, we can leave the Operating Point field in the Linear Analysis Tool window as its default, Model Initial Condition. The Linear Analysis Tool window should appear as follows.

Control Tutorials for MATLAB and Simulink (19)

  • Finally, click the Step button indicated by a small step response with a small green triangle. This automatically generates a step response plot and the model linsys1 as shown below.

Control Tutorials for MATLAB and Simulink (20)

The above step response exactly matches the results achieved from running the simulation because the Simulink model was already linear. We can then export the resulting linearized model into the MATLAB workspace for further analysis and design. This can be accomplished by simply dragging the linsys1 object from the Linear Analysis Workspace to the MATLAB Workspace.


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? ›

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.

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? ›

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.

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.

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 difficult is MATLAB to learn? ›

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.

Is MATLAB real coding? ›

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

Should I learn MATLAB without coding experience? ›

It is preferable to have a little basics of programming to learn and understand MATLAB, as it more or less does what a programming language like C, C++ does, but in a more user friendly way. One can easily pick up MATLAB once basics of programming are known,thats what my exposure to MATLAB says!. How to learn MATLAB?

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.

References

Top Articles
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 6330

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.