Clear Filters
Clear Filters

Difficulty to make the error norms show as intended

2 views (last 30 days)
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

Accepted Answer

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

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!