# Teaching Modeling and Controls with the MATLAB Live Editor

From the series: Online Teaching with MATLAB and Simulink

*
Dr. Richard Hill, University of Detroit Mercy
*

*Melda Ulusoy, MathWorks*

Professor Richard Hill demonstrates how to use the MATLAB^{®} Live Editor to help your instruction come alive. Learn how to create engaging lectures, virtual labs, and interactive homework assignments that illustrate complex topics and help students build intuition for course material. Examples demonstrate how to perform control design interactively using techniques such as Bode plots, Nyquist diagrams, root locus, and PID controllers.

**Published:
18 Jun 2021**

Welcome to the webinar Teaching modeling and controls with the MATLAB Live Editor. My name is Dr. Rick Hill. I'm a professor in the Department of Mechanical Engineering at the University of Detroit Mercy.

And so the purpose of this webinar is to show just how powerful the Live Editor is for improving your instruction-- bringing your instruction alive, keeping your students engaged, really helping them to learn the material better. There's a great deal of research that shows that having active learning greatly improved student learning outcomes. And the MATLAB Live Editor really helps, in that sense, so that you're not just sitting there talking at your students and having them passively listen for 75 minutes.

So many of the examples that I'm going to show are based on this website, the Control Tutorials from MATLAB and Simulink. And so the live scripts that we are developing are interactive versions of some of these static web pages. And so this is a site that was generated, or created, by Don Tilbury and Bill Messner. And they graciously invited me to join them about 10 years ago.

And so I've been helping to oversee the site. And it's keeping it updated, expanding the functionality of it. And so some of these things may look familiar to you.

So the structure of the webinar is first, I'll just try to introduce what the MATLAB Live Editor is. And then, I'll spend a fair bit of time going through examples to show how powerful of a tool it is in the context of teaching system dynamics and control courses. At the end, I will briefly talk about some strategies for how you might integrate this tool into the way that you teach your courses.

So what is the MATLAB Live Editor? So this is a picture of a live script. And if you've done publishing with MATLAB before, you may recognize the format of it. And so in a lot of sense, it does look like a document that you published from an m-file You can have your code.

So you could have code. And then, it'll automatically generate the output of that code. You have narrative. You can change the headings and the formatting.

You can add hyperlinks. You can add figures. You can add equations-- all of these sorts of things, the same sorts of things that you can do when you publish a document.

The thing that's different about the Live Editor is, number one, you can make these changes directly within the graphical document without publishing from an m-file. And then, the other thing is that you can have live controls, such as the slider, here, where you can be varying parameters, and in real time, see what the effect is on the output of the code.

So in this picture, when you vary that slider, you're changing the proportional gain. And then, the following code then uses that new value of that parameter to generate in real time, showing you how the figure changes, how the graph changes. So I'll go ahead and open up MATLAB.

And so here is that live script. And so right away, if I was in here, I could type right into the document. I can go up into the toolbar. I can change the formatting of things.

I can insert equations, and hyperlinks, and figures. I can include these different types of live controls, and things like that.

And so down here, I have a chunk of code. And I've got my slider for the proportional gain. And if I adjust it, it changes the value of that parameter in real time. And then, it runs the code, and you see how it affected the corresponding graph that's generated.

And so the way that you do that-- so if I wanted to insert another slider, when I insert it, I can give it a label. So let's say we want to do the same thing with the derivative gain. I can say what the minimum value is. I can say with the maximum value is.

I can say what increments we want to step it in. I can change what will cause that chunk of code to execute, and how much do I want to execute-- just that current section, the entire document, whatever. And so this slider is now assigning a value to that number.

And then, I simply just type the MATLAB code right around it and set it equal to that variable, Kd. So here is one way to output the code, where it's in line. There are different ways to do this.

So over here in the upper right hand corner, right now I've got Output inline. I can also hide the code. And so that is useful. Both options are useful.

So sometimes, in some cases, maybe you want to hide the code from your students, because they might find it confusing. You want to keep the numerical slider close to the figure that it's modifying. But then sometimes, you might want to show the code, because then the students can learn from the implementation.

