To use **MATLAB’s System Identification Tool** with **Aspen HYSYS dynamic data**, follow these steps to export, import, and analyze the data for model identification. This is commonly done for creating **FOPDT**, **state-space**, or **transfer function** models from HYSYS simulation data.

---

## 🔁 Step-by-Step Guide

### ✅ Step 1: Generate Dynamic Data in Aspen HYSYS

1. Open your dynamic simulation in **Aspen HYSYS**.
2. Apply a **step input** or any test signal (e.g., PRBS) to a manipulated variable (e.g., valve position, setpoint).
3. Record the **output response** from the controlled variable (e.g., temperature, pressure).
4. Use the **Data Recorder**:

   * Go to `Tools > Data Recorder`.
   * Select the variables you want to log (inputs/outputs).
   * Start recording during your simulation.
   * Export the data as a **CSV file**.

---

### ✅ Step 2: Import Data into MATLAB

1. Open MATLAB.
2. Launch the **System Identification Tool**:

   ```matlab
   ident
   ```
3. In the GUI:

   * Go to `File > Import Data > From Workspace` (or choose “From file” if you want to directly load the CSV).
   * If loading from a script:

     ```matlab
     data = readtable('hysys_data.csv');
     u = data.InputVariableName;
     y = data.OutputVariableName;
     Ts = 1; % Sampling time, adjust based on HYSYS export settings
     iddata_obj = iddata(y, u, Ts);
     ident(iddata_obj);
     ```

---

### ✅ Step 3: Identify a Model

1. In the System Identification Tool, load your `iddata` object.
2. Choose model types:

   * **ARX**, **ARMAX**, **OE**, or **State-space**.
   * Or use **Transfer Function** (FOPDT) structure.
3. Estimate the model and validate:

   * Use **Compare** to view fit between model and actual data.
   * Use **residuals** to assess model quality.

---

### ✅ Step 4: Export and Use the Model

* You can export the identified model to MATLAB workspace:

  ```matlab
  sys = tfest(iddata_obj, 1, 1); % Example: 1 pole, 1 zero
  ```

* Use the model for **simulation**, **controller design**, or further **analysis**.

---

## 🔍 Notes

* Ensure **sampling time** in MATLAB matches the data resolution from HYSYS.
* You may need to **normalize** or **detrend** data to improve identification accuracy.
* Use **multi-variable data** if you’re modeling MIMO systems.

---

Would you like a sample MATLAB script for processing a HYSYS CSV file and identifying a FOPDT model?
Here’s a **complete MATLAB example** showing how to:

1. **Load dynamic data** exported from **Aspen HYSYS**.
2. Create an `iddata` object.
3. Identify a **FOPDT (First-Order Plus Dead Time)** transfer function model using `tfest`.
4. **Plot and compare** model output vs. actual data.

---

## 🧾 1. Sample HYSYS Export Data (CSV Format)

Assume you exported a file `hysys_data.csv` with the following columns:

```
Time, InputValve, ReactorTemp
0, 20, 250
1, 22, 251
2, 25, 253
...
```

---

## 🧪 2. MATLAB Script for System Identification

```matlab
% Load HYSYS data
data = readtable('hysys_data.csv');

% Extract input and output
t = data.Time; % Time vector
u = data.InputValve; % Manipulated variable (e.g., valve position)
y = data.ReactorTemp; % Controlled output (e.g., reactor temperature)

% Estimate sampling time (assumes uniform sampling)
Ts = t(2) - t(1);

% Create IDDATA object
data_id = iddata(y, u, Ts);

% Plot raw data
figure;
subplot(2,1,1); plot(t, u); title('Input: Valve Position'); ylabel('u(t)');
subplot(2,1,2); plot(t, y); title('Output: Reactor Temperature'); ylabel('y(t)'); xlabel('Time (s)');

% Estimate FOPDT transfer function model (1 pole, 0 zero)
np = 1; % number of poles
nz = 0; % number of zeros
sys_fopdt = tfest(data_id, np, nz);

% Display result
disp('Identified Transfer Function Model:');
sys_fopdt

% Simulate model output
[y_sim, ~] = sim(sys_fopdt, u);

% Compare actual vs. simulated output
figure;
plot(t, y, 'b', 'DisplayName', 'Actual Output');
hold on;
plot(t, y_sim, 'r--', 'DisplayName', 'FOPDT Model Output');
legend; title('Model Validation'); xlabel('Time (s)'); ylabel('Output');
grid on;
```

