I am trying to solve a difference equation with initial conditions using Matlab's filter function:

$$y[n]=\frac12y[n-1]+x[n],\qquad y[-1]=1\tag{1}$$

For $x[n]=6u[n]$, calculation of $y[n]$ by hand gives


However, the solution produced by filter is different. Could anybody point out my mistake?

The code is shown below:

disp('Solution of a difference equation:')
% The example difference equation considered is
% y(n) = 0.5y(n-1) + x(n) 
num_coef = [ 1 ]; % coefficient of x
den_coef = [1 -0.5]; % coefficient of y
n = 0:4; % Considering five samples 
x = 6 * ones(1,5) % x(n) = 6u(n)
init_cond = [1];  % y(-1) = 1
y = filter(num_coef, den_coef, x, init_cond) % Solution returned by 'filter'
yM = 12 - (11/2)*(1/2).^n % Solution obtained manually
plot(y);hold on; % PLots for comparison
  • $\begingroup$ Hi! So, how are the solutions different? What are these solutions? What is your manual calculation? With the info you're giving, it's hard to even guess, impossible to answer with confidence. $\endgroup$ Apr 30, 2019 at 8:07
  • $\begingroup$ Is your "manually obtained solution" a step response or an impulse response? Your test signal is a constant sequence of length 5, which will give you neither. If you want so see an impulse response, input a signal like [1,0,0,...]. If you want to see the step response, input a long sequence of ones. $\endgroup$
    – Florian
    Apr 30, 2019 at 8:16
  • $\begingroup$ @MarcusMüller: I agree that it would have been nicer to give a Latex formula for the manual solution, but note that the solution is given as yM in the code, so all information is there, just not in a greatly reader-friendly form. I think that despite all its weaknesses the question shouldn't be closed because it shows an important aspect of the commonly used Matlab function filter, namely that it's not correct to supply $y[-1]$ etc. as initial states. $\endgroup$
    – Matt L.
    Apr 30, 2019 at 8:40
  • $\begingroup$ I've edited the question accordingly. $\endgroup$
    – Matt L.
    Apr 30, 2019 at 8:49
  • 1
    $\begingroup$ Thank you all for organizing the question and answering it too... The filtic function is useful $\endgroup$
    – Prashant
    May 1, 2019 at 2:42

1 Answer 1


The problem is the definition of the initial condition of the function filter(). Note that according to the mathworks reference page, filter is implemented using a direct-form II transposed structure. If you look at the corresponding diagram, you see that the filter states (called $w_k(m)$ in the figure) are defined after multiplication with the respective filter coefficients.

So in your example if you want to initialize filter with some past output value $y[-1]$, your initial state is given by $y[-1]\cdot 0.5$ because that's the value of the input of the delay element.

y = filter(num_coef, den_coef, x, 0.5)

should give you the expected result.

Note that for the general case there's the function filtic, which computes the necessary initial conditions given past output samples (and, if necessary, past input samples). For your example you would use the command

zi = filtic(num_coef, den_coef, 1)

which results in zi = 0.5, as explained above.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.