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

%%
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

답변 (1개)

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

It work but the final answer is wrong The photo shows the right answer
This is not a MATLAB problem - your discretization must be wrong somehow.
It's an assignment in our cours , this is the final answer given to us by our professor and he solve it by matlab.
Yes, I don't doubt that it can be solved with MATLAB. But your problem is the discretization, not a technical MATLAB issue.
Ok thanks my friend i will try and work that out.
Ok i worked on my descretizaion and it works thanks for the help much appreciation guys

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

릴리스

R2014a

질문:

2022년 8월 24일

댓글:

2022년 8월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by