MATLAB: Do I get different results with RESID and FILTER while using System Identification Toolbox 7.1 (R2007b)

System Identification Toolbox

I have the following code and am expecting the results of e and e1 to be same but the first element is not same. The reasoning behind this expectation is as follows:
RESID should be performing an inverse filter of the one that is estimated by the model. That is what I am trying to create by using the filter function. However, the resid probably is using or most likely assuming some Zi for the filter. No matter what it is doing one should be able to recreate what resid is doing using the filter command.
Reproduction steps:
x=filter([1],[1 -.9],randn(1,100));
m=armax(x',[1 0])
e=filter(m.a,m.c,x);
e1=resid(m,x')
If you plot e and e1 you will see that other than the first element, they are essentially the same.

Best Answer

This is an expected behavior in System Identification Toolbox 7.1(R2007b). The reason for this behavior is as follows:
m is a dynamic model with one state. So any simulation or prediction calculation requires prescription of initial conditions. When one uses FILTER directly, one is implicitly setting the initial conditions (x0) to zero. Hence with FILTER, the first "error" value is x(1)-x0 = x(1).
However, commands such as PE and RESID estimate initial conditions in such a way that the overall prediction errors are minimized. While RESID does not allow one to change the default handling of initial conditions, PE does. One can enforce zero initial conditions by using:
e2 = pe(m, x', 'init', 'z'); % set initial condition to zero
Then e2 would be same as e. However, if you type:
e3 = pe(m, x') % default is to estimate ( = 'e') initial conditions
or,
[e3, x0] = pe(m, x', 'init', 'e');
one sees that e3 is same as output of RESID e1. Also, x0 is calculated to be equal to x(1), which leads to zero value for e3(1).