The size of X must match the size of Z or the number of columns of Z.
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Kushagra Saurabh
am 22 Nov. 2022
Bearbeitet: Walter Roberson
am 19 Aug. 2024
I am trying to solve the 2d laplace equation for heat conduction using forward diffrence scheme, but I'm getting an error:The size of X must match the size of Z or the number of columns of Z in line 92. here's my code:
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-2);
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-2);
error_mag = 5;
error_req = 1e-07;
iteration = 0;
% % calculation
while error_mag > error_req
for i = 2:Nx-2
for j=2:Ny-2
T_new(i,j) = (2*T(i+1,j)-T(i+2,j)+2*T(i,j+1)-T(i,j+2))/2; % forward difference scheme
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-2);
iteration = iteration +1;
end
end
% calculation of error magnitude
for i= 2:Nx-2
for j = 2: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');
colorbar
0 Kommentare
Akzeptierte Antwort
Abhaya
am 13 Aug. 2024
Bearbeitet: Abhaya
am 19 Aug. 2024
Hi Kushagra,
I understand you are trying to solve the 2d Laplace equation for heat conduction using forward difference scheme, but getting the error that “The size of X must match the size of Z or the number of columns of Z”. This issue arises because the vectors X and Y have a size of 101, whereas the matrix T has a size of 102x102. When you use MATLAB ‘contourf’ function with the parameters‘X’,‘Y’, and‘Z’, the‘Z’parameter represents the height or value at the corresponding(X, Y)location. So the size of vector ‘X’ must match with column size of matrix ‘T`’.
A possible workaround is to extend the vector ‘X’ and vector ‘Y’ up to 1.01 with increment of ‘dx’ and ‘dy’ respectively. This will result vectors ‘X’ and ‘Y’ each of size 102.
Another workaround is to create a 'meshgrid' of 102x102 by appending 0 to the beginning of the vectors 'X' and 'Y'.
% % plotting
X1=[0 X];
Y1=[0 Y];
[x,y] = meshgrid(X1,Y1);
colormap("jet");
contourf(x,y,T');
colorbar
For further explanation follow the documentation of MATLAB ‘contourf’ function.
Hope this helps.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Geometry and Mesh finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!