Summary: How to write a simple looping difference equation in MATLAB.
Note: Your browser may not currently support MathML. See our browser support page for additional details. You can always view the correct math in the PDF version.
Difference equations are usually expressed in software with for loops. A MATLAB program that would compute the first 1000 values of the output has the form
for n=1:1000
y(n) = sum(a.*y(n-1:-1:n-p)) + sum(b.*x(n:-1:n-q));
end
An important detail emerges when we consider making this program work; in fact, as written it has (at least) two bugs. What input and output values enter into the computation of
The initial condition issue resolves making sense of the difference equation for inputs that start at some index. However, the program will not work because of a programming, not conceptual, error. What is it? How can it be "fixed?"
The indices can be negative, and this condition is not allowed in MATLAB. To fix it, we must start the signals later in the array.