Another option is to output the value on the right. And so you've got your code and the narrative and everything on the left-hand side. And on the right-hand side, you've got the corresponding figure that's generated. So those are a few options.

So going back to the PowerPoint slide, that's an introduction to what the Live Editor is. So some of the benefits of this Live Editor is that it's self-contained. So a lot of the functionality of the Live Editor has existed in MATLAB for a long time.

You could do publishing. You could publish from an m-file. So that's existed. You've had interactive apps that you could create and use.

The nice thing about the Live Editor is that you have the interactive nature of the apps. But it's embedded in a document that you can really use to explain the app. So you can have written instructions. You can have figures. You can have hyperlinks.

You can have equations. So it's all in one place. Some of the great things about it is the interactive nature of it. So you can have these graphs, you can have these animations-- which I haven't shown you yet, but I will-- where you can really bring things to life. It's a living thing where you can really demonstrate to the student what the effect of some sort of change is.

And then following along with that, the students can use that, either within class or outside of class to do what you might term inquiry-based instruction, or to run a virtual lab. So you can imagine that you have the students experimenting with this interactive script, so that they can draw conclusions on their own, and build meaning for themselves as opposed to you telling them, this is the effect of adding integral control.

You can give them this live script. And they could do some experimentation where they might be able to come to that conclusion on their own. Another great thing about the Live Editor is, it's really quite easy to use. So as an instructor, if you want to create your own live script, if you have some facility with MATLAB, then you shouldn't have any issues creating these nicely formatted living documents.

From a student's perspective, it's easy to use in the sense that they don't need to know MATLAB at all. You can hide the code. And they can just play with the interactive nature of it, and see what the effect is without knowing anything about MATLAB programming.

So one of the really strong benefits of the Live Editor are these live controls. And so I showed it when we inserted that numeric slider, but here in the toolbar under Controls, there are several different live controls that you can implement. So the one that we already showed was a numeric slider.

And so this allows you to vary a parameter continuously. So maybe this is the parameter of a physical system of a model, the amount of damping or the amount of stiffness, or the amount of mass, or something like that. You could use it to adjust Control gain, anything like that.

You can also implement dropdown menus, where you can choose from discrete choices rather than a continuously changing variable. You could even do things like change the system or the plant that you're analyzing. You could do a different type of input into your system.

You could do a different type of controller or algorithm. There's a checkbox, which is a Boolean sort of thing. It's either checked or it's not. So you could use that to hide code, or hide a solution, or show a solution.

You could use that to hold a figure, or a clear a figure. You can do Edit field. And so this is similar to the numeric slider in the sense that you can change a variable continuously. But instead of having a slider, you type in the value directly, and then you could also have a button where if you press that button, it'll launch that piece of code, or it'll run that piece of code.

So those are some examples. I'll go back to MATLAB. Here's another live script to show some more examples.

And so here on the right-hand side, I have two graphs. I have the free response of a system for some given initial condition. And then, I have the forced response of that same system for some given input.

And on the left, I can choose what type of system I have-- a first-order system, or second-order system. And I can choose what the input is-- Step, or Ramp, or Sine. And so when you insert that dropdown menu, you can give it a label.

And then, for your different choices, for the different items you have different labels. And each of those labels will correspond to a given piece of code. So the first label, first-order system creates a first order system, a specific first-order system with a particular initial condition, for example.

And so here, you can play around. So instead of a first-order system, let's say we're going to choose an underdamped second-order system. And you can see how the behavior changes.

You can see how the response changes for a Step input. You can change the input to the system-- instead of a Step, you can do a Sine. This is an example.

And so the power of this, from my perspective, is maybe you have a homework problem, or you're doing an example in class, where the students are solving a differential equation, or they're given a transfer function. And they're supposed to find the output for a given input. And they have a page of work.

They're doing a partial fraction expansion. They are looking up the things in the Laplace transform table and doing all sorts of algebraic manipulations. And sometimes, the student can get lost in the details of that math.

They do this page of work. And at the end of it, you ask them, does that make sense? Is that physically what you would expect the answer to be? Or based on that answer, could you change your system to get a different performance?

