30 lines
564 B
Matlab
30 lines
564 B
Matlab
% Define length of the sequence
|
|
N = 20;
|
|
|
|
% Preallocate output vector
|
|
x = zeros(N,1);
|
|
|
|
% Define input (unit step)
|
|
u = ones(N,1);
|
|
|
|
% Initial conditions
|
|
x(1) = 1; % x(0)
|
|
x(2) = 2; % x(1)
|
|
|
|
% Calculate the output iteratively using the given expression
|
|
for k = 2:N-1
|
|
x(k+1) = 2 + 4*u(k-1) - (0.5)^(k-1)*u(k-1);
|
|
end
|
|
|
|
% Create a vector for plotting
|
|
k = 0:N-1;
|
|
|
|
% Plot the resulting sequence
|
|
figure;
|
|
stem(k, x, 'filled');
|
|
grid on;
|
|
xlabel('Time step (k)');
|
|
ylim([0,10])
|
|
ylabel('x[k]');
|
|
title('Solution of the Difference Equation with Given Input and Initial Conditions');
|