What caused this error message?

---------------------------------------------------------------------------------------------------------------
% Define the input signal x[n]
n = 0:49;
x = (-1).^n .* (n >= 0);
% Define the length of the input signal
N = length(x);
% Initialize the output signal y[n]
y = zeros(1, N);
% Define the initial conditions
y_minus_1 = 4;
y_minus_2 = -2;
% Calculate the output y[n] using the difference equation
for n = 1:50
if n == 1
y(n) = x(n) + x(n - 1) - (1/4) * y_minus_1 + (1/8) * y_minus_2;
elseif n == 2
y(n) = x(n) + x(n - 1) - (1/4) * y(n - 1) + (1/8) * y_minus_1;
else
y(n) = x(n) + x(n - 1) - (1/4) * y(n - 1) + (1/8) * y(n - 2);
end
end
Array indices must be positive integers or logical values.
% Plot the output signal y[n]
stem(0:N-1, y);
grid on;
xlabel('n');
ylabel('y[n]');
title('Output of the System');
--------------------------------------------------------------------------------------------------------------------
Array index should be positive integer or logical value.
Error: HW2_2_59_c (18th line)
y(n) = x(n) + x(n - 1) - (1/4) * y_minus_1 + (1/8) * y_minus_2;

Answers (1)

In MATLAB, array indices start from 1, so attempting to access x(0) or y(0) would result in this error.
y(n) = x(n) + x(n - 1) - (1/4) * y_minus_1 + (1/8) * y_minus_2;
Here you are trying to access x(n-1) which is essentially x(0) when n=1.
This will throw the error stating array index should be positive or logical value. Make appropiate changes in your code to reolve this.
I hope this helps!

3 Comments

Exactly so. Since this happens only in this branch of the if clause:
if n == 1
y(n) = x(n) + x(n - 1) - (1/4) * y_minus_1 + (1/8) * y_minus_2;
it means @대선 neeeds to choose an appropriate boundary condition for the case where n==1, since x(0) is not accesible in MATLAB. However, @대선 has this information in hand, in the form of:
x = (-1).^n .* (n >= 0);
There we see that when n==0, we would have
x(0) = (-1)^0 = 1
This provides the boundary condition to apply, replacing x(n-1) with 1 in that initial branch.
if n == 1
y(n) = x(n) + 1 - (1/4) * y_minus_1 + (1/8) * y_minus_2;
Thank you!
But now I have another eror message..
What does it mean that index exceeds 1..??
Index exceeds the number of array elements. Index should not exceed 1.
Error: HW2_2_59_c (in line 14)
y(n) = x(n) + x(n - 1) - (1/4) * y(n - 1) + (1/8) * y_minus_1;
Hi @대선, when are you facing this error?
As rightly mentioned by @John D'Errico, you can pre-compute x(0)=1 and calcualte y(1).
y(1)=x(1)+x(0)-(1/4)* y_minus_1 + (1/8) * y_minus_2;
You can later have the limit of for loop from n=2:50
for n = 2:50
if n == 2
y(n) = x(n) + x(n - 1) - (1/4) * y(n - 1) + (1/8) * y_minus_1;
else
y(n) = x(n) + x(n - 1) - (1/4) * y(n - 1) + (1/8) * y(n - 2);
end
end
Check if this is what you intend on doing.

Sign in to comment.

Categories

Find more on Software Development in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 24 Apr 2024

Commented:

Raj
on 25 Apr 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!