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

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

 Accepted Answer

Change
dx = 1.01/(Nx-1);
dy = 1.01/(Ny-1);
to
dx = 1.0/(Nx-1);
dy = 1.0/(Ny-1);

10 Comments

Then what will happen to initial conditions i.e. T(50, 20) = 2.5 and so on?
Your settings are inconsistent.
Either change
dx = 1.01/(Nx-1);
dy = 1.01/(Ny-1);
to
dx = 1.0/(Nx-1);
dy = 1.0/(Ny-1);
or change
X = 0:dx:1;
Y = 0:dy:1;
to
X = 0:dx:1.01;
Y = 0:dy:1.01;
In either case,
T(ix,iy) will be T at P=((ix-1)*dx,(iy-1)*dy)
I got the plot using your 1st suggestion but i 'm not getting my right boundary condition into it, could you please check..
What do you mean by "i 'm not getting my right boundary condition into it" ? And where can I check ?
For (i = 2 and i = Nx-1) and (j = 2 and j = Ny-1), you will have to do the standard discretization with the second-order central difference star. You should replace the boundary conditions settings for these points since they are no boundary points.
I'm getting this profile but is it correct? for fourth corder central difference scheme for 2d laplace equation and I'm not seeing my right boundary condition :T_new(:,Nx-1) = TR(Nx-1) here in the plot..
Didn't you read my comment ?
For (i = 2 and i = Nx-1) and (j = 2 and j = Ny-1), you will have to do the standard discretization with the second-order central difference star. You must replace the boundary conditions settings for these points since they are no boundary points.
sorry sir, but i didn't get you.. actually, i don't know how to put as you said.
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

Sign in to comment.

More Answers (1)

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

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!