Simulate a Dynamic System

This tutorial shows how to simulate the model of a dynamic system using Simulink® software, and then use the results to improve the model. After you prepare the model for simulation, you can use an interface to input measured system data and set room temperature.

To review an example model that is already prepared for simulation, in the MATLAB® Command Window, enter

'help', 'toolbox', 'simulink', 'examples', 'ex_househeat_measured_data.mat'))
'help', 'toolbox', 'simulink', 'examples', 'ex_househeat_simulation_prepared'))

Prepare for Simulation

Verify that a simulation represents the behavior of the system you modeled. Begin by experimentally measuring physical characteristics of the system that have comparable signals in your model:

  • Collect data from physical system

  • Prepare model for simulation

For a detailed description of the workflow, see Prepare for Simulation.

Collect and Plot System Data

Measure the dynamic characteristics from an actual house heating system. You will use the measured data with model simulations to verify the behavior and accuracy of your model.

  1. Measure the outside and inside temperatures of a house every 6 minutes for 24 hours.

  2. Enter the measured data into a Microsoft® Excel® worksheet or open an example spreadsheet. In the MATLAB Command Window, enter

    'help', 'toolbox', 'simulink', 'examples', 'ex_househeat_measured_data.xls'))

  3. Review a plot of the measured data. 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 Model for Simulation

Prepare a model for simulation by adding an external interface for data input and input control signals.

  1. Use the model you created in the tutorial Model a Dynamic System or open the example model. In the MATLAB Command Window, enter

    'help', 'toolbox', 'simulink', 'examples', 'ex_househeat_modeling'))

  2. Replace the Inport block In2 with a Constant block and set the Constant parameter to 20. The Constant block sets the thermostat temperature.

  3. Add an Inport block. Set Port number to 1. This action also sets the Port number of the outside temperature signal to 2.

  4. Rename the first Inport block to Inside Temperature. Rename the second Inport block to Outside Temperature.

  5. Add an Outport block and connect it to the first Inport block (Inside Temperature). The outport blocks are needed for saving (logging) the signals.

Run and Evaluate Simulation

Verify the accuracy of the model and optimize parameters. Some parameters to consider for optimization are heater hysteresis, temperature offset, and the resistance of the house to heat loss. Follow these steps to verify your model:

  • Import Data

  • Run Simulation

  • Evaluate Simulation Result

  • Change Model Parameters

  • Rerun Simulation

For a detailed description of the workflow, see Run and Evaluate Simulation for the Basic Simulation Workflow.

Import Data with Root Inport Mapping

You can use the Root Inport Mapper tool to bring measured signal data from an Excel spreadsheet into a Simulink model.

  1. Open any Inport block. Click the Connect Input button to open the Root Inport Mapper.

  2. On the toolstrip, click From Spreadsheet.

  3. In the From Spreadsheet dialog box, click the browse button. Browse to and select the file matlabroot\help\toolbox\simulink\examples\ex_househeat_measured_data.xls. Click Open. Click OK to import the spreadsheet.

  4. From the Signals drop-down list, select Preview Signals.

  5. On the left side, expand the tree view of Sheet1. Select the Inside Temperature and Outside Temperature check boxes.

  6. Click Close Signal Preview.

  7. On the left side, select Sheet1. The Scenario Signal column shows the two signals from the Excel spreadsheet and an icon indicating the signals are unmapped.

  8. On the toolstrip, select the Port Order option. From the Options drop-down list, select the Update Model check box.

  9. From the Map to Model drop-down list, select Map Uncennected. The mapping summary shows the signals from the Excel spreadsheet mapped to the Input port blocks.

  10. Click Mark for Simulation. The mapping summary shows Sheet1 is marked for simulation and a Dataset object is created in the MATLAB Workspace.

  11. Save the signal data in a MAT-file. In the MATLAB Command Window, type

    save('ex_househeat_measured_data.mat', 'Sheet1')

Configure Model to Load Signal Data

Signal data mapped to input ports is located in a MATLAB workspace variable. With each new MATLAB session, you have to manually reload the data or let the model preload function do it for you.

  1. From the Simulink Editor menu, select File > Model Properties > Model Properties.

  2. Select the Callbacks tab.

  3. In the Model callbacks section, select PreLoadFcn.

  4. In the Model pre-load function box, enter


  5. Click OK.

Configure Model to Save Simulation Results

Configure your model to save (log) signal data during a simulation. You can then view logged signals from a simulation using the Simulink Data Inspector.

  1. In the model, select Simulation > Model Configuration Parameters. In the left pane, select Data Import/Export.

  2. In the right pane, clear the Time and Output check boxes.

  3. Select the Signal logging check box.

  4. Select the Record logged workspace data in Simulation Data Inspector check box.

  5. Click OK.

Select Signals to Save

Identify signals to display in the Simulink Data Inspector, name the signals if they are unnamed, and set the logging parameters.

  1. Right-click the Inside Temperature signal line and select Properties.

  2. In the Signal name box, enter Measured Room Temperature. Select the Log signal data check box. A logging badge appears above the signal line.

  3. Name and select logging for these signals.

    Location of signalSignal name
    Outside Temperature from output port 2.Measured Outside Temperature
    Room Temperature from Room subsystem output portRoom Temperature

Run Simulation

After importing data and enabling logging of data for the signals, you can run a simulation.

  1. Use the model you prepared for simulation or open the example model. In the MATLAB Command Window, enter

    'help', 'toolbox', 'simulink', 'examples', 'ex_househeat_simulation_prepared'))

  2. In the model, select Simulation > Model Configuration Parameters. In the left pane, select Solver. Set Stop time to 24 (hours), Type to Variable-step, and Solver to ode45. Click OK.

  3. Click the Run button .

    The model simulation runs from 0.0 to 24.0 hours using the outside temperature data from the root import block as input.

Compare Simulation Results with Measured System Data

Use the Simulink Data Inspector to compare the simulated output signals with measured data.

  1. In the Simulink Editor toolbar, click the Simulation Data Inspector button .

    A separate run appears in the Runs pane each time you simulate the model.

  2. Select all the signal check boxes. As you select a signal, a plot is drawn on the graph.

    The top signal is the Measured Room Temperature. The middle signal is the simulated Room Temperature. The bottom signal is the Measured Outside Temperature.

Determine Changes to Model

One obvious change to the model is the hysteresis of the thermostat. The simulated room temperature oscillates 18–22 degrees around the temperature set point of 20 degrees. The measured room temperature oscillates 20–25 degrees with the same set point.

  1. Open the Relay block in the Thermostat subsystem.

  2. Change Switch on point from 2 to 0 because the difference between the room temperature and set point is 0.

  3. Change Switch off point from -2 to -5. When the room temperature is 5 degrees above the set point, you want to turn off the heater. The set point is –5 degrees below the room temperature.

Compare Results Between Simulations

Use the Simulation Data Inspector to compare differences between two simulations that use different model parameters. This comparison shows how changes improve the accuracy of your model.

  1. Simulate the model.

  2. Open the Simulation Data Inspector.

  3. Expand the list of logged signals by selecting the arrow to the left of the run. For Run1, select the Measured Outside Temperature and Measured Room Temperature check boxes. For Run2, select the Room Temperature check box.

  4. Review the signals. The minimum and maximum values for the simulated room temperature now match the measured room temperature values.

Report and Plot Simulation Results

Create reports and plots from the Simulation Data Inspector.

  1. In the Simulation Data Inspector toolbar, click the Report button.

  2. In the Create Report dialog box, select Inspect Signals. Click Create Report. The report opens in a web browser.

  3. View the report.

Related Topics

Was this topic helpful?