And oftentimes, they don't know. They get lost in the details. But here, you can really drive home some of the conceptual understanding by having them experiment with this without having to get bogged down in the details of the math.

So here's some more examples. So here, we have a chunk of code. And we've got a couple of sliders where we can change the pole locations of a canonical underdamped second-order system.

So we can change the real part of the pole, and the imaginary part of the pole. And then, we can have-- on the right, we have two graphs. We can see how the pole location is changing in the complex s-plane. And then, we can see how the corresponding Step response is changing.

So the real part, I can see how the pole moves. I can see what the corresponding effect is on beta, on omega n. And then, I can see what the corresponding effect is on the Step response.

And so one thing that you'll notice in this example is that I've added a bunch of annotation to the figures. So the one figure is just a PZ map. The second figure is just a Step, just using the Step command.

And so here, I've created a little function-- display poles-- and another function-- display characteristics-- to add these annotations to the graph. In order to keep things clean, I've defined these functions at the very end of the document. So either you can hide the code or you can put some of the functions, some of the code in a function, and put it at the end of the document.

So here's another example where we have a Bode plot. And we have a slider where we can change the zeta-- the damping ratio for the underdamped second-order system and see what the corresponding effect is on the Bode plot. And so in this case, the code is going to hold the figure.

And so I can see each of the different Bode plots on the same figure in a different color with a legend. And then, I have a button. And so when I click that button, it runs a set of code to clear the figure. And so then, I can start over.

So in here, we've seen some examples. The slider, the dropdown menu, the button. Just a few examples.

The other thing that the Live Editor has is, it has a Live Editor Tasks. And so again, up on the toolbar, right next to the Live Controls, there are Live Tasks. There are quite a few. And in the simplest terms, these Live Editor Tasks are just apps.

But they're apps that you can embed within your live script. And so there are some that are specific to control system design and system dynamics. So there are a couple here, you can see, that use system identification.

So you could feed the app some data, and then fit a model to it. Here, you have one convert model rate. So that'll do a discretization of a continuous model.

You have reduce model order, which will generate a reduced order model. And then, you have this tune PID, which will autotune a PID controller. So if we go back to MATLAB, here's another live script.

And so here what I have is, I have a DC motor. And the model is a DC motor with a speed output. And here, the slider is bearing a parameter k, which is the motor's motor torque constant-- so not a control gain.

And then on the right, what it's going to do is, it's going to tell me what the poles are in the motor model for that value of k. And then, it's going to plot the Step response for the original motor model, the full motor model, a second-order system with these two poles. And then, it's going to do a reduced order model.

And so in this case, the two poles are about a factor of 5 apart. And we're generating the reduced order model just by neglecting the faster pole. So neglect the fast pole, try to keep the DC gain of the two models the same.

We vary the motor torque constant. We see how the poles change. In this case, they will get closer together.

And then, we see what is the effect on the reduced order model in its agreement with the original model. And so in this case, you can see that the two poles are now much closer together. And so you don't get as good of agreement. It's not as good of a design decision to neglect that one pole, because it's not appreciably faster than the other pole.

So that's one way to do a reduced order model. We then have a Live Editor Task. And so this Live Editor Task is the reduced model order Live Editor Task.

And you can specify different things. So you can specify what you will name the reduced order model. You can specify what model you're going to reduce, what method you're going to use-- so something more sophisticated than what we had done.

You can say what order you want the reduced model to have, and where you want the agreement between the two models to be emphasized. And so in that case, the Step response match is better than our simplistic approach. So you can see that.

And then here, it also shows the Bode plot. And so you can see here, we are trying to preserve the DC gain. We're trying to make the model to match at low frequencies. And so they match quite well at low frequencies.

But then, when you get above, let's say 10 radians per second, you really start to see a difference between the two models. And so this is something that you could include to drive home some things, or have the students play around with some more advanced techniques.

Down here, I have how the original model and the reduced model, order model, agree for different sine wave inputs of different frequencies. And so here, we have a frequency of 13 radians per second, which is higher. And you can see that you don't get great agreement between the original model response and the reduced orders model response.

