<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m0529"> 

<name>MATLAB difference equation</name>

<metadata>
  <md:version>2.5</md:version>
  <md:created>2000/08/09</md:created>
  <md:revised>2004/08/10 08:56:26.260 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dhj">
      <md:firstname>Don</md:firstname>
      
      <md:surname>Johnson</md:surname>
      <md:email>dhj@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="mrshawn">
      <md:firstname>Shawn</md:firstname>
      
      <md:surname>Stewart</md:surname>
      <md:email>mrshawn@alumni.rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>difference equation</md:keyword>
  </md:keywordlist>

  <md:abstract>How to write a simple looping difference equation in MATLAB.</md:abstract>
</metadata>
<content>


<para id="intro">
Difference equations are usually expressed in software with <term>for</term> loops.  A MATLAB program that would compute the first 1000 values of the output has the form
</para>

<code type="block">
  for n=1:1000
    y(n) = sum(a.*y(n-1:-1:n-p)) + sum(b.*x(n:-1:n-q));
  end
</code>

<para id="paratwo">
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
<m:math>
<m:apply>
  <m:ci type="fn">y</m:ci>
  <m:cn>1</m:cn>
</m:apply>
</m:math>
? We need values for 
<m:math>
<m:apply>
  <m:ci type="fn">y</m:ci>
  <m:cn>0</m:cn>
</m:apply>
<m:ci>,</m:ci>
<m:apply>
  <m:ci type="fn">y</m:ci>
  <m:cn>1</m:cn>
</m:apply>
<m:ci>,</m:ci>
<m:ci>…</m:ci>
</m:math>
, values we have not yet computed.  To compute them, we would need more previous values of the output, which we have not yet computed.  To compute these values, we would need even earlier values, ad infinitum.  The way out of this predicament is to specify the system's <term>initial conditions</term>:  we must provide the 
<m:math>
<m:ci>p</m:ci>
</m:math> 
output values that occurred before the input started.  These values can be arbitrary, but the choice does impact how the system responds to a given input.  <emphasis>One</emphasis> choice gives rise to a linear system:  Make the initial conditions zero.  The reason lies in the definition of a <cnxn document="m0508" strength="8">linear system</cnxn>:  The only way that the output to a sum of signals can be the sum of the individual outputs occurs when the initial conditions in each case are zero.
</para>

<exercise id="exer1">

<problem>
<para id="prob1">
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?"
</para>
</problem>

<solution>
<para id="soln1">
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.
</para>
</solution>

</exercise>


</content>
</document>
