필터 지우기
필터 지우기

How do I make the table function work?

조회 수: 3 (최근 30일)
Jayden Cavanagh
Jayden Cavanagh 2021년 6월 6일
댓글: Walter Roberson 2021년 6월 6일
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);

답변 (2개)

Walter Roberson
Walter Roberson 2021년 6월 6일
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
Jayden Cavanagh
Jayden Cavanagh 2021년 6월 6일
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 2021년 6월 6일
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);

댓글을 달려면 로그인하십시오.


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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by