They're in phase, but they have different amplitudes. But if we put in a lower frequency input, going back and looking at the Bode plot where the magnitude plots agree much better, you can now see that you have a lower frequency, and much better agreement between the output of the original model and the reduced order model-- almost perfect agreement.

So that's an example of a Live Editor Task. And so at this point, what I'd like to do is, I'd like to really try to give some more specific examples of how this tool is especially useful for modeling and control courses. So one of the things about control courses is that they're quite mathematical.

And so this Live Editor can really help you connect the math to the physical world. And so one way to do that is to show different representations of the math and the graphical. And so one example that we've already shown was comparing the poles, the location of the poles and the complex s-plane with the corresponding time response. So that's one way.

You could also envision a situation where you're varying a parameter. And you can side-by-side see what the effect is on the Bode plot and the Step response. And so really tie together the relationship between those two representations.

Another really powerful thing that you can do is, you can implement animations so that you can change a parameter, and then have a corresponding animation, where you can really have a virtual lab, where the students are experimenting with something where they can see the physical response. So here on the right is an example that you can access from the MathWorks website. It is the animation of a double pendulum.

And so I have modified that for just a single pendulum. And so here, I have a single pendulum. And what I can change, or what I am using my slider to vary, is the initial angle of that pendulum.

So I give it an initial condition. I pull the pendulum back, and I let it go. And so what I'm trying to drive home here is that what I'm doing is I'm comparing the full, non-linear model-- the nonlinear differential equation-- with a linearized version of that differential equation, where I'm using a small angle approximation such that in essence, I'm linearizing the model about an angle of 0, or the pendulum hanging straight down.

And so with the Live Editor, if you change the parameter, it can take a little bit of time to run the code to generate that simulation. And so here, I made a change. This showing a 20-degree change, not a 50-degree change. But it's quite slow, initially.

But once it's run once, then you can play back. So you saw when I ran this initially, it wasn't this slow. And so it had already run the code once. And once you've run it once, you can then play it back. And so that functionality is available within the Live Editor.

In this example, we're changing the initial condition. But you could imagine changing other parameters. So you could imagine changing the length of the pendulum, the mass of the pendulum, different things like that.

You could imagine doing something similar with a mass spring damper, or an inverted pendulum, or anything like that, where you're changing some parameter-- the stiffness, the damping, the mass-- and then observing the effect on the animation. So this is a really effective thing.

In my teaching, I've done this with hardware, where you do actual, physical experiments. And so the challenge of that is, you have to have the physical hardware. You've got to set it up. You've got to distribute it to all the students.

And it's great. There's a lot of value in that. But it takes time. It takes your time. It takes class time.

It takes money. And so here, you can emulate that virtually, where it's very quick to do. And you don't necessarily have to have all that physical stuff.

So that's one value of the Live Editor. Another thing that I think the Live Editor is especially helpful for in teaching controls is to teach these graphical tools that are so common. So the root locus, the Bode diagram, the Nyquist diagram-- these are powerful tools that we all use in control systems.

And again, it's a situation where sometimes, the students can get lost in the details. So you ask them to draw a root locus. They do all these calculations. They can calculate the departure angle. They can calculate the asymptotes, draw a beautiful root locus.

And then, you ask them, well, what does that mean? Or how do I how do you use that to implement, to choose your controller, design your controller? And know they can be at a loss, sometimes.

And so here is an example-- this picture, here-- of a live script where you're trying to teach them what the root locus is. And so here, we have a slider for a parameter k-- the loop gain, for example-- in a close-loop system. And as you move the slider, it plots what the corresponding closed-loop poles are.

So each blue dot paired with each green dot is the closed-loop pole for a specific value of k. And so as they move this slider, it's plotting different closed loop pole locations. And then, if you were to use the R locus command, or whatever and draw this nice, clean, smooth, continuous line, they might be able to make the connection better between what that graph is representing and what it looks like.

And then here, we have a dropdown menu where you can either to choose to hold the figure so that you can keep the previous dots, the previous closed-loop poles, or you could choose to not hold the figure. And it'll show only the closed-loop poles that correspond to the current value of k.

