This tutorial shows how to simulate the model of a dynamic system using Simulink® software, and then use the results to improve the model. The model is a house heating system that includes a heater (plant), thermostat (controller), and room (environment) with interfaces to input measured system data.
Before preparing a model for simulation, build the model and validate the model design. See Model a Dynamic System.
Add interfaces to input measured data, add control signals, and update model parameters. See Basic Simulation Workflow: Prepare for Simulation.
Before simulating a model, consider your goals and requirements. For the house heating system, these are the goals:
Verify that the simulation represents the behavior of the modeled system.
Improve the accuracy of the model by optimizing parameters.
Measure parameters for an actual house heating system. You will use the measured data with model simulations to verify the behavior and accuracy of your model.
Measure the outside and inside temperatures of a house every 6 minutes for 24 hours.
Enter the measured data into a Microsoft® Excel® worksheet.
You can open
Plot the measured data in a chart. The inside temperature data shows temperature spikes when the hot air heater turns on. This pattern is typical for a hot air heating system.
Prepare a model for simulation by adding external interfaces for data input and adding input control signals.
Replace the In1 block with a Signal Builder block. The Signal Builder block imports data from a Microsoft Excel worksheet.
Replace the In2 block with a Constant block and set
the value to
20. The Constant block sets the thermostat
Save the model.
For the first time through the basic simulation workflow, use the model parameters saved with the model.
Some parameters to consider for optimization are heater hysteresis, temperature offset, and the resistance of the house to heat loss. The parameter values for modeling and validating the model design were approximated and estimated.
Define simulation parameters, run simulation, and evaluate simulation results. See Basic Simulation Workflow: Run and Evaluate Simulation.
A Signal Builder block can import data from a Microsoft Excel worksheet into a Simulink model. The imported data is saved with the model. When you close and then open the model, the data is loaded into memory.
Open the model
use the model you prepared for importing data.
Double-click the Signal Builder block.
In the Signal Builder, select File > Import from File. Browse to
and select the file
use your data file.
Expand the Sheet1 node to view the data columns. Select the Sheet1 check box, which also selects the contents.
The Data to Import pane shows:
Excel file name
Worksheet in the file
Columns of data in the worksheet
From the Placement for Selected Data list,
Replace existing dataset. Click Confirm
Selection. Click OK.
Respond to the message that appears. Select No, import without saving.
View imported signals in the Signal Builder window. The figure shows the signal group as it appears in the Signal Builder plots.
Select Group > Rename and enter a group name.
For example, enter
Measured Data. Close the Signal
The Signal Builder block now has two output ports, one for each column of data imported from the worksheet.
In the model, reconnect the Outside Temperature port from the Signal Builder block to the signal line.
Add an Outport block above the Out1 block. Connect the Inside Temperature output from the Signal Builder block to this block.
Before running a simulation of your model, set simulation parameters. Simulation parameters to consider include the solver type and options for saving (logging) the simulation results.
Configure Model to Save Simulation Results. Configure your model to save (log) signal data during a simulation. You can view logged signals from each simulation using the Simulink Data Inspector.
In the model, select Simulation > Model Configuration Parameters. In the left pane, select Data Import/Export.
The data logging parameters appear in the right pane.
Clear the Time and Output check boxes.
Select the Signal logging check box.
Select the Record logged workspace data in Simulation Data Inspector check box.
Select Signals to Save. Identify signals to display in the Simulink Data Inspector, name the signals if they do not have a name, and set the logging parameters.
Right-click the Inside Temperature signal from the Signal Builder block and select Properties.
In the Signal name box,
Measured Room Temperature. Select the Log
signal data check box. A logging badge
above the signal line.
Name and select logging for these signals.
|Location of signal||Signal name|
|Outside Temperature from Signal Builder block|
|Room Temperature from Room Subsystem block|
After importing data and enabling logging of data for the signals, you can run a simulation.
Open the model
use the model you prepared for simulation.
In the model, open the Configuration Parameters dialog
box and select the Solver pane. Set Stop
24 (hours), Type to
and Solver to
Click the Run button to simulate the model.
The model simulation runs from
using the outside temperature data from the Signal Builder block as
Use the Simulink Data Inspector to compare the simulated output signals with measured data.
In the Simulink Editor toolbar, click the Simulation Data Inspector button .
A run appears in the Runs pane each time you simulate the model.
In the Simulink Data Inspector, select each signal check box. Selecting a signal plots the signal in the graph.
The top signal is the measured inside (room) temperature. The middle signal is the simulated room temperature. The bottom signal is the measured outside temperature.
One obvious change to the model is the hysteresis of the thermostat. The simulated room temperature oscillates between 18 and 22 degrees around the temperature set point of 20 degrees. The measured room temperature oscillates between 20 and 25 degrees with the same set point.
Open the Relay block in the Thermostat subsystem.
Change Switch on point from
the difference between the room temperature and set point is 0.
Change Switch off point from
When the room temperature is 5 degrees above the set point, you want
to turn the heater off. The set point is –5 degrees below the
Use the Simulation Data Inspector to compare differences between two simulations that use different model parameters.
Simulate the model.
Open the Simulation Data Inspector.
Select Run1 check boxes for
Outside Temperature and
Measured Room Temperature.
Select the Run2 check box for
Review the signals. The minimum and maximum values for the simulated room temperature now match the measured room temperature values.
Create reports and plots from the Simulation Data Inspector.
In the Simulation Data Inspector toolbar, click Create Report.
In the Create Report dialog box, select Inspect Signals. Click Create Report. The report opens in a web browser.
View the report.
In the Simulation Data Inspector, click Send to Figure. A plot of the simulated signals opens in a MATLAB figure window.