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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by