I'm going to go ahead and show you a couple other live scripts for other types of graphical tools. So here, we have our pendulum again. And so here, you can see that there's this playback controls. And so once you hit Play, it moves very quickly.

And you can start to see how for a 20-degree initial angle, the agreement is not quite as good as it was for 15 degrees. And if I ran it again for a larger angle, the agreement would be even worse. So here is a live script where I have a slider, where I'm changing the frequency of a sinusoidal input into a system.

On the left here, I have with the time response is. So the input is in blue. It's a sine wave of a frequency of 0.3 radians per second.

And then, the output is the dash line. And so as I change the frequency, the phase of the output changes with respect to the input. And the amplitude of the output changes with respect to the input. So those are the two quantities that we're interested in, with frequency response.

And then over here on the right-hand side, at that given frequency, I have these blocks, these squares that are marking the magnitude and the phase. And so looking at this, you can see that as I increase the frequency, the magnitude increases. So it goes above 0 DBs.

And so that means that the output is amplified with respect to the input. And then, the phase gets more out of phase. And so it lags behind. And so you can really make that connection between what the response due to that sine wave is. You can see the output is much bigger. You can see that it's reaching its peak some time after the input does.

And then, that really drives home what is being displayed in the Bode diagram. You can do a similar thing for the Nyquist. So here, we have a Nyquist diagram corresponding alongside its closed-loop Step response. Here, we have a loop gain, k.

And if you're thinking of the Nyquist stability criterion, we don't have any encirclements of the point minus 1. If I increase the gain, you can imagine that each point at each frequency I've taken, that point is at the same angle. It's at the same phase.

But now, it has a correspondingly larger magnitude. And so you're going to grow the shape of that Nyquist diagram. And you're going to get closer to encircling that minus 1 point. So you're becoming less stable.

You can see how the Nyquist diagram changes. You can see how the gain margin changes. And if you increase it enough, eventually you will encircle that. And then, based on the Nyquist stability criterion, the system is now unstable. And you can see that corresponding change in the time response, as well.

We then also mentioned about using the Live Editor to help with tuning. So they had the picture of tuning, the Kp, Ki and Kd. Here is a lag compensator. And so the two parameters that you can vary with the lag compensator are-- how far apart the pole and the 0 are, what the ratio between the pole and the 0 are. So a student could vary that parameter and see what the effect is on the shape of the lag compensator.

The other parameter that you can change is where the pole and the 0 are centered-- what the geometric mean of those two values is, those two frequencies is. And so really quickly, the students can see what the effect is. And if you added it to a plant, you could quickly iterate on what the impact is.

So, powerful. The other thing that these live scripts can be good for with teaching controls is helping the students to understand how to tune controllers. So a typical homework problem might be that they have a canonical second-order system. And they're supposed to calculate control gains to place the poles in a specific location.

And since it's canonical, and the math is pretty easy, they are able to absolutely calculate the absolute gains to put the poles in that absolute location, to get the performance that's required. In the real world,

maybe your model is uncertain. Or maybe your model does not have a canonical form-- it has a 0, it's higher order, it has nonlinearities.

And so especially in an introductory class, they may not have the analytical tools to calculate what the specific control gains should be. And so they may have to iterate, where they do an initial calculation. They try an initial set of gains. But then, they might have to adjust them, or tune them.

And so if they have to do this calculation repeatedly, it takes a long time. And it's not very illustrative. With the Live Editor here, you could imagine that you have a slider for each of the PID gains. And the students can adjust them, and see in real time what the effect is on the system's performance on its time response.

So these live controls can be really, really valuable for that. So those are some examples of how you might use the Live Editor in teaching modeling and controls courses. How, then, do you implement it in your curriculum?

So one way is, you could use these live scripts as your lecture slides. So you could imagine as you're lecturing, you're projecting this live script, and just scrolling down the document. But it's really, very alive in the sense that, as you change your parameter, you can really quickly see what the corresponding change is in the graph, or the animation, or whatever.

So really engaging lecture slides that grab his students' attention, and really illustrate the concepts you're trying to demonstrate. You could also use these live scripts for in-class activities, or virtual labs, where you can imagine the students in the class have their laptops open, or you're in a computer lab. And you share with them the live script file.

