- #1
range.rover
- 16
- 0
Hi guys,I am new for programming. I made my first attempt to solve an heat equation by finite difference method and wrote a code for it in Matlab.I got a solution but i need help from you guys to (vectorize) increase the speed of my program.
((∂^2 T)/(∂x^2 )+(∂^2 T)/(∂y^2 )+(∂^2 T)/(∂z^2 ))(k)=-Q(x,y,z)
clear;
close all;
clc;
n = 5;% n grids and has n - 2 interior points per dimension
x = linspace(0,1,n);% line in x direction of 0 to 1 and divided into n parts
dx = x(2)-x(1); % distance between grids
y = x;
dy = dx;
k=1; %thermal conductivity
Q=300 ; % heat source
m=(n-2)^2;
%Right hand side matrix
B=zeros(m,1);
if (mod(m,2)==0)
B(m/2,1)=Q;
else
B((m+1)/2,1)=Q;
end
%Main Matrix
Tsol = zeros(n);
%boundry conditions
Tsol(1,1:n) = 0; %TOP
Tsol(n,1:n) = 0; %BOTTOM
Tsol(1:n,1) = 0; %LEFT
Tsol(1:n,n) = 0; %RIGHT
tic
error = 1; z = 0;
while error > 0.000001
z = z+1;
c=1;
Torg = Tsol;
for i = 2:n-1
for j = 2:n-1
Tsol(i,j) =abs(((dx^2*B(c,1))+((Torg(i+1,j)+Torg(i-1,j)+Torg(i,j+1)+Torg(i,j-1))*k))/(4*k));
c=c+1;
end
end
error = max(max(abs(Torg-Tsol)));
end
toc
%% plotting
figure, contour(x,y,Tsol),
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
figure,pcolor(x,y,Tsol),shading interp,
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
((∂^2 T)/(∂x^2 )+(∂^2 T)/(∂y^2 )+(∂^2 T)/(∂z^2 ))(k)=-Q(x,y,z)
clear;
close all;
clc;
n = 5;% n grids and has n - 2 interior points per dimension
x = linspace(0,1,n);% line in x direction of 0 to 1 and divided into n parts
dx = x(2)-x(1); % distance between grids
y = x;
dy = dx;
k=1; %thermal conductivity
Q=300 ; % heat source
m=(n-2)^2;
%Right hand side matrix
B=zeros(m,1);
if (mod(m,2)==0)
B(m/2,1)=Q;
else
B((m+1)/2,1)=Q;
end
%Main Matrix
Tsol = zeros(n);
%boundry conditions
Tsol(1,1:n) = 0; %TOP
Tsol(n,1:n) = 0; %BOTTOM
Tsol(1:n,1) = 0; %LEFT
Tsol(1:n,n) = 0; %RIGHT
tic
error = 1; z = 0;
while error > 0.000001
z = z+1;
c=1;
Torg = Tsol;
for i = 2:n-1
for j = 2:n-1
Tsol(i,j) =abs(((dx^2*B(c,1))+((Torg(i+1,j)+Torg(i-1,j)+Torg(i,j+1)+Torg(i,j-1))*k))/(4*k));
c=c+1;
end
end
error = max(max(abs(Torg-Tsol)));
end
toc
%% plotting
figure, contour(x,y,Tsol),
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar
figure,pcolor(x,y,Tsol),shading interp,
title('Temperature (Steady State)'),xlabel('x'),ylabel('y'),colorbar