How to use iteration and error for crank nicolson type to converge spatial and temporal discretization?

3 ビュー (過去 30 日間)
I'm using iteration and error for spatial discretization and temporal discretization respectively. I want apply iteration and error condition in crank nicolson method using while loop.
Please suggest me about this.
Thank you.
  3 件のコメント
Yanni
Yanni 2023 年 5 月 2 日
@Torsten Yes I deleted my comment only. because there is no response for my comments that's why.
kindly, suggest me for the above one.
Walter Roberson
Walter Roberson 2023 年 5 月 2 日
Yes I deleted my comment only. because there is no response for my comments
That is not what the logs tell me. I can see places where you have deleted comments after people replied to your comments.

サインインしてコメントする。

採用された回答

Aditya Srikar
Aditya Srikar 2023 年 5 月 26 日
Here are some steps for implementing iteration and error conditions in Crank Nicolson method using a while loop:
1. Initialize the solution array with the initial or boundary conditions.
2. Set a maximum number of iterations and tolerance level for the error.
3. Define a while loop that iteratively solves the Crank Nicolson equation until either the maximum number of iterations is reached or the tolerance level is satisfied.
4. In each iteration of the while loop, calculate the RHS of the Crank Nicolson equation using the current solution values.
5. Apply the boundary conditions and solve the resulting tridiagonal system of equations using a direct method such as Thomas algorithm or an iterative method like GMRES.
6. Calculate the error between the current and previous solution values.
7. Check if the error is less than the tolerance level and break the while loop if it is satisfied.
8. Otherwise, update the solution values with the newly calculated values and continue the next iteration of the while loop.
  1 件のコメント
Yanni
Yanni 2023 年 5 月 30 日
@Aditya Srikar Really thank you for your tips. I followed the above rule., but i couldn't apply rule 4, 5 and 7 ( break the while loop if it is satisfied.).
In my code I'm applying error condtion to reach steady state criteria and iteration for converges with my bc condtion. it's perplexed me to get the curve. so, here i attached my code .,
kindly assist to terminate my code well.
clc; close all; clear all;
ymax=20; m=80; dy=ymax/m; %'i'th row
dx=xmax/n; %'j'th column
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
max_difference(1)=1; tol=1e-2;
max_Iteration=100; k=0;
UOLD=zeros(m,nt); VOLD=zeros(m,nt);
TNEW=0; TOLD=TNEW*ones(m,nt); TWALL=ones(1,length(t));
A=zeros([1,m]);
B=A;
C=A;
D=A;
T=TOLD;
tic
while max_Iteration>tol %how to use iteration for convergent with bc
for j=1:nt
for i=1:m
if j>i
C(i)=(dt*VOLD(i,j)/4*dy)-(dt/(2*dy^2));
elseif i>j
A(i)=(-dt*VOLD(i,j)/4*dy)-(dt/(2*dy^2));
elseif i==j
B(i)=1+(dt*UOLD(i,j)/2*dx)+(dt/(dy^2));
end
end
end
for j=2:nt
if j==1
for i=1:m
if i==1
D(i)=(-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/2*dx)+(dt/(2*dy^2)*(TOLD(i+1,j)-2*TOLD(i,j)+TNEW))-(dt*VOLD(i,j)/4*dy*(TNEW-TOLD(i+1,j)-TOLD(i,j)))-(dt/4*dy*VOLD(i,j))-(dt/2*dy^2*TNEW);
elseif i==m
D(i)=(-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/2*dx)+(dt/(2*dy^2)*(TWALL(j)-2*TOLD(i,j)+TOLD(i-1,j)))-(dt*VOLD(i,j)/4*dy*(TOLD(i-1,j)-TWALL(j)+TOLD(i,j)))-(-dt/4*dy*VOLD(i,j))-(dt/2*dy^2*TWALL);
else
D(i)=(-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/2*dx)+(dt/(2*dy^2)*(TOLD(i+1,j)-2*TOLD(i,j)+TOLD(i-1,j)))-(dt*VOLD(i,j)/4*dy*(TOLD(i-1,j)-TOLD(i+1,j)+TOLD(i,j)));
end
end
else
for i=1:m
if i==1
D(i)=(-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/2*dx)+(dt/(2*dy^2)*(TOLD(i+1,j)-2*TOLD(i,j)+TNEW))-(dt*VOLD(i,j)/4*dy*(TNEW-TOLD(i+1,j)+TOLD(i,j)))-(dt/4*dy*VOLD(i,j))-(dt/2*dy^2*TNEW);
elseif i==m
D(i)=(-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/2*dx)+(dt/(2*dy^2)*(TWALL(j)-2*TOLD(i,j)+TOLD(i-1,j)))-(dt*VOLD(i,j)/4*dy*(TOLD(i-1,j)-TWALL(j)+TOLD(i,j)))-(-dt/4*dy*VOLD(i,j))-(dt/2*dy^2*TWALL(j));
else
D(i)=(-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/2*dx)+(dt/(2*dy^2)*(TOLD(i+1,j)-2*TOLD(i,j)+TOLD(i-1,j)))-(dt*VOLD(i,j)/4*dy*(TOLD(i-1,j)-TOLD(i+1,j)+TOLD(i,j)));
end
end
end
T(:,j)=TriDiag(A,B,C,D);
dt=0.2+dt
TOLD=T;
%====================STEADY STATE=================================%
max_difference(j) = max(abs(T(:,j)-T(:,j-1))./max(1,abs(T(:,j-1))));
if max_difference(j)<tol
break
end
max_Iteration=max_difference;
k=k+1;
end
end
toc

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by