Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Using Matlab iterative solver

I was trying to use a Matlab Program to solve an iterative equation. I was having which was that I can't seem to set the values after the first two to 1. x=u[n], so x should be equal to 0 for -2 and -1, then till the end it should be equal to 1 (I think). I tried writing the following expressions, but it kept giving me errors on the Matrix dimensions.

n=(-2:10)'; y=[2;1; zeros(length(n)-2,1)];
%attempt #1
%u=ones(10,1);
%x=[0;0;u(3:end)];
%attempt #2
%if n<0
%   x=0;
%else
%   x=1;
%end
%attempt #3, which worked but is awkward to use
x=[0;0;1;1;1;1;1;1;1;1;1;1;1];
for k=1:length(n)-2,
y(k+2)=-3*y(k+1)-2*y(k)+x(k+2)+x(k);
end;
clf; 
stem(n,y,'k'); 
xlabel('n'); 
ylabel('y[n]');
disp('n        y'); 
disp([num2str([n,y])]);

Comments