Solving 2d laplace equation with insulation ,,,,, code won't work because the error doesn't get below 1 neeeed help

2 views (last 30 days)
%%
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

Answers (1)

Alan Stevens
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);
The total number of iteration is: 22
fprintf('The maximum error is %g\n', error);
The maximum error is 7.13032e-05
fprintf('The temperature values are:\n');
The temperature values are:
disp(T)
120.0000 72.6256 72.6256 72.6256 62.5097 0 0 0 0 100.0000 37.9012 37.9012 37.9012 45.6020 0 0 0 0 80.0000 37.9012 37.9012 37.9012 45.6020 0 0 0 0 60.0000 37.9012 37.9012 37.9012 49.2814 51.9268 51.9268 51.9268 0 40.0000 37.9012 37.9012 37.9012 37.9012 37.9012 37.9012 37.9012 0 20.0000 37.9012 37.9012 37.9012 37.9012 37.9012 37.9012 37.9012 0 0 33.3159 33.3159 33.3159 33.3159 33.3159 33.3159 33.3159 0
  6 Comments

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!