Azzera filtri
Azzera filtri

Error Control in a matrix

1 visualizzazione (ultimi 30 giorni)
Onurcan BAL
Onurcan BAL il 30 Mag 2021
Risposto: Vaibhav il 12 Feb 2024
I want to make a calculation in a matrix. I have different equations for first, last and inner rows for matrix. I want to do my calculation via iteration method. I created another matrix(B) to calculate the difference between two consecutive iterations. I want my biggest error to be smaller than 10^-4. So ı defined another matrix C, write my first calculation to this matrix then calculate the difference with the previous iteration via B matrix and then write this iteration into my real (A) matrix. But it does not work. What can I do other than this to calculate the difference between the iterations? Thanks in advance.
for p=1:100
for i=23:-1:1
for j=2:36
if (i==23)
elseif (i>=2) && (i<=21)
elseif (i==1)
if max(B)<(10^(-4))

Risposte (1)

Vaibhav il 12 Feb 2024
Hi Onurcan
I understand that you are trying to implement an iterative method to solve a matrix equation, and you want to stop the iterations when the maximum change between two consecutive iterations is less than (10^(-4)).
Here's an approach that you can consider following:
  1. Initialize matrices A, C, and B, ensuring that C and B are of the same size as A.
  2. Set the relaxation factor w, the constants h, q, k, t, H, and any other necessary constants.
  3. Use a while loop for the iteration process instead of a for loop with a hardcoded number of iterations.
  4. Update the elements of A based on the conditions you specified for the first, last, and inner rows.
  5. Calculate the difference between the new and old values of A and store it in B.
  6. Check the maximum value of B to decide whether to continue iterating or to stop.
Here's a code snippet for your reference:
% Assuming A is already initialized and has the correct size
C = A; % Initialize C with the same values as A
B = zeros(size(A)); % Initialize B with zeros
w = 1; % Set the relaxation factor (example value)
h = 0.1; % Set h (example value)
q = 1; % Set q (example value)
k = 1; % Set k (example value)
t = 1; % Set t (example value)
H = 1; % Set H (example value)
% Set your tolerance
tolerance = 1e-4;
error = inf; % Initialize error to a large value
l = 1; % Initialize iteration counter
% Iteration loop
while error > tolerance
for i = 23:-1:1
for j = 2:36
if i == 23
C(i,j) = A(i-2,j) - (30 * h);
elseif i >= 2 && i <= 21
C(i,j) = A(i,j) + w * ((A(i-1,j) + A(i+1,j) + A(i,j+1) + A(i,j-1) - (4 * A(i,j))) / 4 + (q * (h^2) / (4 * k * t)));
elseif i == 1
C(i,j) = A(i+2,j) - (((2 * h * H) / k) * (A(2,j) - 25));
% Calculate the difference matrix B
B = abs(A - C);
% Update the maximum error
error = max(B(:));
% Update matrix A with the new values
A = C;
% Increment iteration counter
l = l + 1;
disp(['Iterations completed: ', num2str(l)]);
disp('Final matrix A:');
Hope this helps!


Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by