Thursday, May 2, 2013

MATLAB - Parabolic Equation

parabolic
% _________________________________________________________________________
% To study heat flow in one-dimensional system (insulated rod)
% ie finite difference solution to parabolic equation
% By Mahesha MG, MIT
% Date: 02/05/2013
% _________________________________INPUT___________________________________
clc;
clear;
k1=input('Enter thermal conductivity of the material of rod: ');
cp=input('Enter specific heat of the material of rod: ');
rho=input('Enter density of the material of rod: ');
l=input('Enter length of the rod: ');
h=input('Enter step length: ');
Tl=input('Enter temperature at left side of the rod: ');
Tr=input('Enter temperature at right side of the rod: ');
tmax=input('Enter tmax: ');
f1=input('Enter initial condition (a valid MATLAB expression in x): ','s');
% ___________________________INITIAL CONDITION_____________________________
k=k1/(cp*rho);
tou=h^2/(2*k);        %time step
r=(tou*k)/h^2;
x=0:h:l;
temp=eval(f1);
temp(1)=Tl;
temp(size(x))=Tr;
temp2=temp;
temp1=zeros(size(temp));
temp1(1)=Tl;
temp1(size(x))=Tr;
t0=0;
% _________________________FINITE DIFFERENCE METHOD________________________
while t0<tmax
    t0=t0+tou;
    for i=2:size(x')-1
        temp1(i)=0.5*(temp2(i-1)+temp2(i+1));
    end
    temp2=temp1;
    temp=[temp;temp1];
end
temp             %temperature at different interior points with time
t=0:tou:tmax;
surf(x,t,temp)
xlabel('length in m')
ylabel('Time in s')
zlabel('Temperature in C')
% _________________________SAMPLE INPUT AND RESULT_________________________
% Enter thermal conductivity of the material of rod: 2
% Enter specific heat of the material of rod: 1
% Enter density of the material of rod: 1
% Enter length of the rod: 4
% Enter step length: 1
% Enter temperature at left side of the rod: 0
% Enter temperature at right side of the rod: 0
% Enter tmax: 1.5
% Enter initial condition (a valid MATLAB expression in x): 50*(4-x)
%
% temp =
%
%          0  150.0000  100.0000   50.0000         0
%          0   50.0000  100.0000   50.0000         0
%          0   50.0000   50.0000   50.0000         0
%          0   25.0000   50.0000   25.0000         0
%          0   25.0000   25.0000   25.0000         0
%          0   12.5000   25.0000   12.5000         0
%          0   12.5000   12.5000   12.5000         0
% Problem taken from Numerical Methods by E Balagurusamy
% _________________________________________________________________________

No comments:

Post a Comment