In: Mechanical Engineering
Write and upload a MATLAB script to do the following.
Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ; Assume S(1) = 0, S(2) = 1;
(a) Case 1, Assume K = 1
(b) Case 2, Assume K = 2
(c) Case 3, Assume K = 4
Plot all of these on the same plot, for N = 1 to 20
k=1;
for n=3:1:22
s(3)=0;
s(4)=1;
s(n)=((2-k).*s(n-1))-s(n-2);
s(3)=0;
s(4)=1;
end
for i=1:1:20
x(i)=s(i+2)
end
n=[1:1:20];
plot(n,x)
hold all
k=2;
for n=3:1:22
s(3)=0;
s(4)=1;
s(n)=((2-k).*s(n-1))-s(n-2);
s(3)=0;
s(4)=1;
end
for i=1:1:20
y(i)=s(i+2)
end
n=[1:1:20];
plot(n,y)
hold all
k=4
for n=3:1:22
s(3)=0;
s(4)=1;
s(n)=((2-k).*s(n-1))-s(n-2);
s(3)=0;
s(4)=1;
end
for i=1:1:20
z(i)=s(i+2)
end
n=[1:1:20];
plot(n,z)