Friday, January 14, 2011

Simulating heat conduction using Scilab

Heat conduction is one of the simplest physical phenomena to simulate as it simply depends on the diffusion equation. I wrote a simple Scilab code that simulates the heat conduction process from a square block using the finite difference method for the spatial discretization and forward Euler (explicit) method for time integration. The code is going to show you how Scilab can be used for simulation and visualization.



  And here you are the code (tested and worked)


//Scilab code for simulating heat conduction to a square block
//Time step
dt=0.05;

//Ambient temperature
T_amb=100;     //degrees in Celcius

//Creat an empty matrix of the temperature
T=[];

//Lower boundary
for i=1:20,
T(i,1)=T_amb;
end

//Upper boundary
for i=1:20,
T(i,20)=T_amb;
end

//left boundary
for j=1:20,
T(1,j)=T_amb;
end

//right boundary
for j=1:20,
T(20,j)=T_amb;
end

for i=2:19,
for j=2:19,
T(i,j)=20;     //Initial condition
end
end

x=1:20;
y=1:20;

show_window(); clf()
f=gcf();
f.color_map=jetcolormap(256);
f.pixmap='on';

colorbar(0,100);
T_new=T;  //use this line to copy boundaries
for counter=1:60,
T=T_new;
for i=2:19,
for j=2:19,
T_new(i,j)=dt*(T(i-1,j)+T(i+1,j)+T(i,j-1)+T(i,j+1)-4*T(i,j))+T(i,j);
end
end
z=T_new;
Sgrayplot(x,y,z, strf="042",rect=[-5 2 25 40], zminmax=[0,100])
show_pixmap();
end