# Array indices must be positive integers or logical values.

2 Ansichten (letzte 30 Tage)
John am 5 Sep. 2023
Beantwortet: Davide Masiello am 5 Sep. 2023
nMax = 100; % Maximum value of n
tol = 1e-5; % Tolerance value
% Initialize arrays to store the values of eta
eta1 = zeros(nMax, 1);
eta2 = zeros(nMax, 1);
% Compute the values of eta for different values of n
for n = 1:nMax
% Compute eta for the first sequence
if n == 1
eta1(n) = 0; % Initial condition for eta1
else
eta1(n) = (1/(n+1))*(eta1(n-1)/2 + (n-1)/(n+1)*eta1(n-1) + 1/(n+1)*(1/(n*(n+1)))*1*(eta1(n-1)/4));
end
% Compute eta for the second sequence
if n == 1
eta2(n) = 0; % Initial condition for eta2
else
eta2(n) = (1/(n+1))*(eta2(n-1)/2 + (n-1)/(n+1)*((eta2(n-1)/2)/2) + 1/(n+1)*(1/(n*(n+1)))*1*(eta2(n-1)/4));
end
% Check convergence condition
if norm(eta1(n) - eta1(n-1)) < tol
break;
if norm(eta2(n) - eta2(n-1)) < tol
break; % Convergence reached, stop the loop
end
end
end
% Plot the convergence behavior
plot(1:n, eta1(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 1');
hold on;
plot(1:n, eta2(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 2');
hold off;
xlabel('n');
ylabel('\eta');
title('Convergence Behavior of Sequences');
legend('Location', 'best');
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (2)

Dyuman Joshi am 5 Sep. 2023
if norm(eta1(n) - eta1(n-1)) < tol
break;
if norm(eta2(n) - eta2(n-1)) < tol
break; % Convergence reached, stop the loop
end
end
When n==1, n-1==0, which can not be used as an index in MATLAB.
Modified code -
nMax = 100; % Maximum value of n
tol = 1e-5; % Tolerance value
% Initialize arrays to store the values of eta
eta1 = zeros(nMax, 1);
eta2 = zeros(nMax, 1);
%%eta1 and eta2 are preallocated as zeros array, thus assigning 0 for n==1 is redundant
%%Start the loop from n==2, it also takes care of the error
for n = 2:nMax
%%Combine the conditions
% Compute eta for the first sequence
eta1(n) = (1/(n+1))*(eta1(n-1)/2 + (n-1)/(n+1)*eta1(n-1) + 1/(n+1)*(1/(n*(n+1)))*1*(eta1(n-1)/4));
eta2(n) = (1/(n+1))*(eta2(n-1)/2 + (n-1)/(n+1)*((eta2(n-1)/2)/2) + 1/(n+1)*(1/(n*(n+1)))*1*(eta2(n-1)/4));
% Check convergence condition
%%Combine the check for convergence
if norm(eta1(n) - eta1(n-1)) < tol || norm(eta2(n) - eta2(n-1)) < tol
break;
end
end
n
% Plot the convergence behavior
plot(1:n, eta1(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 1');
hold on;
plot(1:n, eta2(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 2');
hold off;
xlabel('n');
ylabel('\eta');
title('Convergence Behavior of Sequences');
legend('Location', 'best');
However, as the 1st values of eta1 and eta2 are zero, the rest of the values will also be zero, as the value of term n is directly proportional to the value of term (n-1) (observed from the formula/expression above).
Are you sure the inital values of eta1 and eta2 are zero?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Davide Masiello am 5 Sep. 2023
The problem is in the convergence check step, where you try to evaluate eta at n-1 even when n = 1.
Adding n > 1 as additional statement solves the problem. See below.
nMax = 100; % Maximum value of n
tol = 1e-5; % Tolerance value
% Initialize arrays to store the values of eta
eta1 = zeros(nMax, 1);
eta2 = zeros(nMax, 1);
% Compute the values of eta for different values of n
for n = 1:nMax
% Compute eta for the first sequence
if n == 1
eta1(n) = 0; % Initial condition for eta1
else
eta1(n) = (1/(n+1))*(eta1(n-1)/2 + (n-1)/(n+1)*eta1(n-1) + 1/(n+1)*(1/(n*(n+1)))*1*(eta1(n-1)/4));
end
% Compute eta for the second sequence
if n == 1
eta2(n) = 0; % Initial condition for eta2
else
eta2(n) = (1/(n+1))*(eta2(n-1)/2 + (n-1)/(n+1)*((eta2(n-1)/2)/2) + 1/(n+1)*(1/(n*(n+1)))*1*(eta2(n-1)/4));
end
% Check convergence condition
if n > 1 && norm(eta1(n) - eta1(n-1)) < tol
break;
if n > 1 && norm(eta2(n) - eta2(n-1)) < tol
break; % Convergence reached, stop the loop
end
end
end
% Plot the convergence behavior
plot(1:n, eta1(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 1');
hold on;
plot(1:n, eta2(1:n), '-o', 'LineWidth', 2, 'DisplayName', 'Sequence 2');
hold off;
xlabel('n');
ylabel('\eta');
title('Convergence Behavior of Sequences');
legend('Location', 'best');
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by