The size of X must match the size of Z or the number of columns of Z.
Show older comments
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');
colorbar
Accepted Answer
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
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!