Solving 2d laplace equation with insulation ,,,,, code won't work because the error doesn't get below 1 neeeed help
3 views (last 30 days)
Show older comments
%%
clear all
close all
clc
%%
m=7;
n=9;
L=40;
H=30;
dx=L/(n-1);
dy=H/(m-1);
x=0:dx:L;
y=0:dy:H;
lamda=1.5;
epsilon=1e-4;
%%
T=zeros(m,n);
T_old=zeros(m,n);
%%
T(:,1)=[120 100 80 60 40 20 0];
T(5:6,9)=[0 0];
%%
error=1;
iteration=0;
while error>=epsilon
for j=n-1:-1:2
for i=2:m-1
T_old(i,j)=T(i,j);
T(1,2:4)=(T(1,j+1)+T(1,j-1)+(2*T(2,j)))/4;
T(1,5)=((2*T(1,4))+(2*T(2,5)))/4;
T(2:3,5)=(T(i-1,5)+T(i+1,5)+(2*T(i,4)))/4;
T(4,5)=((2*T(4,4))+(2*T(5,5)))/4;
T(4,6:8)=(T(4,j+1)+T(4,j-1)+(2*T(5,j)))/4;
T(7,2:8)=(T(7,j+1)+T(7,j-1)+(2*T(6,j)))/4;
T(2:4,2:4)=(T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1))/4;
T(5:6,2:8)=(T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1))/4;
T(i,j)=lamda*T(i,j)+(1-lamda)*T_old(i,j);
error_T(i,j)=100*abs((T(i,j)-T_old(i,j))/T(i,j));
end
end
error=max(error_T(:));
iteration=iteration+1;
end
%%
fprintf('The total number of iterarion is: %d\n',iteration);
fprintf('The temperature values are:\n');
T
0 Comments
Answers (1)
Alan Stevens
on 25 Aug 2022
Like this?
m=7;
n=9;
L=40;
H=30;
dx=L/(n-1);
dy=H/(m-1);
x=0:dx:L;
y=0:dy:H;
lamda=1.5;
epsilon=1e-4;
%%
T=zeros(m,n);
T_old=zeros(m,n);
%%
T(:,1)=[120 100 80 60 40 20 0];
T(5:6,9)=[0 0];
%%
error=1;
iteration=0;
while (error>=epsilon) && (iteration<100)
T_old=T;
for j=n-1:-1:2
for i=2:m-1
T(1,2:4)=(T(1,j+1)+T(1,j-1)+(2*T(2,j)))/4;
T(1,5)=((2*T(1,4))+(2*T(2,5)))/4;
T(2:3,5)=(T(i-1,5)+T(i+1,5)+(2*T(i,4)))/4;
T(4,5)=((2*T(4,4))+(2*T(5,5)))/4;
T(4,6:8)=(T(4,j+1)+T(4,j-1)+(2*T(5,j)))/4;
T(7,2:8)=(T(7,j+1)+T(7,j-1)+(2*T(6,j)))/4;
T(2:4,2:4)=(T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1))/4;
T(5:6,2:8)=(T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1))/4;
end
end
T=lamda*T+(1-lamda)*T_old;
error_T=100*abs(T-T_old)./T;
error=max(error_T(:));
iteration=iteration+1;
end
%%
fprintf('The total number of iteration is: %d\n',iteration);
fprintf('The maximum error is %g\n', error);
fprintf('The temperature values are:\n');
disp(T)
6 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!