Index exceeds the number of array elements. Index must not exceed 100

2 vues (au cours des 30 derniers jours)
John
John le 5 Sep 2023
Commenté : John le 5 Sep 2023
eta = 0.25
omega = 0.01
gamma = 0.1
zeta = 0.1
theta = 0.5
alpha = 0.5
psi = eta/2
AJ = eta/4
beta = 0.02
%Now, we can implement the iteration process and plot the convergence rates. Here's an example MATLAB code that demonstrates this:
%matlab
% Define the initial value and number of iterations
eta0 = 0.5;
numIterations = 100;
% Initialize arrays to store the values of eta for each iteration
eta1 = zeros(1, numIterations);
eta2 = zeros(1, numIterations);
%eta_array = zeros(0.5, numIterations);
% Define the iteration process for the two sequences
for n = 1:numIterations
% Sequence 1
eta1(n+1) = theta*psi*(eta1(n) + eta1(n+1))/2 + zeta*(eye(size(AJ)) - omega*AJ)*(eta1(n) + eta1(n+1))/2;
% Sequence 2
eta2(n+1) = alpha*psi*(eta2(n)) + beta*eta2(n) + gamma*(eye(size(AJ)) - omega*AJ)*(eta2(n) + eta2(n+1))/2;
eta_array(n) = eta1(n+1);
eta = eta1(n+1);
eta_array(n) = eta2(n+1);
eta = eta2(n+1);
end
% Plot the convergence rates for the two sequences
plot(1:numIterations, eta1(2:end), 'b-', 'LineWidth', 2);
hold on;
plot(1:numIterations, eta2(2:end), 'r--', 'LineWidth', 2);
xlabel('Iteration');
ylabel('\eta_n');
legend('Sequence 1: \eta_{n+1}', 'Sequence 2: \eta_{n+1}');
title('Convergence Rates Comparison');
grid on;

Réponses (1)

Torsten
Torsten le 5 Sep 2023
Modifié(e) : Torsten le 5 Sep 2023
You initialize eta1 and eta2 as arrays of zeros with 100 elements.
When your loop reaches n = 100, you try to access eta1(101) and eta2(101) on the right-hand sides of the expressions
eta1(n+1) = theta*psi*(eta1(n) + eta1(n+1))/2 + zeta*(eye(size(AJ)) - omega*AJ)*(eta1(n) + eta1(n+1))/2;
% Sequence 2
eta2(n+1) = alpha*psi*(eta2(n)) + beta*eta2(n) + gamma*(eye(size(AJ)) - omega*AJ)*(eta2(n) + eta2(n+1))/2;
which do not yet exist.
Run your loop up to n = numIterations-1 and change the plot commands to
% Plot the convergence rates for the two sequences
plot(2:numIterations, eta1(2:end), 'b-', 'LineWidth', 2);
hold on;
plot(2:numIterations, eta2(2:end), 'r--', 'LineWidth', 2);

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by