Main Content

## Simulate Models with Arbitrary Inputs and Initial Conditions

### What is the Linear Simulation Tool?

You can use the Linear Simulation Tool to simulate linear models with arbitrary input signals and initial conditions.

The Linear Simulation Tool lets you do the following:

• Import input signals from the MATLAB® workspace.

• Import input signals from a MAT-file, Microsoft® Excel® spreadsheet, ASCII flat-file, comma-separated variable file (CSV), or text file.

• Generate arbitrary input signals in the form of a sine wave, square wave, step function, or white noise.

• Specify initial states for state-space models.

Default initial states are zero.

### Opening the Linear Simulation Tool

To open the Linear Simulation Tool, do one of the following:

• In the Linear System Analyzer, right-click the plot area and select Plot Types > Linear Simulation.

• Use the `lsim` function at the MATLAB prompt:

```lsim(modelname) ```
• In the MATLAB Figure window, right-click a response plot and select Input data.

### Working with the Linear Simulation Tool

The Linear Simulation Tool contains two tabs, Input signals and Initial states.

After opening the Linear Simulation Tool (as described in Opening the Linear Simulation Tool), follow these steps to simulate your model:

1. Click the Input signals tab, if it is not displayed. 2. In the Timing area, specify the simulation time vector by doing one of the following:

• Import the time vector by clicking Import time.

• Enter the end time and the time interval in seconds. The start time is set to `0` seconds.

3. Specify the input signal by doing one of the following:

4. If you have a state-space model and want to specify initial conditions, click the Initial states tab. By default, all initial states are set to zero.

You can either enter state values in the Initial value column, or import values by clicking Import state vector. For more information about entering initial states, see Specifying Initial Conditions. 5. For a continuous model, select one of the following interpolation methods in the Interpolation method list to be used by the simulation solver:

• Zero order hold

• First order hold (linear interpolation)

• Automatic (Linear Simulation Tool selects first order hold or zero order hold automatically, based on the smoothness of the input)

Note

The interpolation method is not used when simulating discrete models.

6. Click Simulate.

### Importing Input Signals

You can import input signals from the MATLAB workspace after opening the Linear Simulation Tool (see Opening the Linear Simulation Tool). You can also import inputs from a MAT-file, Microsoft Excel spreadsheet, ASCII flat-file, comma-separated variable file (CSV), or text file.

For information about creating your own inputs, see Designing Input Signals. For an overview of working with the Linear Simulation Tool, see Working with the Linear Simulation Tool.

To import one or more input signals:

1. In the Linear Simulation Tool, click the Input signals tab, if it is not displayed.

2. Specify the simulation time in the Timing area.

3. Select one or more rows for the input channels you want to import. The following figure shows an example with two selected channels. 4. Click Import signal to open the Data Import dialog box. The following figure shows an example of the Data Import dialog box. 5. In the Import from list, select the source of the input signals. It can be one of the following:

• `Workspace`

• `MAT file`

• `XLS file`

• `CSV file`

• `ASCII file`

6. Select the data you want to import. The Data Import dialog box contains different options depending on which source format you selected.

7. Click Import.

For an example of importing input signals, see the following:

### Example: Loading Inputs from a Microsoft Excel Spreadsheet

To load inputs from a Microsoft Excel (XLS) spreadsheet:

1. In the Linear Simulation Tool, click Import signal in the Input signals tab to open the Data Import dialog box.

2. Select XLS file in the Import from list.

3. Click Browse.

4. Select the file you want to import and click Open. This populates the Data Import dialog box with the data from the Microsoft Excel spreadsheet. ### Example: Importing Inputs from the Workspace

To load an input signal from the MATLAB workspace:

1. Enter this code to open a response plot with a second-order system:

```s=tf('s'); ss=(s+2)/(s^2+3*s+2); lsim(ss,randn(100,1),1:100); ```
2. Right-click the plot background and select Input data. This opens the Linear Simulation Tool with default input data. 3. Create an input signal for your system in the MATLAB Command Window, such as the following:

```new_signal=[-3*ones(1,20) 2*ones(1,30) 0.5*ones(1,50)]'; ```
4. In the Linear Simulation Tool, click Import signal.

5. In the Data Import dialog box, click, Assign columns to assign the first column of the input signal to the selected channel. 6. Click Import. This imports the new signal into the Linear Simulation Tool. 7. Click Simulate to see the response of your second-order system to the imported signal. ### Designing Input Signals

You can generate arbitrary input signals in the form of a sine wave, square wave, step function, or white noise after opening the Linear Simulation Tool (see Opening the Linear Simulation Tool).

For information about importing inputs from the MATLAB workspace or from a file, see Importing Input Signals. For an overview of working with the Linear Simulation Tool, see Working with the Linear Simulation Tool.

To design one or more input signals:

1. In the Linear Simulation Tool, click the Input signals tab (if it is not displayed).

2. Specify the simulation time in the Timing area. The time interval (in seconds) is used to evaluate the input signal you design in later steps of this procedure.

3. Select one or more rows for the signal channels you want to design. The following figure shows an example with two selected channels. 4. Click Design signal to open the Signal Designer dialog box. The following figure shows an example of the Signal Designer dialog box. 5. In the Signal type list, select the type of signal you want to create. It can be one of the following:

• `Sine wave`

• `Square wave`

• `Step function`

• `White noise`

6. Specify the signal characteristics. The Signal Designer dialog box contains different options depending on which signal type you selected.

7. Click Insert. This brings the new signal into the Linear Simulation Tool.

8. Click Simulate in the Linear Simulation Tool to view the system response.

### Specifying Initial Conditions

If your system is in state-space form, you can enter or import initial states after opening the Linear Simulation Tool (see Opening the Linear Simulation Tool).

For an overview of working with the Linear Simulation Tool, see Working with the Linear Simulation Tool.

You can also import initial states from the MATLAB workspace.

To import one or more initial states:

1. In the Linear Simulation Tool, click the Initial states tab (if it is not already displayed).

2. In the Selected system list, select the system for which you want to specify initial conditions.

3. You can either enter state values in the Initial value column, or import values from the MATLAB workspace by clicking Import state vector. The following figure shows an example of the import window: Note

For n-states, your initial-condition vector must have n entries.

4. After specifying the initial states, click Simulate in the Linear Simulation Tool to view the system response.

## Support

#### Learn how to automatically tune PID controller gains

Download code examples