How do I make the table function work?

5 vues (au cours des 30 derniers jours)
Jayden Cavanagh
Jayden Cavanagh le 6 Juin 2021
Commenté : Walter Roberson le 6 Juin 2021
I am trying to make a table that shows each of the L1norm,L2norm and LInfnorm that corresponds to the right ndiv value. In the end I should get a 8x4 table. What the table is actually doing is setting it out as a 1x4 and has all of the variables stacked as [1x8 double].
Norms =
1×4 table
ndiv L1norm L2norm LInfnorm
____________ ____________ ____________ ____________
[1×8 double] [1×8 double] [1×8 double] [1×8 double]
How do I fix this so it shows me variables instead?
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(nidx) = norm(diffVec,1)*(dx*dz);
L2norm(nidx) = norm(diffVec,2)*sqrt(dx*dz);
LInfnorm(nidx) = norm(diffVec,Inf);
end
Norms=table(ndiv,L1norm,L2norm,LInfnorm);

Réponses (2)

Walter Roberson
Walter Roberson le 6 Juin 2021
ndiv=[4,8,16,32,64,128,256,512].;
L1norm(nidx,1) = norm(diffVec,1)*(dx*dz);
L2norm(nidx,1) = norm(diffVec,2)*sqrt(dx*dz);
LInfnorm(nidx,1) = norm(diffVec,Inf);
  2 commentaires
Jayden Cavanagh
Jayden Cavanagh le 6 Juin 2021
When I do that I get this error message
Error using table (line 233)
All table variables must have the same number of rows.
Error in ErrorNorms (line 58)
Norms=table(ndiv,L1norm,L2norm,LInfnorm)
Walter Roberson
Walter Roberson le 6 Juin 2021
Ah, I notice that a ' went missing from when I typed the code on my mobile.
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(nidx,1) = norm(diffVec,1)*(dx*dz);
L2norm(nidx,1) = norm(diffVec,2)*sqrt(dx*dz);
LInfnorm(nidx,1) = norm(diffVec,Inf);
end
Norms=table(ndiv,L1norm,L2norm,LInfnorm);

Connectez-vous pour commenter.


Sulaymon Eshkabilov
Sulaymon Eshkabilov le 6 Juin 2021
Here is the correction:
...
Norms=table(ndiv',L1norm',L2norm',LInfnorm', 'variablenames', {'ndiv', 'L1norm', 'L2norm', 'Linnorm'});

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by