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 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
Accepted Answer
More Answers (0)
Categories
Find more on Polygons 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!