# Difference equation with variable coefficients in MATLAB

Given the following difference equation

$$y[n]-\left(\dfrac{n}{n+1}\right)y[n-1] = n\cdot x[n]$$

How can we use MATLAB to solve it?

I know if the coefficients are constant we can simply use filter(b, a,x), but not sure how to do it with variable coefficients.

• A loop would be a good idea... I mean, you can find $y[n]$ iteratively.
– GKH
Jan 31 '20 at 5:36
• Thanks, any sample code/algorithm you could recommend? Jan 31 '20 at 19:45
• Well, in Octave I would do this: y(1) = -1; x = randn(1,100); for n = 2:100 y(n) = n*x(n) + (n/(n+1))*y(n-1); end
– GKH
Jan 31 '20 at 21:03

When $$n=0$$ you may need to know $$y[-1]$$. The current output $$y(n)$$ depends on the current input $$x(n)$$ and previous output $$y(n-1)$$ scaled.
• The input sequence $x[n]$ is known then $y[n]$ can be computed with $$y[n]= n\cdot x[n] + \left(\dfrac{n}{n+1}\right)y[n-1]$$ when $n=0$ use $y[-1]$ if available otherwise $y[-1]=0$ Do you need a MATLAB code example to solve above problem? But, I guess its not that complicated to implement. Jan 31 '20 at 19:51
• Well for $n=0;\, y[0] = 0$ nevertheless. Jan 31 '20 at 19:55