필터 지우기
필터 지우기

Difficulty to make the error norms show as intended

조회 수: 2 (최근 30일)
Jayden Cavanagh
Jayden Cavanagh 2021년 6월 6일
답변: Walter Roberson 2021년 6월 6일
For my code I need to check the error norm for each ndiv value and then set it in a table of format (ndiv, L1norm,L2norm and LInfnorm) with all of the respective norms for the corresponding ndiv. Currently my code just does it for the last one. How do I set it so that I can create a table of all the error norms for the corresponding ndiv values?
Code:
a=25;
b=a;
ndiv=[4,8,16,32,64,128,256,512];
for nidx=1:length(ndiv)
nx=ndiv(nidx);
nz=nx;
x = linspace(0, a, nx);
z = linspace(0, b, nz);
[X, Z] = meshgrid(x,z);
dx = a/(nx-1);
dz = b/(nz-1);
Tnp1 = zeros(nx, nz);
T_analytic=zeros(nx,nz);
Tnp1(:,1) = 20; % Left boundary
Tnp1(:,end) = 20; % Right boundary
Tnp1(1,:) = 20+380*sin((x*pi)/25)+205*sin((x*5*pi)/25); % Bottom boundary
Tnp1(end,:) = 20; % Top boundary
err = 1;
tol = 1e-8;
k=0;
while err > tol
Told = Tnp1;
k=k+1;
for i = 2:nx-1
for j = 2:nz-1
Tnp1(i,j)=Tnp1(i,j)+1.85*(0.25*(Tnp1(i+1,j)+Tnp1(i-1,j)+Tnp1(i,j+1)+Tnp1(i,j-1))-Tnp1(i,j));
T_analytic(i,j)=20+380/sinh(pi)+205/sinh(5*pi);
end
end
err = max(max(abs((Tnp1-Told)./Tnp1)));
end
T_numeric = Tnp1; %Final T Grid for SOR method
% Create a variable for the difference between solutions:
diff = T_numeric(2:end-1,2:end-1) - T_analytic(2:end-1,2:end-1);
% "Unwrap" the 2D array to a 1D row vector;
diffVec = reshape(diff',1,numel(diff));
% Compute Lp norms as usual;
L1norm = norm(diffVec,1)*(dx*dz);
L2norm = norm(diffVec,2)*sqrt(dx*dz);
LInfnorm = norm(diffVec,Inf);
end

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 6일
L1norm(nidx) = norm(diffVec,1)*(dx*dz);
L2norm(nidx) = norm(diffVec,2)*sqrt(dx*dz);
LInfnorm(nidx) = norm(diffVec,Inf);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by