Is it possible to solve difference equation in MATLAB?
Mostra commenti meno recenti
Can I solve this below difference equation using Matlab?
y(k)-3y(k-1)+2y(k-2)=2u(k-1)-2u(k-2)
u(k)=k when k>=0 and u(k)=0 when k<0
y(k)=0 when k<0
1 Commento
John D'Errico
il 8 Feb 2022
A cute homework problem. But you would find it difficult to convince me it is not HW. Anyway, I'll show a complete solution in time, at least if anyone cares.
Risposta accettata
Più risposte (2)
At least three ways this problem can be solved in Matlab that have been discussed in this thread. I thought it might be helpful to put them all together.
Restatement of the problem:
y(k)-3y(k-1)+2y(k-2)=2u(k-1)-2u(k-2)
u(k)=k when k>=0 and u(k)=0 when k<0
y(k)=0 when k<0
Option 1: Because the initial conditions on the output are zero and the input is causal, we can use filter(), exactly like @Tasin Nusrat did to solve for the first 11 outputs of y
k = 0:10;
a = [1 -3 2]; % left hand side of difference equation
b = [0 2 -2]; % right hand side of difference equation
u = k; % for k >=0
yfilter = filter(b,a,u).'; % yfilter(1) corresponds to k = 0, etc.
Option 2: Use a loop, credit to @Benjamin Thompson. This code could be cleaned up, but I think it has the benefit of being clear. Note that we have to account for Matlab's 1-based indexing.
yloop = nan(11,1);
u = @(k) (k.*(k>=0));
for kk = 0:10
uk1 = u(kk-1);
uk2 = u(kk-2);
if kk == 0
yk1 = 0;
yk2 = 0;
elseif kk == 1
yk1 = yloop(0 + 1);
yk2 = 0;
else
yk1 = yloop(kk-1 + 1);
yk2 = yloop(kk-2 + 1);
end
yloop(kk + 1) = 3*yk1 - 2*yk2 + 2*uk1 - 2*uk2;
end
syms k integer
syms z
% Transfer function from U to Y, by inspection
H(z) = (2/z - 2/z^2)/(1 - 3/z + 2/z^2);
% z-transform of U(z)
syms U(z)
U(z) = ztrans(k,k,z);
% Because the initial conditions on y are zero, and because u is causal, the z-transform of the output is
syms Y(z)
Y(z) = simplify(H(z)*U(z));
% closed form expression for y(k)
y(k) = simplify(iztrans(Y(z),z,k))
% make sure y(k) = 0 for k < 0
y(k) = piecewise(k<0, 0, y(k));
% evaluate
yclosed = double(y(0:10)).';
Verify all three solutions are the same:
k = (0:10).';
table(k, yfilter , yloop , yclosed)
We know that y = 0 for k < 0, so we can prepend yloop or yfilter with zeros to plot values on the negative axis if desired.
stem(-5:10,[zeros(1,5) yfilter.'])
Benjamin Thompson
il 7 Feb 2022
Modificato: Benjamin Thompson
il 7 Feb 2022
0 voti
Yes it is. Reorder terms so y(k) is on one side of the equation. Write a for loop in MATLAB to solve this numerically after you decide how many steps in k you want to solve for.
3 Commenti
Tasin Nusrat
il 7 Feb 2022
Benjamin Thompson
il 8 Feb 2022
You must solve it implicitly for values of k you are interested in. The sequence for u(k) is already defined per your original question. So lets say for 0 <= k < 10
y(0) = 0
y(1) = 3*y(0) - 2*y(-1) + 2*u(0) - 2*u(-1) = 0
y(2) = 3*y(1) - 2*y(0) + 2*u(1) - 2*u(0) = 2
y(3) = and repeat...
You can write a for loop in MATLAB or in many other programming languages to solve this for additional values of k.
John D'Errico
il 8 Feb 2022
Actually, you don't NEED to go to that extent. As I say in my answer, you can indeed use filter, or you can find an explicit functional form, though that took me a little thought to compute the actual coefficients of such a form. If you want to see how to use filter, I'll come back and show that.
Categorie
Scopri di più su Frequency Transformations in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
