From ControlTheoryPro.com

Contents
1 Introduction to the Simple Offline Least Squares System Identification Example
Sometimes the best way to learn something is through a simple example. This simple example uses a continuous second order system as the system to be identified. The input/output relationship is saved in the Data Capture scope. There is a discrete model based on the least squares approximation. The final discrete transfer function block uses the transfer funciton returned by MATLAB's C2D function.
The results of the C2D function provide a discrete transfer function (via the Tustin Method) which is stable and provides similar results to that of the continuous time system. The least squares method provides a discrete transfer function good for interpolation of response points between measured points. However, the least squares method does not provide a discrete transfer function which is stable.
1.1 Refresher from Offline Least Squares
See Also 

Below is a refresher from the Offline Least Squares article. The equation shows the minimum amount of data required for a least squares solution but it also shows how to use the input and output to form the mathematics for a least squares solution. The minimum amount of required data results in
Eqn. 2 
2 Simple Simulink Model
Figure 1 shows a simple system identification model. The model show a second order transfer function for the model to ID. The input is random and noise is added to system output. The noisy output is captured by the Data Capture scope. The discrete version of the ID'd system is the Discrete Least Squares Model. And it's output along with the noisy output are captured in the Model Comparison scope.
Transfer Function Parameters
>> f = 100; >> w = f * (2*pi); >> z = 1/sqrt(2);
Using MATLAB's C2D Command the discrete model of the second order system is
>> a = tf([w^2], [1, 2*z*w, w^2]); >> b = c2d(a, 1/10000); 0.001916 z + 0.00186  z^2  1.911 z + 0.915
2.1 Forming the parameter estimates
The data from Data Capture scope is saved to the ParamID variable. From this variable we can form the parameter estimates. The input and output vectors are formed with the following code:
>> t = ParamID.time; >> y = ParamID.signals(2).values; >> r = ParamID.signals(1).values;
The least squares solution is formed using the following equation:
where
 is the input/output data,
 is the output and
 are the least squares parameters.
The input/output data relationship where A
where Y is formed with the following code
Y = []; for i = 1:order Y = [Y, y((1+i):(end(orderi)))]; end
and R is formed with the following code
R = []; for i = 1:order R = [R, r(i:end(orderi+1))]; end
The loop using order allows for an A matrix which is flexible. The size of the Y and R matrices are based on the order of the model you wish to form  in our case an order of 2. This type of formation assumes that there is plenty of excess data available. In this example I saved data at 10 kHz for 10 seconds and only require 4 data points.
The equation for finding the least squares parameter set is
We find the system parameters with the following MATLAB code
>> theta = pinv(A) * y;
3 Results
The results of the least squares parameter estimation is a discrete model good for interpolation. However, when the offline least squares solution is used online it does not perform well as can be seen in Figure 4.