Index exceeds the number of array elements. Index must not exceed 100
2 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
回答(1 个)
Torsten
2023-9-5
编辑:Torsten
2023-9-5
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!