Sei sulla pagina 1di 4

Simple SCILAB CODE for Heat Equation

Que: Write the SciLab code for the solution of one dimensional heat equation
subjected to u(0,t)=u(L,t)=0, if the initial conditions are given by
u(x,0)=f(x)=4*(x/L)*(1-x/L). Use the values of k=1, L=1. Also find the
temperature in rod at x=0.4,t=0.2

Source code:

clc
clear
L=input('Enter the length of rod L')
k=input('Enter the value of coefficient k')
n=input('Enter the number of terms in solution you
want n')
function w=f(x)
w=4*x/L*(1-x/L)
endfunction
for i=1:n
function w1=f1(x)
w1=f(x)*sin(i*%pi*x/L);
endfunction
b(i)=(2/L)*integrate('f1(x)','x',0,L);
end
// calculate u
function uu=u(x, t)
uu=0;
for j=1:n;
uu=uu+b(j)*sin(j*%pi*x/L)*exp(-j^2*
%pi^2*k*t/L^2);
end
endfunction;
x=[0:0.05:1];t=[0:0.025:0.25];
u1=feval(x,t,u);
plot3d(x,t,u1,'x@t@u(x,t)')
//..... for 2d plot.......................
scf(1)
plot(x',u1(:,1),'m.*',x', u1(:,3),'g.-',x',u1(:,6),'b.
+',x',u1(:,9),'r.x')
//u1(:,1) temperature for time t=0,
//u1(:,3) temperature for time t=0.05,
//u1(:,6) temperature for time t=0.075,
//u1(:,9) displacemnt for time t=0.15,
xtitle('Heat equation Solution','x','u(x,t)')

Console Command:
Enter the length of rod L
1
Enter the length of rod L
--> 1

Enter the value of coefficient k


--> 1
Enter the value of coefficient k
1

Enter the number of terms in solution you want n


30
Enter the number of terms in solution you want n
--> 30

Plot 2D:
Plot 3D:
Numerical output:
u1=feval(.4,.2,u)
u1 =
0.1363464

Potrebbero piacerti anche