Simulate a Dynamic System

Model Overview for This Tutorial

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.

Prepare Simulation

Add interfaces to input measured data, add control signals, and update model parameters. See Basic Simulation Workflow: Prepare for Simulation.

Determine Simulation Goals

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.

Collect Data

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.

  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. You can open ex_househeat_measured_data.xls for an example.

  3. 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 Model

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

  1. Open the model matlabroot/help/toolbox/simulink/examples/ex_househeat_modeling.slx or use the model you created in the tutorial Model a Dynamic System.

  2. Replace the In1 block with a Signal Builder block. The Signal Builder block imports data from a Microsoft Excel worksheet.

  3. Replace the In2 block with a Constant block and set the value to 20. The Constant block sets the thermostat temperature.

  4. Save the model.

Update Model Parameters

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.

Run and Evaluate Simulation

Define simulation parameters, run simulation, and evaluate simulation results. See Basic Simulation Workflow: Run and Evaluate Simulation.

Import Data

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.

  1. Open the model matlabroot\help\toolbox\simulink\examples\ex_househeat_simulation_import_data.slx or use the model you prepared for importing data.

  2. Double-click the Signal Builder block.

  3. In the Signal Builder, select File > Import from File. Browse to and select the file matlabroot\help\toolbox\simulink\examples\ex_househeat_measured_data.xls or use your data file.

  4. 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

  5. From the Placement for Selected Data list, select Replace existing dataset. Click Confirm Selection. Click OK.

  6. Respond to the message that appears. Select No, import without saving.

  7. View imported signals in the Signal Builder window. The figure shows the signal group as it appears in the Signal Builder plots.

  8. Select Group > Rename and enter a group name. For example, enter Measured Data. Close the Signal Builder.

    The Signal Builder block now has two output ports, one for each column of data imported from the worksheet.

  9. In the model, reconnect the Outside Temperature port from the Signal Builder block to the signal line.

  10. Add an Outport block above the Out1 block. Connect the Inside Temperature output from the Signal Builder block to this block.

Set Simulation Parameters

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.

  1. 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.

  2. 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.

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.

  1. Right-click the Inside Temperature signal from the Signal Builder block 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 Signal Builder blockMeasured Outside Temperature
    Room Temperature from Room Subsystem blockRoom Temperature

Run Simulation

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

  1. Open the model matlabroot\help\toolbox\simulink\examples\ex_househeat_simulation_prepared.slx or use the model you prepared for simulation.

  2. In the model, open the Configuration Parameters dialog box and select the Solver pane. Set Stop time to 24 (hours), Type to Variable-step, and Solver to ode45.

  3. Click the Run button to simulate the model.

    The model simulation runs from 0.0 to 24.00 hours using the outside temperature data from the Signal Builder block as input.

Compare Results with Measured 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 run appears in the Runs pane each time you simulate the model.

  2. 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.

Determine Changes to Model

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.

  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 the heater off. 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.

  1. Simulate the model.

  2. Open the Simulation Data Inspector.

  3. Select Run1 check boxes for Measured Outside Temperature and Measured Room Temperature. Select the Run2 check box for Room Temperature.

  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 Create Report.

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

  3. View the report.

  4. In the Simulation Data Inspector, click Send to Figure. A plot of the simulated signals opens in a MATLAB figure window.

More About

Was this topic helpful?