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

2 views (last 30 days)

```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 Comments
Adam
Adam on 6 Sep 2023
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'?

Sign in to comment.

Accepted Answer

MarKf
MarKf on 6 Sep 2023
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 Comments
MarKf
MarKf on 6 Sep 2023
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 on 6 Sep 2023
Yes it is latex. You can also check the one I just sent.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!