Error Control in a matrix

조회 수: 4 (최근 30일)
Onurcan BAL
Onurcan BAL 2021년 5월 30일
답변: Vaibhav 2024년 2월 12일
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

답변 (1개)

Vaibhav
Vaibhav 2024년 2월 12일
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));
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!

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by