---

## 📈 3. Sample Output (Assumed Result)

Let’s say the model output is:

```matlab
Identified Transfer Function Model:
         2.31
---------------------
s + 0.045, with delay = 3.5 sec
```

This means:

* **Gain (K) = 2.31**
* **Time constant (τ) = 1/0.045 ≈ 22.2 sec**
* **Dead time (L) = 3.5 sec**

---

## 🧩 Interpretation

This FOPDT model represents how the reactor temperature responds to changes in valve position. It can now be used for:

* **PID tuning** (e.g., with Ziegler-Nichols or IMC tuning).
* **Controller simulation** using MATLAB Simulink.
* Further system analysis.

---

Would you like a version of this in Simulink or an example for **MIMO system identification** as well?
Great! Let's walk through a **MIMO (Multiple-Input, Multiple-Output)** system identification example in MATLAB using the **System Identification Toolbox**.

---

## 🎯 Scenario

Suppose we have a **2-input, 2-output** process exported from **Aspen HYSYS**. For example:

* **Inputs:**

  * `u1`: Valve 1 position (InputValve1)
  * `u2`: Valve 2 position (InputValve2)

* **Outputs:**

  * `y1`: Reactor temperature (ReactorTemp)
  * `y2`: Reactor pressure (ReactorPressure)

These are recorded in a CSV file `hysys_mimo.csv`:

```
Time, InputValve1, InputValve2, ReactorTemp, ReactorPressure
0, 20, 30, 250, 101
1, 22, 30, 251, 102
2, 25, 32, 253, 103
...
```

---

## 🧪 MATLAB Script for MIMO System Identification

```matlab
% Load CSV data
data = readtable('hysys_mimo.csv');

% Extract time and variables
t = data.Time;
u1 = data.InputValve1;
u2 = data.InputValve2;
y1 = data.ReactorTemp;
y2 = data.ReactorPressure;

% Estimate sampling time
Ts = t(2) - t(1);

% Construct input and output matrices
U = [u1, u2];   % 2 inputs
Y = [y1, y2];   % 2 outputs

% Create iddata object for MIMO
data_id = iddata(Y, U, Ts);

% Plot the data
figure;
subplot(2,1,1); plot(t, U); title('Inputs'); legend('Valve 1', 'Valve 2');
subplot(2,1,2); plot(t, Y); title('Outputs'); legend('Temp', 'Pressure');

% Estimate a state-space MIMO model
nx = 4; % Number of states (tune as needed)
sys_mimo = ssest(data_id, nx);

% Display model summary
disp('Identified MIMO State-Space Model:');
sys_mimo

% Validate with prediction
figure;
compare(data_id, sys_mimo);
```

---

## 📈 Output Interpretation

`ssest` produces a **state-space model**:

$$
\begin{align*}
\dot{x}(t) &= A x(t) + B u(t) \\
y(t) &= C x(t) + D u(t)
\end{align*}
$$

Example output:

```matlab
A = [...], B = [...], C = [...], D = [...]
Inputs: u1, u2
Outputs: y1, y2
```

This model captures the dynamic interaction between multiple inputs and outputs, including cross-coupling.

---

## ✅ Optional: Transfer Function Estimation for Each Path

If you want **individual transfer functions** (e.g., `y1/u1`, `y1/u2`, etc.):

```matlab
sys_tf = tfest(data_id, 2, 1); % 2 poles, 1 zero per transfer function
sys_tf
```

This gives a **2×2 transfer function matrix**:

$$
\begin{bmatrix}
G_{11}(s) & G_{12}(s) \\
G_{21}(s) & G_{22}(s)
\end{bmatrix}
$$

Where:

* $G_{11}(s)$: effect of u1 on y1
* $G_{12}(s)$: effect of u2 on y1
* $G_{21}(s)$: effect of u1 on y2
* $G_{22}(s)$: effect of u2 on y2

---

## 📚 Summary

* Use `iddata` to package your MIMO data.
* Use `ssest` for a **state-space model**, or `tfest` for a **transfer function matrix**.
* Use `compare()` to validate model performance.

Would you like to see how to use this model for **MIMO PID controller design** or simulation in **Simulink**?
