I run this but its given me issues. Can someone assist

1 回表示 (過去 30 日間)
John
John 2023 年 9 月 6 日
コメント済み: John 2023 年 9 月 6 日

```matlab % Step 1: Define variables and parameters eta = 1; % Initial value of eta epsilon = 1e-5; % Convergence condition L = 3; % L value

% Step 2: Loop for iteration eta_diff = []; % Store absolute difference between eta_n+1 and eta_n iteration = 0; % Initialize iteration counter

while true iteration = iteration + 1;

    % Calculate values using the given equation
    omega = 1 / (iteration * (iteration + 1));
    zeta = 1 / (iteration + 1);
    vartheta = 1 / (iteration + 1);
    psi_eta = eta / 2;
    A = L * eta;
    J = eta;
    eta_next = vartheta * psi_eta + zeta * (eye(size(A)) - omega * A * J) * (eta + eta_next) / 2;
    eta_diff = [eta_diff, abs(eta_next - eta)];
    % Check for convergence
    if eta_diff(end) < epsilon
        break;
    end
    eta = eta_next;
end

% Step 3: Calculate convergence rate and plot iterations = 1:iteration; convergence_rate = eta_diff(2:end) ./ eta_diff(1:end-1);

figure; subplot(2,1,1); plot(iterations, eta_diff); xlabel('Number of Iterations'); ylabel('|\eta_{n+1} - \eta_n|'); title('Convergence Rate');

subplot(2,1,2); plot(iterations(2:end), convergence_rate); xlabel('Number of Iterations'); ylabel('Convergence Rate'); title('Convergence Rate');

% Print the number of iterations until convergence fprintf('Convergence achieved in %d iterations.\n', iteration); ```

  2 件のコメント
Adam
Adam 2023 年 9 月 6 日
It would help someone to answer if you could format your code properly and give an explanation to go with it. e.g. what are the 'issues'?
John
John 2023 年 9 月 6 日
Ok Will do that soon

サインインしてコメントする。

採用された回答

MarKf
MarKf 2023 年 9 月 6 日
I assume that the copy and paste of your code did not go well... but then you should've fixed it before posting.
Even after fixing it, the reason it does not run is on the line eta_next = vartheta * etc.That said, that issue means that the code likely still needs some work.
% Step 1: Define variables and parameters
eta = 1; % Initial value of eta
epsilon = 1e-5; % Convergence condition
L = 3; % L value
% Step 2: Loop for iteration
eta_diff = []; % Store absolute difference between eta_n+1 and eta_n
iteration = 0; % Initialize iteration counter
while true
iteration = iteration + 1;
% Calculate values using the given equation
omega = 1 / (iteration * (iteration + 1));
zeta = 1 / (iteration + 1);
vartheta = 1 / (iteration + 1);
psi_eta = eta / 2;
A = L * eta;
J = eta;
eta_next = eta; %issue just below, as eta_next; was not yet defined
eta_next = vartheta * psi_eta + zeta * (eye(size(A)) - omega * A * J) * (eta + eta_next) / 2; %issue is here, as eta_next; is not yet defined
eta_diff = [eta_diff, abs(eta_next - eta)];
% Check for convergence
if eta_diff(end) < epsilon
break;
end
eta = eta_next;
end
% Step 3: Calculate convergence rate and plot
iterations = 1:iteration;
convergence_rate = eta_diff(2:end) ./ eta_diff(1:end-1);
figure; subplot(2,1,1);
plot(iterations, eta_diff);
xlabel('Number of Iterations'); ylabel('|\eta_{n+1} - \eta_n|'); title('Convergence Rate');
subplot(2,1,2);
plot(iterations(2:end), convergence_rate);
xlabel('Number of Iterations'); ylabel('Convergence Rate'); title('Convergence Rate');
% Print the number of iterations until convergence
fprintf('Convergence achieved in %d iterations.\n', iteration);
Convergence achieved in 2 iterations.
  4 件のコメント
MarKf
MarKf 2023 年 9 月 6 日
Sorry, "graph starts at number" is not very clear. It does converge to zero but the scropt is likely not working as inteded with 2 iterations. It seems that you're trying to translate a formula or another script, the mathematical problem however is ill-defined to say the least.
I don't understand this kind of scripting \eta_{n+1} (is it latex?) but whatever language that is it seems it's accessing n+1 before defining it and you can't do that with matlab (unless you preallocate the vector I guess, but that'd be nonsensse, in that case it'd probably be a different variable called with a different name). As the number of iterations increases it seems that the second plot would also get populated with data.
John
John 2023 年 9 月 6 日
Yes it is latex. You can also check the one I just sent.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by