Either there's a shared directory, or you drop it into the chat. You drop the link into the chat of your Zoom meeting, or whatever it is. And the students open up that live script, and play with it in the middle of class. So you pose a question-- what do you think the effect of adding integral control is, in this instance?

And you pose this question. They have this live script that they experiment with for a few minutes. And then, you come back as a class, and you discuss.

You could also imagine what some people refer to as a peer instruction approach. So it's an approach that I use in my teaching, where I will have a multiple choice question. And so I'll say, OK, this is my question-- what's the effect of adding an integral control? Is the answer A, B, C, or D?

Have the students play around with it. Poll the class. 50% of the class thinks it's A, 50% of the class thinks it's C. So you ask the class, OK, turn to your neighbor and try to justify your choice.

And so they explain it together. They're really trying to work through it. And it really sticks with them. They really come to have meaning-- that concept really comes to have meaning for them.

If the entire class gets it right on the first try, then you can move on. And you don't have to spend that class time discussing it. In some respect, it's closed-loop teaching. You're getting that immediate closed-loop feedback from the students.

Another approach you could imagine would be to assign these live scripts as preclass reading, except instead of it just being a static textbook, it's a living textbook, where you can in the narrative direct them to do experiments-- play with the value of the dampening ratio and see what its effect is on the Step response, or whatever.

So you can imagine it that way. Another approach would be to provide these live scripts as a homework assignment. And so you can imagine the live script as being a template, where there might be pieces of the file that are blank that the students have to fill in with their solution.

You could imagine that you could have information hidden, where there's maybe a checkbox, where you show a solution, or show an example, or you hide a solution, or hide an example-- different things like

that. So really, really powerful ways to use it in your classroom. There are different places to access, resources to help you implement these things.

As I mentioned earlier, many of the examples that I showed you that I'm developing are interactive versions of this website. So this website, you can access here-- ctms.engin.umich.edu. And so within those pages, there will be a link that will take you to a MathWorks site, sort of a landing page.

And that MathWorks site is this second link. So I'm going to go ahead and go to this website. And so here's this site. It has a lot of different examples-- different model systems, and then different aspects of it.

So if I open up one of these pages, this is just a static web page. Within it, there's this button, here. There's this link. It says Run Live Script Version in MATLAB Online.

And so it takes you to this landing page. And there's several examples, here. But this is the one example that we wanted to go to. And so here, you can then download the live script for use yourself. Or you could watch this example.

And what it will do is, it'll open up MATLAB Online. So it'll open up this live script within the online version of MATLAB. And so MATLAB Online is really a nice tool, in the sense that if you wanted to use this in a class where students haven't necessarily downloaded MATLAB to their own machines, you could use MATLAB Online as a web base.

And so they don't have to install anything. And so then, here you have your live script. And right within MATLAB Online, you can have the students playing with different parameters and seeing what the effect is. So that's one way to access.

As time goes on, we will add more and more examples to that site, and more and more links to the Control Tutorials website. The MathWorks website also has a lot of other information.

It has documentation. It has examples you can look at. And the MATLAB File Exchange is also a great place to look, if you're trying to do something specific.

And so that brings us to the end. So hopefully, I've demonstrated how the Live Editor is a really powerful tool for bringing your instruction to life, for engaging the students in an active learning. Here, I demonstrated examples for teaching modeling and control. But obviously, you could use these tools for any other class that you might teach.

I'd like to thank the MathWorks that's supporting this work, and in particular Melda Ulsoy, who has been very supportive, given a lot of ideas for examples, really gotten in there and coded some of these examples-- a big contributor. And then, my students at the University of Detroit Mercy, who have been my Guinea pigs. Here is my email address. And here is Melda's email address.

We would welcome you to reach out if you have any questions, or want an example, or anything like that. Hopefully, you found this useful. And I look forward to you being able to take advantage of this powerful tool. Thank you.

웹사이트 선택

번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:

또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.

사이트 성능 최적화 방법

최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.

미주

- América Latina (Español)
- Canada (English)
- United States (English)

유럽

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

아시아 태평양

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)