I am trying to solve a difference equation with initial conditions using Matlab's
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 = ; % 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 plot(yM,'r')