Getting Started with Simulink for Controls
Get started with Simulink® by walking through an example. This video shows you the basics of what it's like to use Simulink.
You will learn how to model, simulate, and test a controller for a solar panel as it tracks the movement of the sun throughout the day. You’ll see how to model a physical system, design a basic PI controller for that system, and then run simulations to ensure that the model is working correctly.
After going through the example, you’ll learn how Simulink is just one part of Model-Based Design for modeling, simulating, testing, and implementing real-world systems.
Simulink is a graphical environment for modeling dynamic systems---that is, systems that change over time. Simulink takes care of the simulation so that you can focus on the engineering. You can use it to model simple things---like a home thermostat; or complex systems---like a fully autonomous vehicle or a surgical robot.
This video will show you the basics of Simulink and give you an idea of what working in Simulink looks like. Stay tuned to the end to find out where to go to learn more about how to use Simulink. Let’s get started!
At the MathWorks headquarters in Natick, Massachusetts, there’s a number of solar panels to generate electricity.
These panels face south and are fixed in place. That means they produce more electricity when the sun is shining directly on them in the middle of the day, and less power when the sun is to the east or west, early and late in the day.
What if you had solar panels that rotated to track the sun so that you could produce as much electricity as possible?
In this video, we’ll use Simulink to design a tracking system to keep a solar panel aligned with the sun. If you want to follow along as we build the model, you can download it using the link below.
The physical system consists of a panel and a motor. We’ll model those first and then we’ll add a controller to track the sun’s position.
Once we’re happy with the design, we’ll test it to see how well it does tracking actual sun data.
The physical system has two main components. With some basic physics, we can write out the equations of motion for each. Let’s model the panel first.
You start Simulink by clicking the Simulink button in the MATLAB toolstrip. This opens the Start Page, where you can create new models, find examples, and even find basic training.
We’re starting our model from scratch, so we’ll choose Blank Model.
Simulink models are built up from blocks and signals. Open the Library Browser to see all of the blocks available. To model the equation for the panel, we’ll start with the Integrator block.
Let’s click and drag an Integrator from the library to our model.
So why an Integrator block? Well, the integrator block takes an input and integrates it over time. If we integrate acceleration, we get velocity. If we integrate velocity, we get position. This is the basis for modeling differential equations in Simulink.
Our equation has acceleration and velocity terms, so we need at least one integrator. Let’s add another one to also get the position.
We connect blocks together with signals by clicking and dragging between blocks. Don’t worry about these red lines, we’ll connect them up in a second.
It’s a good idea to label signals to keep things organized, so we’ll double-click the signals and type in a name. Let’s name this one theta_dot_dot for acceleration, then theta_dot for velocity, and theta for the position of the panel.
Next, let’s model the right-hand side of this equation. First, the torque term.
Let’s use a Constant block. We’ll replace this with the motor later.
You can double-click a block to change its parameters---let’s change the value to 10.
We need to perform a subtraction, so let’s also grab a Subtract block.
The damping term depends on the panel’s velocity---the theta_dot signal. We can branch this signal by right-clicking and dragging to connect it to the Subtract block.
And don’t forget we still need to multiply that by Kd. Let’s use a Gain block for that.
Rather than hard-coding the gain value, we can also use a variable, or even MATLAB code. So let’s just say the gain is Kd. That red box is telling us that Kd hasn’t been defined yet. So click the three dots and select Create. We’ll give it a value of 5 and store it in MATLAB’s base workspace. Let’s check MATLAB, and yep, the variable Kd has been created.
To finish up the equation, we need to divide by the inertia, J. We’ll do that with another Gain block.
But now that we know the name of the block, we can just double-click in the model and start typing the block name. Then, use the drop down to find the right block and hit enter.
We’ll set the gain to 1/J and, again, let’s define the variable J in the base workspace with a value of 8.6.
Let’s add an annotation to show the equation being modeled---so when we come back to it later, we’ll easily know what the equation is.
That should be all we need to model the panel. But to check if everything is working correctly, we’ll want to visualize some signals. Simulink has a bunch of visualization tools. For a quick check of a signal, the easy option is to use a Scope block ... and connect it to the signal we want to view.
Let’s add a second scope to see the velocity signal.
OK now we’re ready to simulate the model. We can set the simulation Stop Time in the Simulation tab----but, we’ll leave it as 10 for now.
To run the simulation, just click the Run button. The simulation finished but what you didn’t see was that Simulink numerically solved the differential equation through time.
Double-click on a scope to check out what happened.
In the position scope, we see the panel’s angular position increasing.
In the velocity scope, the velocity starts from zero and levels off.
So with a constant torque, the panel starts turning and then settles to rotating at a fixed rate. Well that makes sense.
Let’s do a quick sanity check and see what happens if we switch the sign of the torque. We can edit the constant value directly on the block!
Rerun… and… that looks good---we see the panel now spins in the opposite direction.
Now that we have the panel model working, let’s group those blocks together to keep things organized. Just select the blocks you want to include---we’ll leave the Constant block and the scopes out for now---and then click Create Subsystem in the Modeling tab.
Now all of those blocks are contained within this subsystem. Let’s name it Panel.
You can double-click to see the subsystem contents. These oval-shaped blocks are inports and outports---that’s the data going in and out of the subsystem. Let’s adjust the port names.
Go back up to the top level... and there are the port names we just entered.
Ok, we still need a motor. Remember that motor equation? Well, we can follow the exact same process to model it.
And.. we’ve got our motor!
We’ll supply a voltage to the motor to generate a torque and move the panel. Let’s check what happens. We can see that the panel spins when a voltage is applied to the motor---so everything looks good so far!
OK we’ve modeled the panel and the motor. Now we need a controller to set the correct voltage so that the panel tracks the sun.
From our model, we know where the panel is pointing. And, let’s say the sun is over here.
We want the panel pointing at the sun, so the difference between these two angles is the error. We’ll add a controller that applies a voltage to the motor to make that error as small as possible.
And if the sun moves, the controller will react accordingly to keep the panel pointing at the sun.
OK, back to our Simulink model!
We have the position of the panel here. To get the error, we need the position of the sun. While we’re designing the controller, we’ll use a Unit Step input---that’s pretty common in control design. We’ll test it with some actual sun position data later.
Now to calculate the error. We’ll use a Sum block---it’s already got this nice circular shape common in control schematics. We just need to change the second port to be minus instead of plus.
Next we need a controller. There are a lot of options, but a common approach is some form of PID control---which stands for proportional/integral/derivative, because the control output is some function of the error, the integrated error, and the derivative of the error. But we don’t need to build all that ourselves, we’ll just add a PID Controller block.
We connect the input to the error signal and the output will drive the motor.
You can see there are a lot of ways to customize the controller. We’ll switch to a PI controller---the D term helps respond to quick changes, which we don’t need because the sun moves steadily across the sky.
There are two gains to adjust: one for the proportional term and one for the integral term. These affect the controller’s response. We’ll set the proportional gain to 240 and the integral gain to 180.
To see how well the controller performs, let’s use the same scope to show both the sun position and the panel position. Notice that a new port is automatically added.
Let’s run the model… And add a legend to the scope so we can tell the signals apart. Let’s also make the position line dashed.
We can see that the controller overshoots a little and then settles to the reference value of 1. That’s good enough for our application.
OK, we designed our controller. But, can it actually track the movement of the sun? Well let’s see how it performs when we provide it with some real data.
Let’s load some sun position data into the MATLAB base workspace. This file has two variables: a vector for time spanning 15 hours, and a vector of the sun’s position at each point in time.
Let’s plot it.
You can see that the sun rises in the northeast at about 60 degrees from due north and sets in the northwest at about 300 degrees.
We can bring the sun position data into the model by replacing the Step block with an Inport.
We need to select which data to use. Click Model Settings in the Modeling tab. Then navigate to the Data Import/Export pane.
There are a LOT of settings in here---if you’re ever unsure about anything, just right click and select “What’s this”. This Input option is what we need.
When specifying input data, the first column should always be time. After that, you can add a column vector for each Inport in your model.
Since we now have 15 hours of data, we’ll change the simulation stop time.
Let’s run the model and we can see its tracking the sun’s position quite well.
So… we modeled our solar panel system, developed a controller, and tested the system to ensure it will track the motion of the sun. And, it looks like our design works!
But that’s just the beginning. If we want to make these panels a reality, we can incorporate proper design specs into our model.
We can bring in other tools like Simscape to model the mechanical and electrical systems, without needing to derive any equations! And with Stateflow, we can add logic to make the panels smart, so they spin back to the east at the end of the day, and they know what to do if conditions change.
Then when we’re ready, we can automatically generate code from the model and deploy it directly to hardware.
At each step of the way, we can continuously test the design to ensure it’s error-free and meeting specifications.
With the model at the center of our design process, we can tackle all kinds of design questions. And so can you.
Now that you have a feel for what working in Simulink is like, it’s time to learn it. The best way to learn Simulink is to work with it. So, start up Simulink Onramp, which will teach you the basics of Simulink interactively. It’s free and takes just a couple of hours.
Welcome to Simulink.
Download Code and Files
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- 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)
- United Kingdom (English)
- Australia (English)
- India (English)
- New Zealand (English)
- 日本Japanese (日本語)
- 한국Korean (한국어)