0% found this document useful (0 votes)
26 views

Codes For Matlab

The document describes solving two partial differential equations numerically using finite difference methods. It first solves the 2D heat equation on a rectangular domain, computing the temperature T at discrete points on the grid. It then solves the 1D Poisson equation for a function u, computing u at discrete x-locations. Both problems iterate the computations until the error between successive approximations is less than a threshold.

Uploaded by

Gautham S Nair
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Codes For Matlab

The document describes solving two partial differential equations numerically using finite difference methods. It first solves the 2D heat equation on a rectangular domain, computing the temperature T at discrete points on the grid. It then solves the 1D Poisson equation for a function u, computing u at discrete x-locations. Both problems iterate the computations until the error between successive approximations is less than a threshold.

Uploaded by

Gautham S Nair
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

L=input('ENTER L:

H=input('ENTER H:
n=input('ENTER N:
m=input('ENTER M:
dx=L/n;
dy=H/m;
for i=1:m+1
for j=1:n+1
T(i,j)=0;
end
end

');
');
');
');

x(1)=0;
y(1)=0;
for i=2:n
x(i)=(i-1)*dx;
end
for i=2:m
y(i)=(i-1)*dy;
end

for k=0:200
for j=2:n
for i=2:m
T(1,1)=0;
for z=2:n+1
T(1,z)=0;
T(m+1,z)=100;
end
for z=2:m+1
T(z,1)=0;
T(z,n+1)=100;
end
Tn(i,j)=((T(i+1,j)+T(i-1,j))*dy*dy + (T(i,j-1)+T(i,j+1))*dx*dx)/
(2*(dx*dx+dy*dy));
err1(i)=abs(Tn(i,j)-T(i,j));
err2(j)=abs(Tn(i,j)-T(i,j));
T=Tn;
end
end
errall=max(max(err1),max(err2));
if errall<0.000001
break;

end
end
surf(x,y,T)

L=input('enter L:');
n=input('enter n:');
dx=L/n;
for i=2:n
x(i)=(i-1)*dx;
u(i)=0;
un(i)=0;
end
u(1)=0;
u(n+1)=0;
for j=0:200
for i=2:n
u(n+1)=0;
un(i)=(u(i+1)+u(i-1)-dx*dx*x(i)*x(i))/(2-dx*dx);
err(i)=abs(un(i)-u(i));
u=un;
end
errall=max(err);
if errall<0.0001
break;
end
end
uexact =(sin(x)+sin(1-x)*2)/sin(1)+x.*x-2;
plot(x,uexact);
xlabel('x');
ylabel('u');
hold on;
plot(x,u,'--r');

You might also like