Introduction to Simulink for System Modeling and Simulation
Overview
In this session you will learn the basics of Simulink for modeling, simulating, and analyzing multidomain dynamical systems. You will see how to build simulation models using Simulink’s block diagramming interface, customizable set of libraries, and connectivity to MATLAB.
Highlights
- Introduction to Simulink
- Modeling and simulating a pump with electric, mechanical, and fluid flow features
- Designing and simulating closed loop PID and supervisory control algorithms for the pump
- Viewing simulation results
- Examples of different Simulink applications
- How to get started with Simulink
About the Presenters
Ed Marquez is a Simulink Product Manager at MathWorks. He supports automated report generation and Model-Based Design applications. Ed is experienced in automotive system modeling and controls. He holds a MS and BS in Mechanical Engineering from Virginia Tech.
Karanjodh Singh Meen has a MS in Mechanical Engineering from Arizona State University and BE form Punjab Engineering College, India. He specializes in the area of control systems, modeling of physical systems and robotics. He has experience working with drones and Automobiles. In 2018, he joined Technical Support at MathWorks and later moved to the Application Engineering Group in 2019.
Recorded: 24 Jan 2023
Hello, everyone. Thank you for joining us for this introductory session on system modeling and simulation with Simulink. To start our conversation, let's consider a medical device. And as you design and engineer devices like this one, how do you ensure that it delivers the amount of medicine that the user intends?
And how do you design and test its control algorithm before building an expensive hardware prototype? Throughout this session, we'll show you the model of an infusion pump. And you will see that Simulink and also modeling and simulation can help us answer these questions.
My name is Ed Marquez. And I'm a product manager for Simulink at MathWorks. Today, I have Karan with me. Hi, Karan.
Hey, Ed. How are you?
Doing great. How are you?
I'm doing good.
Excellent. Can you tell us a little bit more about yourself?
Yes. So my name Karanjodh Singh. And I'm an application engineer at MathWorks. And I support Simulink. So actually, I have a Simulink model prepared for the infusion pump. So why don't we jump right into the model?
That sounds great.
So today, we will show you how you can design and develop algorithms using Simulink. But first of all, let's go ahead and simulate the system. So this is a model of an infusion pump. Now, infusion pump is a device that is usually operated by a nurse. And the goal of this device is to automatically deliver medicine to the patient at regular intervals.
So basically, what we have in the Simulink model is on the left-hand side, you have the input commands provided by the nurse. You have the control algorithm. This is what we are designing and developing. And then you have the plant model, which basically represents a model of the physical hardware that you have. And this is what we are trying to control.
Now, as you can see, the simulation is already running in the background. And it has been running for a few seconds. Now, let's go ahead and visualize the results of the simulation.
Now, what are we looking at right now? So this graph represents the pressure in the delivery line. So as you can see, as the medicine is delivered to the patient, there is a sudden spike in pressure. And then it goes back down to some base value. And this goes on for a little bit. But at the end, something weird starts to happen where the pressure just keeps on building up.
Now, this really goes on to show the real power of simulation and Simulink, where you can build and simulate your algorithms and see how they behave. And using these analyses and these results, you can fine tune our system and even correct some of the design errors which otherwise you would have found in expensive prototypes that you have built. But before we go into details of how we can design the systems and probably fix errors like this, let's take a step back and look at the agenda that we have for today.
So today, we are going to start by getting an overview of Simulink. Then we will move on to look into some example models that people built and some additional examples that we will show. And finally, we will look at some applications that use Simulink. And after that, we will share resources and other ways that you can either reach out to us, or you can learn more about the product.
So Ed, why don't you go ahead and tell us a little bit more about Simulink and MATLAB?
Absolutely. So I'll give you a quick overview of MATLAB and Simulink because we at MathWorks created these two platforms to help engineers and scientists make their work easier. And perhaps you're already familiar with MATLAB. If not, MATLAB is a programming environment for algorithm development, data analysis, visualization, and also numeric computations.
Simulink, which is our focus for today, is a multi-domain block diagram environment that allows engineers to design, simulate, and test dynamic systems before moving to hand-coding or before moving to hardware prototypes. And we at MathWorks also produce more than 100 add-ons that you can use on top of MATLAB and Simulink for specialized tasks. And those could be tasks for image processing, computer vision, wireless communications, and many more. And we'll tell you more about those applications later.
But with that said, let's jump into the tool. And let's build. And look at some demos in Simulink. So the way to get to Simulink is from the MATLAB interface. And I have a couple of ways to get there. I can type Simulink in the command window. Or I can simply click on this icon in the MATLAB tool strip.
And what you see here is that I have a start page for Simulink, where on the left pane, I have recent models that have worked with, recent projects I've worked on. And most importantly, I have a few ways that I can learn the product. And we call these the on-ramps. And I'll talk more about these later in my presentation.
Now, here in the New tab, I can start a new model from a blank template, or I can also use other templates. And you saw that we also have Example tabs, but I'll cover that in more detail later on. So the model that I want to build today is a simple motor. And I know that a motor is an electromechanical system. So let's see if I type mechanical here, OK, I have two hints here for mechanical rotational systems. And that's what I want to start with.
So you see that with the template I get a few blocks for free that I'll already put in there. And let me give you first a tour of the interface.
Ed, I have a question here. So you started the template, but it already has some blocks in it. Now, if I start with some other template, can I access these blocks in a different template?
Absolutely. You can access any block in Simulink from the Library browser, OK? And that's basically the collection of libraries where I can find all the elements to build my models, OK? I can find blocks for Simulink and libraries for Simulink. But then I can also find blocks for Simscape, which is an add-on that we'll be using to build a physical model of our motor.
But before I get to this, let me give you a tour of the interface. Here, I have the canvas, which is all the blank area where I can create my model. And up here, I have the tool strip, which gives me features and capabilities by workflows. You see this is organized by with a Simulation tab. I have capabilities for debugging models. I have more capabilities for modeling.
Here, I can change the format of my model. So I can change the background color. Undo that with Control C. I can format my blocks. And here, I also have apps which are user interfaces that help me work at higher levels of abstraction.
Now, Ed, I noticed when you selected that block, an additional menu pops up. Can you talk a little bit more about that?
That is a great question. And what happened there is when I select a block or a model element, the tool strip shows me capabilities in the context of the work that I'm doing. So you see that here I get specific capabilities for that block that I selected. And if I grab multiple blocks, then that changes to give me actions that are available for those blocks or multiple blocks that I selected.
All right, so now, let's go ahead and build our motor model. If we go back to the Library browser, let's go ahead and try to find some blocks. But actually, I did forget a step. Let me show you where we're trying to go. And for that, I'm going to use an image to show you the kind of system that we're trying to build.
I said a motor, and a motor is just a voltage source, the motor itself and then a mechanical part for it. So again, if I go back to the Library browser, in the Electrical library under Foundations, I can go to Electrical Sources and grab a controlled voltage source. So I can just drag and drop that. And I can rotate this block with Control R with the keyboard. Or in the tool strip, I get a lot of format options for that block.
So Ed, I'm wondering if I make a very large model, which has thousands of blocks in it, the process of going to the Library browser and adding a block from there could get time consuming. Is there another way of adding blocks to the canvas?
That's a great question. So to make your point, I could try to find the next block that I'm trying to insert here. And I would have to go through these libraries. So I believe the next block I want is under these electromechanical systems. I want a DC motor. And you see that I would have to find those block elements before I add them to a model. But to your point, what I can do as well is use quick insert capabilities that Simulink gives me to insert blocks in the model.
So for example, by creating that connection, and double-clicking on that node, Simulink gives me cues on what is the next block that I can connect to this signal or to this connection. So here, that's exactly what I want. I want an electrical reference, which is the ground for the electrical side of my motor. And I can also connect the voltage source to that ground. OK?
Now, if we continue building the electrical side of my motor, I can show you the other way to insert blocks. And that is just by clicking on the canvas. If I know the block name, I can just start typing that block name. So in this case, I know I want a torsional spring damper. And Simulink also tells me where that's coming from. It's coming from the driveline/couplings and drives. So I can capture that. Let me try that again. Torsional spring damper.
So Ed, I'm noticing that these blocks have different colors. Is there some additional meaning associated with that?
That's a good question. And those are the different domains that I'm modeling here for my components. So you see that the blue side represents the electrical domain, and the green side is the mechanical domain. And I could have many more to model a multi-domain system.
So now, here, I can rotate or invert these components with the tool strip and make a connection. So the connection I'm making here is with a mechanical rotational reference. And this is sort of the ground for the rotational domain or for the mechanical domain.
OK, so I can make this other connection here. In this case, I might want to connect to a connection port if I want to take that somewhere else. And one important thing we also need to do is we need to parameterize our block elements. What I mean by that is that when I double-click on a block, I get a brief description of how the block works. But I also get, in addition to that, parameters that I can enter to define the behavior of the component or of the block. In this case, I'll start entering these variables that I have already defined in the MATLAB workspace.
So Simulink is smart enough to figure out that these variables, like R, L, EMF, are already there in the MATLAB workspace. And when you run the simulations, it's just going to pick up those variables from the MATLAB workspace and use those variables.
That is exactly right. So you see I completed the variables for the motor. And I can do the same for the torsional spring damper. And I know what these values are. So I can just enter them here relatively quickly.
All right, so now, I could test this model and try and simulate it. And you will see that it will run, but I have really no way to get to the results because I haven't set that up or made those connections yet. So let's see how we get that.
And since we're working with physical components, I will need sensors to get to those values. So by branching the signal here, and I did that by hitting Control and then dragging the signal, and I can do the same by right-clicking and dragging, now I can connect this to a sensor that allows me to get to the values that are going through this connection. OK? So if I just type sensor, this is the one I want. I want an ideal rotational motion sensor. And then I can rotate this block.
And I could double-click, get a brief description of how the block works. But I do know that the signal I want is the angular speed from that sensor. So I took that W output, connected to this scope, and I know we will also need another mechanical rotational reference, so just another ground here that I would connect to my sensor. And we should be able now to simulate. But at the same time, you might notice here that I still need something to drive this model.
So let's just use a step input. And that's coming from the Sources library in Simulink. And so if I double-click on that block, I can say that at the step time of one, I want a final value of 100. OK? And now, we should be able to simulate this network. So let's see.
So in the scope, I'm seeing the response of the motor. But you see that these values are going really high, 2 times 10 to the fourth. And let's see how we can correct that. And by the way, I can also visualize the motor speed next to the reference input that I'm giving it by just dragging that signal and connecting that to the scope.
OK. But now, what we can do is we can create a controller for this motor model that we have just created. So let me just do that. And what I'll do is I'm going to encapsulate or group these components I have selected into a subsystem. And a subsystem is just a collection of components that help you have a cleaner, more organized model. All right?
So by doing that, I know that I will need a PID controller. So I can just type PID. And I can delete some of these signals. And I know that the PID as an input it needs the error from the actual motor speed that we're measuring and also a reference speed. So I'll do that right now. I can input the signs that I want here for this sum block. This way I get the error.
And I can connect here the, let's call this signal here-- I can name signals-- let's call it motor speed. And here, I can enter this signal, which I will call reference speed. OK? And again, I can visualize this reference next to what I'm sensing. So now that I have a controller, let's see how this model would behave.
So you see here that by just adding the controller, I get a relatively improved response, but I believe we can do a lot better because we have a massive--
[INTERPOSING VOICES]
--overshoot here.
So we have a lot of overshoot and transient response in the system. So Ed, can you show me how we can tune the PID?
Exactly.
[INTERPOSING VOICES]
So if I knew gains that would work for my system, I would just parameterize the controller here. In this case, I just want to use a PID controller. I don't know if any gains to use. But what I'll do is I'll use some of Simulink built-in capabilities for controls. This is actually part of an add-on. But I can click this tune button. And you will see that Simulink will do some plan linearization.
And it will show me these graphs where I can, by simply dragging and dropping these sliders, I can tune the response of that controller. OK? So let's see how we can make it work. And perhaps this is OK. I can probably live with this.
So once you see a response that fits your needs or your requirements, you can actually click this Run button here. And what that will do is that it will update the gains in the controller block. So you see that now I can accept those gains. And by the way, let me do a double-check here. Because what I want to do is I want to make sure that I'm visualizing these values in the correct units.
And if I go into my motor here, I want to make sure that we're passing this in vaults as well. All right, so now, if I simulate the model again, I should be able to see, OK, so there's still a little bit of overshoot. But at the same time, I can always go back to the PID controller and tune that as needed to improve that response and make it a lot better.
So now that I built my motor model, one thing that I could do is I can share this work with other people so that we can plug it into a larger system, right? Because this is just a component, that motor. And I can save it as a model that can be called from a different system or a different model.
Or I can save it as a reference subsystem. And that's exactly what I'll do. And I'll just call this motor. And I'll save it in the working directory. So we'll go ahead and do that.
And with that, Karan, would you like to walk us through a more complex system?
Yes. So let's jump right back into the infusion pump example that we were looking at earlier. So just to gain some reference again. So we saw this model earlier. And we briefly looked at the results of the simulation of this model. And we saw that the system starts to malfunction towards the end of the simulation. And hopefully, we'll be able to fix it today. Cool.
So first of all, let's jump into the plant model. So inside of the plant model, you can see it has a lot of sub-components. It has the DC motor, which acts as the actuator, then it has this drive system, reservoir, and a delivery line. In fact, if you go inside the DC motor subsystem, you can see it looks very similar to the one that Ed designed for us.
In fact, let's select all these components and try to pull in the subsystem that Ed has worked on. Let's delete this. And this is to show you how easy it is to bring in components that other people have designed for you.
Right, so this really supports that collaborative nature in Simulink where I could have multiple teams or team members developing certain parts of my system and then plug it all together in the end to do a system level simulation.
Exactly. So right now, I just brought in the motor model that Ed had designed earlier. And I just plugged it in in a matter of a few seconds. Now, let's go ahead and run the simulation again and see if my larger model likes this or not.
Yep, looks like the simulation is running. And you can see at the bottom that it's running for a few seconds. Then we can go ahead and stop the simulation at this point.
So in fact, if you go one level up can see the other components in this as well. And it could be possible that different people are working on this plant model, and everybody is working on one of these components. And finally, you pull those different components together in one model, connect them together, and do a system level simulation. This is where the tool becomes really, really useful, where you're assimilating these complex multi-domain systems and analyzing and visualizing the behavior of these multi-domain systems.
Now, once you have completed the plant model, and you know how the plant model behaves, the next step is to start controlling the plant model.
Now, Karan, if I might quickly go back to the plant, I did notice that there are elements here from multiple domains and different colors, which is something we had talked about. I see that there is a domain for the motor, which probably has electrical components with mechanical components. But I also see other parts that, for example, that reservoir and delivery line. And I believe-- yes, you have multiple domains here. So again, this speaks to that multi-domain nature of Simulink, where I can bring in different aspects of my system and plug it all together and understand how that system behaves.
Yeah. In fact, as you probably would have noticed, initially we were working with blue and green, which represented electrical and mechanical domain, respectively. Now, we have a brown domain, which represents a hydraulic domain.
So moving on to how do we design a controller and test that controller against the plant model. So Ed in the beginning showed you briefly how we can design the PID to control the speed of that motor. And that's what we have implemented in this motor controller. It's a slightly modified version of the PID that Ed designed.
Now, imagine you're a nurse who was trying to operate this device, which is going to control the amount of medicine delivered to a patient. But the way you control it is by trying to control the speed of a motor. It's not a very intuitive design, right? That is why we need to add some level of abstraction and some level of supervisory logic to our controller.
And for that, we have the supervisory controller. So in this, we have some input commands coming in from the nurse, from the user. And at the bottom, we have some feedback coming in from the current status and the measurements of our machine. Now, if you go inside the supervisory logic, so we have built this decision logic using a tool called State Flow. And this is an add-on top of Simulink. And much like Ed built that DC motor for you guys using drag and drop or quick insert methodologies, similarly, we can build a state machine.
And if you go inside one of these state machines, you can see that it has multiple states. It has four states that it can run in. And it can transition between different states, depending upon some conditions that are associated with those transitions.
Right. So state flow really is an add-on for building those state machines. And those state machines are great for applications like this one, where I might need supervisory logic. And as you mentioned, Karan, we have the four states, but we're changing states with these transitions that are evaluating inputs or certain constants. And depending on what state I'm in, then the system will behave in a certain way.
Exactly. So now, if I run the simulation, you will be able to see that the machine starts in the off state, and it quickly transitions and going through one of these states all the way to the Basaon. And each of these state is associated with the different driving condition for the electric DC motor that we have. Cool. All right, so now we have seen the motor controller, which controls the speed. And on top of that had some supervisory logic which tells the motor what speed to go at.
But finally, we need to build some robustness into the system. Because when we ran these simulations earlier, we saw that our system had some faults in it because towards the end of the simulation the behavior was not quite as we expected. And to detect such abnormal behavior in the system, that is why we have built a subsystem called Occlusion Detection.
So the entire goal of the subsystem is to detect if the machine is malfunctioning. And the way we detect those malfunctioning is we have developed two different kinds of logic. The first option is using the physics and the mathematics associated and the principles associated with the machine. And we have implemented those using simple Simulink blocks.
And the second way of detecting malfunction in the machine is using machine learning algorithms. And we have implemented the machine learning algorithms using MATLAB code. And the main thing that I'm trying to highlight over here is that, for example, if you have your existing algorithm developed in, say, MATLAB or C, you can pull that algorithm into Simulink and use it as a part of your Simulink block diagram.
Right. Karan, so I'm seeing a few important takeaways here. The first one is that Simulink has the capabilities for variant management, right? Like, for example, I could have multiple ways to implement similar functionality. And those multiple ways may vary in performance or fidelity. And I might want to try some of those designs out. And Simulink enables me to do that with this concept of the variance.
The other takeaway I saw is that there's a lot of flexibility with Simulink to implement different approaches, right? For example, I think you mentioned you're using just Simulink to model this functionality here. And down here, you're using machine learning techniques with MATLAB, which leads me to my third point. And that is that you can bring in existing functionality that might exist elsewhere into Simulink, right? Whether it's MATLAB code that you already have, or C code that you might already have. And also, functionality from different places you can plug here in Simulink.
Yes. And in fact, you mentioned a plan that we are trying to compare different types of algorithms that we have implemented here. And if you closely notice at the block, at the very end source block that we have on the right, right now it's switched to select the first option. As you can see, if we are not implementing, we are not actually using the two algorithms that we have development. Now, if I go back to my MATLAB command window and change my control variable associated with that switch, and set it to use the first option. And now, let me update my Simulink block diagram using Control D.
You can see the switch has moved on to the second option. Now, this is the option of using mathematical principles of the machine that we're trying to control. And now, when we simulate the system, hopefully, my machine will be able to function a lot better.
Cool. So it can do some of this fault testing within Simulink, right? And that's the power of system modeling and simulation that I don't have to build an expensive prototype to find the faults in the system, to find the errors in the system. I can just create this model and look at some of those results here through simulation.
Exactly. So what we have done in this model is that we first simulated an initial version. We saw that there was something wrong and some piece of the puzzle was missing because our simulation results were not as expected. And then we fixed that by modifying our design.
Now, imagine the only other way to do this is build a physical prototype and test it in a physical prototype. And that process could easily become very costly and time-consuming.
All right. So what if we look at the results now that the model is simulating with that variant that you enabled?
So just before I go into results, let me quickly share a few other things. So first thing that I want to share is that we have designed this control algorithm in Simulink, right? Now, the next step is that we want to implement this algorithm, let's say, on some target hardware. For example, you have a target FPGA that you want to deploy this algorithm on. Or you want to deploy it on an embedded processor. How do you do that?
Or maybe you want to do some formal verification and validation like ISO 26262 or something like that. How do you do that? So for that, what you do is you go to the Apps tab. And this is where you can do all of those extended workflows. And you can either go to code generation and generate C-code, HTML code, and so on. Or you can do some model verification and validation. And you can see that a lot of other apps as well available here to help with either control design or whatever you're working on at the moment.
OK, so now, let's go and see our results. So it looks like our performance has improved quite a bit. Now, let's actually compare it with our initial run that we have stored on the left-hand side here. Let me change the color to make the difference apparent.
So basically, what we're visualizing here is the results from this new run that you did with the new variant. And then the red line is the results from the very first run where there was nothing to mitigate the fault. Is that correct?
Yes. In fact, you can see that there's still some room for improvement in the latest results as well. And we can quickly go back to the system and make a few other modifications and again simulate it and see how it does. That's the goal of using Simulink and running many simulations so that you can fine tune your design before you build prototypes.
Cool. Now, with that in mind, Ed, why don't you tell us how we can learn a little bit more about the tool and start getting used to the tool?
Absolutely. And for that, I will take us back to the Simulink start page. So if we go to that start page, you can come down here to the bottom left corner where we have onramps for Simulink and State Flow and even more from the MATLAB side. You can explore onramps on MATLAB, machine learning, and so on.
But these onramps, they're interactive free tutorials that are available to you from the product to walk you through different steps so that you can learn and get familiar with a product in a matter of just hours. So there is these onramps include overview videos. They also include tutorials that walk you through some of the design problems.
And you can see the steps or the tasks are here in the pane on the left. And as you do your modeling work in the model, you can submit your work. And then you get instant feedback on whether the design that you did was correct or incorrect. So it's an excellent way to learn the tool as you use it.
The other way that you have available to you to learn the product and to get started is through examples. So by going to that start page, you can explore these examples. There are many of them for different systems for different applications. And some of them we call them reference applications, which are an excellent way for you to simply get started and be successful in your project.
OK, so Karan, we also saw just one example of an infusion pump. But can you show us examples of other applications?
Yes. So today, we focused primarily on the example of an infusion pump. But I want to emphasize on the fact that it is just an example. Simulink is being used for a lot of other applications in different industries. For example, it's been used for image processing, advanced driver assistant algorithms, power electronics, and so on. If you're interested in learning more about different applications that Simulink is used for, and you want to read a little bit more about the success stories that different companies have been able to achieve using our tool, you can visit and read about them at our website.
Now, how do you get started? So we already showed you that you can get started to learn a little bit more about the tool from the tool itself, using things like Simulink onramp or State Flow onramp. But there are a lot of other resources that we have. For example, you can start with MATLAB Central, which is a user-driven community, and where a lot of users come together, share their work, share their questions. And you can explore about the product from there.
In addition to that, we have services built around the product to support the product. We have training services, as well as consulting services. Now, the goal of these services is to help you utilize the tool in a way that is the best suited for your needs.
Right. And also, these training and consulting services are helpful to you if you need to get started with other activities, like verification and validation, or even co-generation from your models as Karan mentioned before. So don't be afraid to contact us if you want to have a conversation around those topics as well.
Cool. So that brings us towards the end of the presentation. So Ed, why don't you summarize all that we covered today?
Absolutely. So in the session today, we saw that Simulink is a graphical environment for designing, simulating, and testing systems. We saw that it works really well together with MATLAB. It's based on top of MATLAB. And Simulink is widely used by several industries and across many applications. Today, we just saw an example for a medical device. And we also saw some of the resources that are available to you so that you can get started learning the product, adopting the product, and ultimately being successful in the projects that you work on. So with that, we want to thank you for your time.