Difference Equation in Matlab

Discussion in 'Programmer's Corner' started by mad12, Feb 11, 2009.

  1. mad12

    Thread Starter Member

    Oct 9, 2008
    Hello everyone,

    I have a few Matlab problems for my class that I could use some help on. The problem is the following:
    Searching around it seemed to me like Matlab could not calculate the y[0] value because it didnt like the 0 index. I havent done Matlab in a while so I'm not sure if that is the case or not. To overcome this, I calculated the y[1] and y[2] values and used these in a for loop. My Matlab m file looked like this:

    Code ( (Unknown Language)):
    1. n=[1:30];
    2. y(1) = 6.16;
    3. y(2) = 8.77;
    4. for m=3:30;
    5.     y(m)=0.1*y(m-1)+.72*y(m-2)+5;
    6. end
    7. stem(n,y,'o')
    When I ran this it gave me the attached output. I also noticed it didnt give me the value at 0 as well. I was wondering if someone could help me figure out how to implement this recursively in Matlab, assuming what I have in incorrect. Thanks!
    Last edited: Feb 11, 2009
  2. mad12

    Thread Starter Member

    Oct 9, 2008
    Never mind I figured it out. If anybody is wondering what the solution is, I just had to hand calculate the first 2 y values, and then used these initial values to solve it recursively. My code looked like:

    Code ( (Unknown Language)):
    2. [COLOR=black][FONT=&quot]n=[1:30];[/FONT][/COLOR]
    3. [COLOR=black][FONT=&quot]y(1) = 4.38;[/FONT][/COLOR]
    4. [COLOR=black][FONT=&quot]y(2) = 1.158;[/FONT][/COLOR]
    5. [COLOR=blue][FONT=&quot]for[/FONT][/COLOR][COLOR=black][FONT=&quot] m=3:30;[/FONT][/COLOR]
    6.   [COLOR=black][FONT=&quot]  y(m)=0.1*y(m-1)+.72*y(m-2);[/FONT][/COLOR]
    7. [COLOR=blue][FONT=&quot]end[/FONT][/COLOR]
    8. [COLOR=black][FONT=&quot]stem(n,y,[/FONT][/COLOR][COLOR=#a020f0][FONT=&quot]'o'[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
    9. [COLOR=black][FONT=&quot]xlabel([/FONT][/COLOR][COLOR=#a020f0][FONT=&quot]'n'[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]
    10. [COLOR=black][FONT=&quot]ylabel([/FONT][/COLOR][COLOR=#a020f0][FONT=&quot]'x[n]'[/FONT][/COLOR][COLOR=black][FONT=&quot])[/FONT][/COLOR]