The size of X must match the size of Z or the number of columns of Z.

4 次查看(过去 30 天)
i'am trying to solve 2d laplace equation using fourth order central difference could but i'am getting an error:The size of X must match the size of Z or the number of columns of Z, During plotting. could someone help me here.
My code is:
clear all
close all
clc
% % geometry of domain
Nx = 102;
Ny = 102;
dx = 1.01/(Nx-1);
dy = 1.01/(Ny-1);
X = 0:dx:1;
Y = 0:dy:1;
% % initial condition
T = zeros(Nx,Ny);
T(50,20) = 2.5;
T(25,25) = -0.5;
T(75,10) = -2.5;
% % boundary condition
TL = 1;
TR = cos(6*(3*pi*Y)/2)+1;
TT = 1;
TB = 1+X;
T(:,1) = TL;
T(Ny,:) = TT;
T(:,Nx) = TR(Nx-1);
T(1,:) = TB(1);
T(:,2) = TL;
T(Ny-1,:) = TT;
T(2,:) = TB(2);
T(:,Nx-1) = TR(Nx-1);
T_new(Nx,Ny) = 0;
TL = 1;
TR = cos(6*(3*pi*Y)/2)+1;
TT = 1;
TB = 1+X;
T_new(:,1) = TL;
T_new(Ny,:) = TT;
T_new(:,Nx) = TR(Nx-1);
T_new(1,:) = TB(1);
T_new(:,2) = TL;
T_new(Ny-1,:) = TT;
T_new(2,:) = TB(2);
T_new(:,Nx-1) = TR(Nx-1);
error_mag = 3;
error_req = 1e-03;
iteration = 0;
% % calculation
while error_mag > error_req
for i = 3:Nx-2
for j=3:Ny-2
T_new(i,j) = (16*T(i+1,j)+16*T(i-1,j)-T(i-2,j)-T(i+2,j)-T(i,j+2)+16*T(i,j+1)+16*T(i,j-1)-T(i,j-2))/60; % fourth order central difference
T_new(50,20) = 2.5;
T_new(25,25) = -0.5;
T_new(75,10) = -2.5;
TL = 1;
TR = cos(6*(3*pi*Y)/2)+1;
TT = 1;
TB = 1+X;
T_new(:,1) = TL;
T_new(Ny,:) = TT;
T_new(:,Nx) = TR(Nx-1);
T_new(1,:) = TB(1);
T_new(:,2) = TL;
T_new(Ny-1,:) = TT;
T_new(2,:) = TB(2);
T_new(:,Nx-1) = TR(Nx-1);
iteration = iteration +1;
end
end
% calculation of error magnitude
for i= 3:Nx-2
for j = 3:Ny-2
error_mag = abs(T(i,j)-T_new(i,j));
end
end
%assigning new to old
T = T_new;
end
% % plotting
[x,y] = meshgrid(X,Y);
colormap("jet");
contourf(X,Y,T');
Error using contourf
The size of X must match the size of Z or the number of columns of Z.
colorbar

采纳的回答

Torsten
Torsten 2022-11-23
Change
dx = 1.01/(Nx-1);
dy = 1.01/(Ny-1);
to
dx = 1.0/(Nx-1);
dy = 1.0/(Ny-1);
  10 个评论
Torsten
Torsten 2022-11-23
T_new(i,j) = (T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1))/4; % second order central difference
for i = 2, 2 <= j <= Ny-1
for j = 2, 2 <= i <= Nx-1
for i = Nx-1, 2<=j <= Ny-1
for j = Ny-1, 2<= i <= Nx-1

请先登录,再进行评论。

更多回答(1 个)

Voss
Voss 2022-11-23
The error happens because T is a 102-by-102 matrix but X and Y only have 101 elements:
% % geometry of domain
Nx = 102;
Ny = 102;
dx = 1.01/(Nx-1);
dy = 1.01/(Ny-1);
X = 0:dx:1;
Y = 0:dy:1;
% % initial condition
T = zeros(Nx,Ny);
whos X Y T
Name Size Bytes Class Attributes T 102x102 83232 double X 1x101 808 double Y 1x101 808 double

类别

Help CenterFile Exchange 中查找有关 Configure Simulation Conditions 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by