- Initialize matrices A, C, and B, ensuring that C and B are of the same size as A.
- Set the relaxation factor w, the constants h, q, k, t, H, and any other necessary constants.
- Use a while loop for the iteration process instead of a for loop with a hardcoded number of iterations.
- Update the elements of A based on the conditions you specified for the first, last, and inner rows.
- Calculate the difference between the new and old values of A and store it in B.
- Check the maximum value of B to decide whether to continue iterating or to stop.
Error Control in a matrix
1 view (last 30 days)
Show older comments
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)
C(i,j)=A(i-2,j)-(30*h)
B(i,j)=A(i,j)-C(i,j)
C(i,j)=A(i,j)
elseif (i>=2) && (i<=21)
A(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)))
B(i,j)=A(i,j)-C(i,j)
C(i,j)=A(i,j)
elseif (i==1)
A(i,j)=A(i+2,j)-(((2*h*(H))/k)*(A(2,j)-25))
B(i,j)=A(i,j)-C(i,j)
C(i,j)=A(i,j)
end
end
end
if max(B)<(10^(-4))
break
end
l=l+1
end
0 Comments
Answers (1)
Vaibhav
on 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:
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));
end
end
end
% 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;
end
disp(['Iterations completed: ', num2str(l)]);
disp('Final matrix A:');
disp(A);
Hope this helps!
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!